From 914f0aba8ebe9135750ae76b2bfdb961e332c293 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Thu, 1 Oct 2009 11:09:19 +0000
Subject: [PATCH] better gestion of remote messages ("a la getdp")

---
 Common/CommandLine.cpp     |  2 +-
 Common/Gmsh.cpp            |  4 ++-
 Common/GmshMessage.cpp     | 33 ++++++++++++++++++++++++
 Common/GmshMessage.h       |  7 +++++
 Common/GmshRemote.cpp      | 52 ++++++++++++++++----------------------
 Common/GmshRemote.h        |  2 +-
 Common/VertexArray.cpp     |  5 ++--
 Common/VertexArray.h       |  2 +-
 Post/PViewVertexArrays.cpp |  6 +++--
 9 files changed, 74 insertions(+), 39 deletions(-)

diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp
index cc852b39ae..33511f2280 100644
--- a/Common/CommandLine.cpp
+++ b/Common/CommandLine.cpp
@@ -129,7 +129,7 @@ void GetOptions(int argc, char *argv[])
       if(!strcmp(argv[i] + 1, "socket")) {
         i++;        
         if(argv[i])
-          CTX::instance()->solver.socketName = argv[i++];
+          Msg::InitClient(argv[i++]);
         else
           Msg::Fatal("Missing string");
         CTX::instance()->batch = -3;
diff --git a/Common/Gmsh.cpp b/Common/Gmsh.cpp
index c2ee693a21..172bbd1287 100644
--- a/Common/Gmsh.cpp
+++ b/Common/Gmsh.cpp
@@ -139,7 +139,7 @@ int GmshBatch()
 #endif
 
   if(CTX::instance()->batch == -3){
-    GmshRemote(CTX::instance()->solver.socketName);
+    GmshRemote();
   }
   else if(CTX::instance()->batch == -2){
     GModel::current()->checkMeshCoherence(CTX::instance()->geom.tolerance);
@@ -167,5 +167,7 @@ int GmshBatch()
   currtime.resize(currtime.size() - 1);
   Msg::Info("Stopped on %s", currtime.c_str());
 
+  Msg::FinalizeClient();
+
   return 1;
 }
diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp
index e8585db718..8ecda64453 100644
--- a/Common/GmshMessage.cpp
+++ b/Common/GmshMessage.cpp
@@ -9,6 +9,7 @@
 #include <time.h>
 #include "GmshConfig.h"
 #include "GmshMessage.h"
+#include "GmshSocket.h"
 #include "Gmsh.h"
 #include "Options.h"
 #include "Context.h"
@@ -36,6 +37,7 @@ int Msg::_errorCount = 0;
 GmshMessage *Msg::_callback = 0;
 std::string Msg::_commandLine;
 std::string Msg::_launchDate;
+GmshClient *Msg::_client = 0;
 
 #if defined(HAVE_NO_VSNPRINTF)
 static int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
@@ -118,6 +120,7 @@ void Msg::Fatal(const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Fatal", str);
+  if(_client) _client->Error(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -158,6 +161,7 @@ void Msg::Error(const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Error", str);
+  if(_client) _client->Error(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -190,6 +194,7 @@ void Msg::Warning(const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Warning", str);
+  if(_client) _client->Warning(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -216,6 +221,7 @@ void Msg::Info(const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Info", str);
+  if(_client) _client->Info(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -255,6 +261,7 @@ void Msg::Direct(int level, const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Direct", str);
+  if(_client) _client->Info(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -288,6 +295,7 @@ void Msg::StatusBar(int num, bool log, const char *fmt, ...)
   va_end(args);
 
   if(_callback && log) (*_callback)("Info", str);
+  if(_client && log) _client->Info(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -318,6 +326,7 @@ void Msg::Debug(const char *fmt, ...)
   va_end(args);
 
   if(_callback) (*_callback)("Debug", str);
+  if(_client) _client->Info(str);
 
 #if defined(HAVE_FLTK)
   if(FlGui::available()){
@@ -498,6 +507,29 @@ bool Msg::GetBinaryAnswer(const char *question, const char *yes,
   }
 }
 
+void Msg::InitClient(std::string sockname)
+{
+  if(_client) delete _client;
+  _client = new GmshClient();
+  if(_client->Connect(sockname.c_str()) < 0){
+    Msg::Error("Unable to connect to server on %s", sockname.c_str());
+    delete _client;
+    _client = 0;
+  }
+  else
+    _client->Start();
+}
+
+void Msg::FinalizeClient()
+{
+  if(_client){
+    _client->Stop();
+    _client->Disconnect();
+    delete _client;
+  }
+  _client = 0;
+}
+
 void Msg::Barrier()
 {
 #if defined(HAVE_MPI)
@@ -520,3 +552,4 @@ int Msg::GetMaxThreads(){ return 1; }
 int Msg::GetThreadNum(){ return 0; }
 
 #endif
+
diff --git a/Common/GmshMessage.h b/Common/GmshMessage.h
index 8cd2905723..0fb0ecdf10 100644
--- a/Common/GmshMessage.h
+++ b/Common/GmshMessage.h
@@ -10,6 +10,8 @@
 #include <string>
 #include <stdarg.h>
 
+class GmshClient;
+
 // the external message handler
 class GmshMessage{
  public:
@@ -36,6 +38,8 @@ class Msg {
   static GmshMessage *_callback;
   // command-line and startup time
   static std::string _commandLine, _launchDate;
+  // communication with Gmsh when run remotely
+  static GmshClient *_client;
  public:
   Msg() {}
   static void Init(int argc, char **argv);
@@ -72,6 +76,9 @@ class Msg {
   static double GetValue(const char *text, double defaultval);
   static bool GetBinaryAnswer(const char *question, const char *yes, 
                               const char *no,  bool defaultval=true);
+  static void InitClient(std::string sockname);
+  static GmshClient *GetClient(){ return _client; }
+  static void FinalizeClient();
 };
 
 #endif
diff --git a/Common/GmshRemote.cpp b/Common/GmshRemote.cpp
index 9fbe583be0..90857147ae 100644
--- a/Common/GmshRemote.cpp
+++ b/Common/GmshRemote.cpp
@@ -1,3 +1,8 @@
+// Gmsh - Copyright (C) 1997-2009 C. Geuzaine, J.-F. Remacle
+//
+// See the LICENSE.txt file for license information. Please report all
+// bugs and problems to <gmsh@geuz.org>.
+
 #include <sstream>
 #include "GmshMessage.h"
 #include "GmshSocket.h"
@@ -7,12 +12,9 @@
 #include "PViewOptions.h"
 #include "PViewData.h"
 #include "VertexArray.h"
-#include "Context.h"
 
-static void computeAndSendVertexArrays(GmshClient &client)
+static void computeAndSendVertexArrays(GmshClient *client)
 {
-  CTX::instance()->terminal = 1; // debug
-  client.Info("Sending vertex arrays");
   for(unsigned int i = 0; i < PView::list.size(); i++){
     PView *p = PView::list[i];
     p->fillVertexArrays();
@@ -32,54 +34,48 @@ static void computeAndSendVertexArrays(GmshClient &client)
           (p->getNum(), data->getName(), type + 1, min, max, 
            data->getNumTimeSteps(), data->getTime(opt->timeStep),
            data->getBoundingBox(), len);
-        client.SendMessage(GmshSocket::GMSH_VERTEX_ARRAY, len, str);
+        client->SendMessage(GmshSocket::GMSH_VERTEX_ARRAY, len, str);
         delete [] str;
       }
     }
   }
 }
 
-int GmshRemote(std::string socket)
+int GmshRemote()
 {
-  GmshClient client;
-
-  if(client.Connect(socket.c_str()) < 0){
-    Msg::Error("Unable to connect to server on %s", socket.c_str());
-    return 1;
-  }
-  client.Start();
-  client.Info("Remote Gmsh sucessfully started");
+  GmshClient *client = Msg::GetClient();
+  
+  if(!client) return 0;
 
   computeAndSendVertexArrays(client);
 
-  client.Info("Remote Gmsh is listening...");
   while(1){
     // stop if we have no communications for 5 minutes
-    int ret = client.Select(300, 0);
+    int ret = client->Select(300, 0);
     if(!ret){
-      client.Info("Timout: stopping remote Gmsh...");
+      client->Info("Timout: stopping remote Gmsh...");
       break;
     }
     else if(ret < 0){
-      client.Error("Error on select: stopping remote Gmsh...");
+      client->Error("Error on select: stopping remote Gmsh...");
       break;
     }
 
     int type, length;
-    if(!client.ReceiveHeader(&type, &length)){
-      client.Error("Did not receive message header: stopping remote Gmsh...");
+    if(!client->ReceiveHeader(&type, &length)){
+      client->Error("Did not receive message header: stopping remote Gmsh...");
       break;
     }
       
     char *msg = new char[length + 1];
-    if(!client.ReceiveString(length, msg)){
-      client.Error("Did not receive message body: stopping remote Gmsh...");
+    if(!client->ReceiveString(length, msg)){
+      client->Error("Did not receive message body: stopping remote Gmsh...");
       delete [] msg;
       break;
     }
 
     if(type == GmshSocket::GMSH_STOP){
-      client.Info("Stopping remote Gmsh...");
+      client->Info("Stopping remote Gmsh...");
       break;
     }
     else if(type == GmshSocket::GMSH_VERTEX_ARRAY){
@@ -94,20 +90,16 @@ int GmshRemote(std::string socket)
       ParseString(msg);
     }
     else if(type == GmshSocket::GMSH_SPEED_TEST){
-      client.Info("Sending huge array");
+      client->Info("Sending huge array");
       std::string huge(500000000, 'a');
-      client.SpeedTest(huge.c_str());
+      client->SpeedTest(huge.c_str());
     }
     else{
-      client.Error("Ignoring unknown message");
+      client->Error("Ignoring unknown message");
     }
     
     delete [] msg;
   }
 
-  client.Info("Remote Gmsh is stopped");
-  client.Stop();
-  client.Disconnect();
-
   return 0;
 }
diff --git a/Common/GmshRemote.h b/Common/GmshRemote.h
index 6ea08a1b47..b441a76e50 100644
--- a/Common/GmshRemote.h
+++ b/Common/GmshRemote.h
@@ -8,7 +8,7 @@
 
 #include <string>
 
-int GmshRemote(std::string socket);
+int GmshRemote();
 
 #endif
 
diff --git a/Common/VertexArray.cpp b/Common/VertexArray.cpp
index da07a08824..2bfe2c8d63 100644
--- a/Common/VertexArray.cpp
+++ b/Common/VertexArray.cpp
@@ -123,7 +123,6 @@ void VertexArray::finalize()
     _data3.clear();
   }
   _barycenters.clear();
-  //printf("vert array : %d Mb\n", getMemoryUsage());
 }
 
 class AlphaElement {
@@ -210,11 +209,11 @@ void VertexArray::sort(double x, double y, double z)
   _colors = sortedColors;
 }
 
-int VertexArray::getMemoryUsage()
+double VertexArray::getMemoryInMb()
 {
   int bytes = _vertices.size() * sizeof(float) + _normals.size() * sizeof(char) +
     _colors.size() * sizeof(unsigned char);
-  return bytes / 1024 / 1024;
+  return (double)bytes / 1024. / 1024.;
 }
 
 char *VertexArray::toChar(int num, std::string name, int type, double min, double max, 
diff --git a/Common/VertexArray.h b/Common/VertexArray.h
index 53916a84d7..47e5158ebc 100644
--- a/Common/VertexArray.h
+++ b/Common/VertexArray.h
@@ -161,7 +161,7 @@ class VertexArray{
   // sort the arrays with elements back to front wrt the eye position
   void sort(double x, double y, double z);
   // estimate the size of the vertex array in megabytes
-  int getMemoryUsage();
+  double getMemoryInMb();
   // serialize the vertex array into a string (for sending over the
   // network)
   char *toChar(int num, std::string name, int type, double min, double max, 
diff --git a/Post/PViewVertexArrays.cpp b/Post/PViewVertexArrays.cpp
index f621afc81a..03f9423564 100644
--- a/Post/PViewVertexArrays.cpp
+++ b/Post/PViewVertexArrays.cpp
@@ -1114,9 +1114,11 @@ class initPView {
     p->va_triangles->finalize();
     p->va_vectors->finalize();
 
-    Msg::Info("%d vertices in vertex arrays", p->va_points->getNumVertices() + 
+    Msg::Info("%d vertices in vertex arrays (%g Mb)", p->va_points->getNumVertices() + 
               p->va_lines->getNumVertices() + p->va_triangles->getNumVertices() + 
-              p->va_vectors->getNumVertices());
+              p->va_vectors->getNumVertices(), p->va_points->getMemoryInMb() +
+              p->va_lines->getMemoryInMb() + p->va_triangles->getMemoryInMb() + 
+              p->va_vectors->getMemoryInMb());
 
     p->setChanged(false);
   }
-- 
GitLab