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

Better sockets

parent 56cf8279
No related branches found
No related tags found
No related merge requests found
/* $Id: Socket.cpp,v 1.4 2001-05-03 01:04:07 geuzaine Exp $ */ /* $Id: Socket.cpp,v 1.5 2001-05-04 11:53:01 geuzaine Exp $ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -150,12 +150,28 @@ int Socket_StopProgram(char *progname, char *sockname, int sock){ ...@@ -150,12 +150,28 @@ int Socket_StopProgram(char *progname, char *sockname, int sock){
return 0; return 0;
} }
long Socket_GetTime(){
struct timeval tp;
gettimeofday(&tp, (struct timezone *)0);
return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec;
}
void Socket_Idle(double delay){
long t1 = Socket_GetTime();
while(1){
if(Socket_GetTime() - t1 > 1.e6*delay) break;
}
}
int Socket_Connect(char *sockname){ int Socket_Connect(char *sockname){
struct sockaddr_un addr; struct sockaddr_un addr;
int len, sock; int len, sock;
int tries; int tries;
/* slight delay to be sure that the socket gets created by the
server before we attempt to connect to it... */
Socket_Idle(0.1);
/* create socket */ /* create socket */
sock = socket(PF_UNIX, SOCK_STREAM, 0); sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock<0){ if (sock<0){
...@@ -169,7 +185,7 @@ int Socket_Connect(char *sockname){ ...@@ -169,7 +185,7 @@ int Socket_Connect(char *sockname){
len = strlen(addr.sun_path)+sizeof(addr.sun_family); len = strlen(addr.sun_path)+sizeof(addr.sun_family);
for (tries=0;tries<5;tries++) { for (tries=0;tries<5;tries++) {
if (connect(sock, (struct sockaddr *)&addr, len) < 0) { if (connect(sock, (struct sockaddr *)&addr, len) < 0) {
Msg(GERROR, "Socket connect failed"); Msg(WARNING, "Socket connect failed on '%s'", sockname);
} }
else { else {
return sock; return sock;
......
// $Id: Solvers.cpp,v 1.1 2001-05-03 00:09:42 geuzaine Exp $ // $Id: Solvers.cpp,v 1.2 2001-05-04 11:53:01 geuzaine Exp $
#include "Gmsh.h" #include "Gmsh.h"
...@@ -43,11 +43,11 @@ extern GUI *WID; ...@@ -43,11 +43,11 @@ extern GUI *WID;
_GetDP_Info GetDP_Info ; _GetDP_Info GetDP_Info ;
int GetDP(char *args){ int GetDP(char *args){
int sock, i, n ; int sock, type, i, n ;
char progname[1000], sockname[1000], str[1000]; char progname[1000], sockname[1000], str[1000];
GET_PATH(sockname); GET_PATH(sockname);
strcat(sockname, ".gmsh-socket-GetDP"); strcat(sockname, ".gmshsock");
sprintf(progname, "getdp %s", args); sprintf(progname, "getdp %s", args);
sock = Socket_StartProgram(progname, sockname); sock = Socket_StartProgram(progname, sockname);
...@@ -56,37 +56,40 @@ int GetDP(char *args){ ...@@ -56,37 +56,40 @@ int GetDP(char *args){
return 0; return 0;
} }
Msg(INFO, "GetDP start (%s)", progname);
GetDP_Info.nbres = 0; GetDP_Info.nbres = 0;
GetDP_Info.nbpostop = 0; GetDP_Info.nbpostop = 0;
while(1){ while(1){
Socket_ReceiveInt(sock, &i); Socket_ReceiveInt(sock, &type);
if(i<0){ if(type == GETDP_END) break;
Msg(INFO, "Finished '%s'", progname);
break;
}
Socket_ReceiveString(sock, str); Socket_ReceiveString(sock, str);
switch(i){ switch(type){
case 10 : // status case GETDP_PROGRESS :
Msg(STATUS3N, "GetDP %s", str); Msg(STATUS3N, "GetDP %s", str);
break ; break ;
case 101 : // available resolution case GETDP_RESOLUTION_NAME :
strcpy(GetDP_Info.res[GetDP_Info.nbres++],str); strcpy(GetDP_Info.res[GetDP_Info.nbres++],str);
break ; break ;
case 102 : // available postoperations case GETDP_POSTOPERATION_NAME :
strcpy(GetDP_Info.postop[GetDP_Info.nbpostop++],str); strcpy(GetDP_Info.postop[GetDP_Info.nbpostop++],str);
break ; break ;
case 200 : // load postprocessing file case GETDP_LOAD_VIEW :
n = List_Nbr(Post_ViewList); n = List_Nbr(Post_ViewList);
MergeProblem(str); MergeProblem(str);
Draw(); Draw();
if(n != List_Nbr(Post_ViewList)) if(n != List_Nbr(Post_ViewList))
WID->set_context(menu_post, 0); WID->set_context(menu_post, 0);
break ; break ;
case GETDP_INFO :
Msg(DIRECT, "GetDP > %s", str);
break;
default : default :
Msg(WARNING, "Unknown type of message received from GetDP");
Msg(DIRECT, "GetDP > %s", str); Msg(DIRECT, "GetDP > %s", str);
break ; break ;
} }
...@@ -107,9 +110,12 @@ int GetDP(char *args){ ...@@ -107,9 +110,12 @@ int GetDP(char *args){
WID->getdp_choice[1]->value(0); WID->getdp_choice[1]->value(0);
} }
Msg(STATUS3N, "");
Socket_StopProgram(progname, sockname, sock); Socket_StopProgram(progname, sockname, sock);
Msg(INFO, "GetDP stop (%s)", progname);
return 1; return 1;
} }
......
#ifndef _SOLVERS_H_ #ifndef _SOLVERS_H_
#define _SOLVERS_H_ #define _SOLVERS_H_
// Message protocol for GetDP
#define GETDP_END -1
#define GETDP_INFO 1
#define GETDP_PROGRESS 2
#define GETDP_RESOLUTION_NAME 3
#define GETDP_POSTOPERATION_NAME 4
#define GETDP_LOAD_VIEW 5
typedef struct{ typedef struct{
int nbres, nbpostop; int nbres, nbpostop;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment