Skip to content
Snippets Groups Projects
Commit 27d0a4f6 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

trying to fix FH's last commit

parent d0176036
Branches
Tags
No related merge requests found
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#define _GMSH_SOCKET_H_ #define _GMSH_SOCKET_H_
#include "GmshConfig.h" #include "GmshConfig.h"
#include <string>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -82,7 +83,7 @@ class GmshSocket{ ...@@ -82,7 +83,7 @@ class GmshSocket{
// the socket descriptor // the socket descriptor
int _sock; int _sock;
// the socket name // the socket name
const char *_sockname; std::string _sockname;
// send some data over the socket // send some data over the socket
void _SendData(const void *buffer, int bytes) void _SendData(const void *buffer, int bytes)
{ {
...@@ -132,7 +133,7 @@ class GmshSocket{ ...@@ -132,7 +133,7 @@ class GmshSocket{
#endif #endif
} }
public: public:
GmshSocket() : _sock(0), _sockname(0) GmshSocket() : _sock(0)
{ {
#if defined(WIN32) && !defined(__CYGWIN__) #if defined(WIN32) && !defined(__CYGWIN__)
WSADATA wsaData; WSADATA wsaData;
...@@ -276,7 +277,6 @@ class GmshClient : public GmshSocket { ...@@ -276,7 +277,6 @@ class GmshClient : public GmshSocket {
if(remotelen > 0) if(remotelen > 0)
strncpy(remote, sockname, remotelen); strncpy(remote, sockname, remotelen);
remote[remotelen] = '\0'; remote[remotelen] = '\0';
printf("FHF Client conected on portno=%d remote=%s\n",portno,remote);
struct hostent *server; struct hostent *server;
if(!(server = gethostbyname(remote))){ if(!(server = gethostbyname(remote))){
CloseSocket(_sock); CloseSocket(_sock);
...@@ -321,40 +321,37 @@ class GmshServer : public GmshSocket{ ...@@ -321,40 +321,37 @@ class GmshServer : public GmshSocket{
virtual int NonBlockingWait(int socket, double waitint, double timeout) = 0; virtual int NonBlockingWait(int socket, double waitint, double timeout) = 0;
int Start(const char *command, const char *sockname, double timeout) int Start(const char *command, const char *sockname, double timeout)
{ {
char cmd[1024], soc[1024];
strcpy(soc, sockname); // sockname is 'const'
strcpy(cmd, command); // command is 'const'
if(!sockname) throw "Invalid (null) socket name"; if(!sockname) throw "Invalid (null) socket name";
_sockname = sockname; _sockname = sockname;
int tmpsock; int tmpsock;
if(strstr(_sockname, "/") || strstr(_sockname, "\\") || !strstr(_sockname, ":")){ if(strstr(_sockname.c_str(), "/") || strstr(_sockname.c_str(), "\\") ||
!strstr(_sockname.c_str(), ":")){
// UNIX socket (testing ":" is not enough with Windows paths) // UNIX socket (testing ":" is not enough with Windows paths)
_portno = -1; _portno = -1;
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
// delete the file if it already exists // delete the file if it already exists
unlink(_sockname); unlink(_sockname.c_str());
// create a socket // create a socket
tmpsock = socket(PF_UNIX, SOCK_STREAM, 0); tmpsock = socket(PF_UNIX, SOCK_STREAM, 0);
if(tmpsock < 0) throw "Couldn't create socket"; if(tmpsock < 0) throw "Couldn't create socket";
// bind the socket to its name // bind the socket to its name
struct sockaddr_un addr_un; struct sockaddr_un addr_un;
memset((char *) &addr_un, 0, sizeof(addr_un)); memset((char *) &addr_un, 0, sizeof(addr_un));
strcpy(addr_un.sun_path, _sockname); strcpy(addr_un.sun_path, _sockname.c_str());
addr_un.sun_family = AF_UNIX; addr_un.sun_family = AF_UNIX;
if(bind(tmpsock, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0){ if(bind(tmpsock, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0){
CloseSocket(tmpsock); CloseSocket(tmpsock);
throw "Couldn't bind socket to name"; throw "Couldn't bind socket to name";
} }
// change permissions on the socket name in case it has to be rm'd later // change permissions on the socket name in case it has to be rm'd later
chmod(_sockname, 0666); chmod(_sockname.c_str(), 0666);
#else #else
throw "Unix sockets not available on Windows"; throw "Unix sockets not available on Windows";
#endif #endif
} }
else{ else{
// TCP/IP socket // TCP/IP socket
const char *port = strstr(_sockname, ":"); const char *port = strstr(_sockname.c_str(), ":");
_portno = atoi(port + 1); // can be zero in case of random port assignation _portno = atoi(port + 1); // can be zero in case of random port assignation
// create a socket // create a socket
tmpsock = socket(AF_INET, SOCK_STREAM, 0); tmpsock = socket(AF_INET, SOCK_STREAM, 0);
...@@ -374,24 +371,25 @@ class GmshServer : public GmshSocket{ ...@@ -374,24 +371,25 @@ class GmshServer : public GmshSocket{
CloseSocket(tmpsock); CloseSocket(tmpsock);
throw "Couldn't bind socket to name"; throw "Couldn't bind socket to name";
} }
// retrieve the number of the randomly assigned port if(!_portno){ // retrieve name if randomly assigned port
socklen_t addrlen = sizeof(addr_in); socklen_t addrlen = sizeof(addr_in);
int rc = getsockname(tmpsock, (struct sockaddr *)&addr_in, &addrlen); int rc = getsockname(tmpsock, (struct sockaddr *)&addr_in, &addrlen);
_portno = ntohs(addr_in.sin_port); _portno = ntohs(addr_in.sin_port);
printf("FHF server listening on portno=%d\n",_portno);
char tmp[10]; char tmp[10];
sprintf(tmp, "%d", _portno); sprintf(tmp, "%d", _portno);
strcat(soc, tmp); _sockname = tmp;
_sockname = soc; }
} }
strcat(cmd, soc); if(command && strlen(command)){
// FIXME: this is ugly - we assume that the command line always
// ends with the socket name
std::string cmd(command);
cmd += " " + _sockname;
#if !defined(WIN32) #if !defined(WIN32)
strcat(cmd, " &"); cmd += " &";
#endif #endif
SystemCall(cmd.c_str()); // start the solver
if(cmd && strlen(cmd)){
SystemCall(cmd); // start the solver
} }
else{ else{
timeout = 0.; // no command launched: don't set a timeout timeout = 0.; // no command launched: don't set a timeout
...@@ -439,7 +437,7 @@ class GmshServer : public GmshSocket{ ...@@ -439,7 +437,7 @@ class GmshServer : public GmshSocket{
{ {
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
if(_portno < 0) if(_portno < 0)
unlink(_sockname); unlink(_sockname.c_str());
#endif #endif
ShutdownSocket(_sock); ShutdownSocket(_sock);
CloseSocket(_sock); CloseSocket(_sock);
......
...@@ -93,8 +93,7 @@ bool onelab::localNetworkClient::run(const std::string &what) ...@@ -93,8 +93,7 @@ bool onelab::localNetworkClient::run(const std::string &what)
if(CTX::instance()->solver.socketName.size() && if(CTX::instance()->solver.socketName.size() &&
CTX::instance()->solver.socketName[0] == ':') CTX::instance()->solver.socketName[0] == ':')
tmp << GetHostName(); // prepend hostname if only the port number is given tmp << GetHostName(); // prepend hostname if only the port number is given
//tmp << CTX::instance()->solver.socketName << getId(); tmp << CTX::instance()->solver.socketName << getId();
tmp << CTX::instance()->solver.socketName;
sockname = tmp.str(); sockname = tmp.str();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment