From e2c1f687b09736e34bd4b13a65d3acbd9732ec32 Mon Sep 17 00:00:00 2001
From: Maxime Graulich <maxime.graulich@gmail.com>
Date: Tue, 30 Jun 2015 14:17:03 +0000
Subject: [PATCH] ONELAB2

---
 Fltk/onelab2Group.cpp                        |  2 +-
 contrib/onelab2/OnelabAttributes.h           |  2 +-
 contrib/onelab2/OnelabLocalNetworkClient.cpp | 28 +++++++++++---------
 contrib/onelab2/OnelabLocalNetworkClient.h   | 28 ++++++++++----------
 contrib/onelab2/OnelabNetworkClient.cpp      |  7 +++--
 contrib/onelab2/OnelabServer.cpp             |  1 +
 6 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/Fltk/onelab2Group.cpp b/Fltk/onelab2Group.cpp
index a531620537..6517dba073 100644
--- a/Fltk/onelab2Group.cpp
+++ b/Fltk/onelab2Group.cpp
@@ -262,7 +262,7 @@ onelabGroup::onelabGroup(int x, int y, int w, int h, const char *l)
 
   rebuildSolverList();
 
-  OnelabDatabase::instance()->useAsClient()->setCallback(this);
+  if(!OnelabDatabase::instance()->isNetworkClient()) OnelabDatabase::instance()->useAsClient()->setCallback(this);
 }
 onelabGroup::~onelabGroup()
 {
diff --git a/contrib/onelab2/OnelabAttributes.h b/contrib/onelab2/OnelabAttributes.h
index 433a501960..f06a0e9137 100644
--- a/contrib/onelab2/OnelabAttributes.h
+++ b/contrib/onelab2/OnelabAttributes.h
@@ -94,7 +94,7 @@ public:
 class OnelabAttrMessage : public OnelabAttr
 {
 private:
-	UInt8 *_message = NULL;
+	UInt8 *_message;
 	UInt16 _messageLength;
 	UInt8 _level;
 	
diff --git a/contrib/onelab2/OnelabLocalNetworkClient.cpp b/contrib/onelab2/OnelabLocalNetworkClient.cpp
index 32cb4a5b9b..17635b3ead 100644
--- a/contrib/onelab2/OnelabLocalNetworkClient.cpp
+++ b/contrib/onelab2/OnelabLocalNetworkClient.cpp
@@ -4,37 +4,39 @@
 #ifdef HAVE_UDT
 OnelabLocalNetworkClient::OnelabLocalNetworkClient(std::string name, UDTSOCKET fd, unsigned int ip, unsigned short port, bool UDT)
 {
-	_name = name;
+  _mutex_wait = PTHREAD_MUTEX_INITIALIZER;
+  _name = name;
   _fds = 0;
-	_fdu = fd;
-	_ip.address = ip;
-	_ip.port = port;
+  _fdu = fd;
+  _ip.address = ip;
+  _ip.port = port;
 }
 #endif
 OnelabLocalNetworkClient::OnelabLocalNetworkClient(std::string name, Socket fd, unsigned int ip, unsigned short port)
 {
-	_name = name;
-	_fds = fd;
-	_ip.address = ip;
-	_ip.port = port;
+  _mutex_wait = PTHREAD_MUTEX_INITIALIZER;
+  _name = name;
+  _fds = fd;
+  _ip.address = ip;
+  _ip.port = port;
 }
 void OnelabLocalNetworkClient::sendto(UInt8 *buff, unsigned int len)
 {
 #ifdef HAVE_UDT
-	if(_fds) ip4_socket_send(_fds, buff, len);
+  if(_fds) ip4_socket_send(_fds, buff, len);
   else udt_socket_send(_fdu, buff, len);
 #else
-	ip4_socket_send(_fds, buff, len);
+  ip4_socket_send(_fds, buff, len);
 #endif
 }
 int OnelabLocalNetworkClient::recvfrom(UInt8 *buff, unsigned int maxlen)
 {
   IPv4 unused;
 #ifdef HAVE_UDT
-	if(_fds) return ip4_socket_recv(_fds, buff, maxlen, unused);
-	return udt_socket_recv(_fdu, buff, maxlen);
+  if(_fds) return ip4_socket_recv(_fds, buff, maxlen, unused);
+  return udt_socket_recv(_fdu, buff, maxlen);
 #else
-	return ip4_socket_recv(_fds, buff, maxlen);
+  return ip4_socket_recv(_fds, buff, maxlen);
 #endif
 }
 int OnelabLocalNetworkClient::recvmsg(OnelabProtocol &msg)
diff --git a/contrib/onelab2/OnelabLocalNetworkClient.h b/contrib/onelab2/OnelabLocalNetworkClient.h
index 68d499a5b8..cdc54a12fe 100644
--- a/contrib/onelab2/OnelabLocalNetworkClient.h
+++ b/contrib/onelab2/OnelabLocalNetworkClient.h
@@ -14,26 +14,26 @@ class OnelabLocalNetworkClient
 private:
   Socket _fds;
 #ifdef HAVE_UDT
-	UDTSOCKET _fdu;
+  UDTSOCKET _fdu;
 #endif
-	IPv4 _ip;
-	std::string _name;
-  pthread_mutex_t _mutex_wait = PTHREAD_MUTEX_INITIALIZER;
+  IPv4 _ip;
+  std::string _name;
+  pthread_mutex_t _mutex_wait;
 public:
 #ifdef HAVE_UDT
-	OnelabLocalNetworkClient(std::string name, UDTSOCKET fd, unsigned int ip, unsigned short port, bool UDT);
-	UDTSOCKET getUSocket() {return _fdu;}
+  OnelabLocalNetworkClient(std::string name, UDTSOCKET fd, unsigned int ip, unsigned short port, bool UDT);
+  UDTSOCKET getUSocket() {return _fdu;}
 #endif
-	OnelabLocalNetworkClient(std::string name, Socket fd, unsigned int ip, unsigned short port);
-	virtual ~OnelabLocalNetworkClient(){}
-	void sendto(UInt8 *buff, unsigned int len);
-	int recvfrom(UInt8 *buff, unsigned int maxlen);
+  OnelabLocalNetworkClient(std::string name, Socket fd, unsigned int ip, unsigned short port);
+  virtual ~OnelabLocalNetworkClient(){}
+  void sendto(UInt8 *buff, unsigned int len);
+  int recvfrom(UInt8 *buff, unsigned int maxlen);
   int recvmsg(OnelabProtocol &msg);
-	Socket getSSocket() {return _fds;}
-	std::string getName() {return _name;}
+  Socket getSSocket() {return _fds;}
+  std::string getName() {return _name;}
   void updateParameter(onelab::parameter *);
-	unsigned int getIp() {return _ip.address;}
-	unsigned short getPort() {return _ip.port;}
+  unsigned int getIp() {return _ip.address;}
+  unsigned short getPort() {return _ip.port;}
 
   void run(std::string action);
 };
diff --git a/contrib/onelab2/OnelabNetworkClient.cpp b/contrib/onelab2/OnelabNetworkClient.cpp
index 5c2fd2fe70..30aaed9717 100644
--- a/contrib/onelab2/OnelabNetworkClient.cpp
+++ b/contrib/onelab2/OnelabNetworkClient.cpp
@@ -147,7 +147,7 @@ bool OnelabNetworkClient::connect()
     if(_sockname.size())
       unix_socket_connect(_fds, _sockname.c_str());
     else
-      ip4_socket_connect(_fds, _ip);
+      _connected = ip4_socket_connect(_fds, _ip) != -1;
   }
   else
     udt_socket_connect(_fdu, _ip);
@@ -155,8 +155,11 @@ bool OnelabNetworkClient::connect()
   if(_sockname.size())
     unix_socket_connect(_fds, _sockname.c_str());
   else
-    ip4_socket_connect(_fds, _ip);
+    _connected = ip4_socket_connect(_fds, _ip) != -1;
 #endif
+
+  if(!_connected) return false;
+
   msg.attrs.push_back(new OnelabAttrStart(_name));
   recvlen = msg.encodeMsg(buff, bufflen);
   sendto(buff, recvlen);
diff --git a/contrib/onelab2/OnelabServer.cpp b/contrib/onelab2/OnelabServer.cpp
index 40b72c48d4..8f3b950971 100644
--- a/contrib/onelab2/OnelabServer.cpp
+++ b/contrib/onelab2/OnelabServer.cpp
@@ -836,6 +836,7 @@ void *listenOnClients(void *param)
           {
             if(msg.attrs.size()==1 && msg.attrs[0]->getAttributeType() == OnelabAttrAction::attributeType()) {
               std::clog << "\033[0;31m" << "Client " << cli->getName() << " ask " << ((OnelabAttrAction *)msg.attrs[0])->getClient() << " to " << ((OnelabAttrAction *)msg.attrs[0])->getAction() <<  "\033[0m" << std::endl;
+              //FIXME you may want to run the solver (see OnelabDatabase)
               OnelabServer::instance()->performAction(((OnelabAttrAction *)msg.attrs[0])->getAction(), ((OnelabAttrAction *)msg.attrs[0])->getClient());
             }
           }
-- 
GitLab