diff --git a/Common/Timer.cpp b/Common/Timer.cpp index 575053719e4af1487567d488e23ce36b3df08c2e..991d559783c1404d7179c441dfd49c4af1df779f 100644 --- a/Common/Timer.cpp +++ b/Common/Timer.cpp @@ -1,4 +1,4 @@ -// $Id: Timer.cpp,v 1.17 2006-02-24 22:07:06 geuzaine Exp $ +// $Id: Timer.cpp,v 1.18 2006-02-25 00:15:00 geuzaine Exp $ // // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle // @@ -32,9 +32,9 @@ long GetTimeMilliSeconds() return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec; } -void SleepMilliSeconds(int usec) +void SleepMilliSeconds(int ms) { - usleep(usec); + usleep(1000 * ms); } #else // pure windows @@ -49,9 +49,9 @@ long GetTimeMilliSeconds() return (long)ft.dwHighDateTime * 100000 + (long)ft.dwLowDateTime / 10; } -void SleepMilliSeconds(int usec) +void SleepMilliSeconds(int ms) { - Sleep(usec); + Sleep(ms); } #endif diff --git a/Common/Timer.h b/Common/Timer.h index 777cc4abc7ae54a6f127fcb031ece4a74e889ef0..4d380bbc3dc010f3a601b4c1e8b991bea440074d 100644 --- a/Common/Timer.h +++ b/Common/Timer.h @@ -21,6 +21,6 @@ // Please report all bugs and problems to <gmsh@geuz.org>. long GetTimeMilliSeconds(); -void SleepMilliSeconds(int usec); +void SleepMilliSeconds(int ms); #endif diff --git a/Fltk/Callbacks.cpp b/Fltk/Callbacks.cpp index 7bdea4143b9e53843db5b06577065b413e9997d5..733f20ae5437ec7ceadd0c619e6753ff9911f60d 100644 --- a/Fltk/Callbacks.cpp +++ b/Fltk/Callbacks.cpp @@ -1,4 +1,4 @@ -// $Id: Callbacks.cpp,v 1.409 2006-02-24 22:07:06 geuzaine Exp $ +// $Id: Callbacks.cpp,v 1.410 2006-02-25 00:15:00 geuzaine Exp $ // // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle // @@ -3265,8 +3265,8 @@ void solver_kill_cb(CALLBACK_ARGS) return; } #endif + Msg(INFO, "Killed %s pid %d", SINFO[num].name, SINFO[num].pid); } - Msg(INFO, "Killed %s pid %d", SINFO[num].name, SINFO[num].pid); SINFO[num].pid = -1; } diff --git a/utils/solvers/c++/GmshClient.h b/utils/solvers/c++/GmshClient.h index 5742f21f45caa4e4175e6c57d8ceea6f921ac7a1..6a2f00fd8068590fbcd9feb98c7779eef254a2ba 100644 --- a/utils/solvers/c++/GmshClient.h +++ b/utils/solvers/c++/GmshClient.h @@ -1,7 +1,7 @@ #ifndef _GMSH_CLIENT_H_ #define _GMSH_CLIENT_H_ -// Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle +// Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -86,12 +86,12 @@ class GmshClient { _SendData(&len, sizeof(int)); _SendData(str, len); } - void _Idle(int usec) + void _Idle(int ms) { #if !defined(WIN32) || defined(__CYGWIN__) - usleep(usec); + usleep(1000 * ms); #else - Sleep(usec); + Sleep(ms); #endif } public: diff --git a/utils/solvers/c/GmshClient.c b/utils/solvers/c/GmshClient.c index c37b6c497fe5cadafcfa62414f3a222ab67763e8..f0bd8909cfd2c38217a520bd95d15624c368cb2a 100644 --- a/utils/solvers/c/GmshClient.c +++ b/utils/solvers/c/GmshClient.c @@ -1,6 +1,6 @@ -/* $Id: GmshClient.c,v 1.4 2005-09-25 18:51:27 geuzaine Exp $ */ +/* $Id: GmshClient.c,v 1.5 2006-02-25 00:15:01 geuzaine Exp $ */ /* - * Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle + * Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -39,18 +39,19 @@ #if !defined(WIN32) || defined(__CYGWIN__) +#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> #include <sys/time.h> -#include <unistd.h> #include <netinet/in.h> #include <netdb.h> #else /* pure windows */ -#include <winsock2.h> +#include <winsock.h> +#include <process.h> #endif @@ -71,25 +72,12 @@ static void Socket_SendData(int socket, void *buffer, int bytes) } while(remaining > 0); } -static long Socket_GetTime() +static void Socket_Idle(int ms) { #if !defined(WIN32) || defined(__CYGWIN__) - struct timeval tp; - gettimeofday(&tp, (struct timezone *)0); - return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec; + usleep(1000 * ms); #else - return 0; -#endif -} - -static void Socket_Idle(double delay) -{ -#if !defined(WIN32) || defined(__CYGWIN__) - long t1 = Socket_GetTime(); - while(1) { - if(Socket_GetTime() - t1 > 1.e6 * delay) - break; - } + Sleep(ms); #endif } @@ -97,7 +85,13 @@ static void Socket_Idle(double delay) int Gmsh_Connect(char *sockname) { +#if !defined(WIN32) || defined(__CYGWIN__) struct sockaddr_un addr_un; +#else + WSADATA wsaData; + int iResult; +#endif + struct sockaddr_in addr_in; int sock; int tries; @@ -105,9 +99,15 @@ int Gmsh_Connect(char *sockname) int portno, remotelen; char remote[256], *port; +#if defined(WIN32) && !defined(__CYGWIN__) + iResult = WSAStartup(MAKEWORD(2,2), &wsaData); + if(iResult != NO_ERROR) + return -4; /* Error: Couldn't initialize Windows sockets */ +#endif + /* slight delay to be sure that the socket is bound by the server before we attempt to connect to it... */ - Socket_Idle(0.1); + Socket_Idle(100); if(strstr(sockname, "/") || strstr(sockname, "\\") || !strstr(sockname, ":")){ /* UNIX socket (testing ":" is not enough with Windows paths) */ @@ -126,6 +126,7 @@ int Gmsh_Connect(char *sockname) /* create socket */ if(portno < 0){ +#if !defined(WIN32) || defined(__CYGWIN__) sock = socket(PF_UNIX, SOCK_STREAM, 0); if(sock < 0) return -1; /* Error: Couldn't create socket */ @@ -135,8 +136,12 @@ int Gmsh_Connect(char *sockname) for(tries = 0; tries < 5; tries++) { if(connect(sock, (struct sockaddr *)&addr_un, sizeof(addr_un)) >= 0) return sock; - Socket_Idle(0.1); + Socket_Idle(100); } +#else + /* Unix sockets are not available on Windows without Cygwin */ + return -1; +#endif } else{ sock = socket(AF_INET, SOCK_STREAM, 0); @@ -149,11 +154,10 @@ int Gmsh_Connect(char *sockname) addr_in.sin_family = AF_INET; memcpy((char *)&addr_in.sin_addr.s_addr, (char *)server->h_addr, server->h_length); addr_in.sin_port = htons(portno); - addr_in.sin_addr.s_addr = INADDR_ANY; for(tries = 0; tries < 5; tries++) { if(connect(sock, (struct sockaddr *)&addr_in, sizeof(addr_in)) >= 0) return sock; - Socket_Idle(0.1); + Socket_Idle(100); } } @@ -170,5 +174,10 @@ void Gmsh_SendString(int socket, int type, char str[]) void Gmsh_Disconnect(int sock) { +#if !defined(WIN32) || defined(__CYGWIN__) close(sock); +#else + closesocket(sock); + WSACleanup(); +#endif } diff --git a/utils/solvers/c/GmshClient.h b/utils/solvers/c/GmshClient.h index 1b0a3de4c0b026a5aa5328acbecf103af632c834..b3239ba2cf5991b7575ebaa7a491aed2a2c1fd49 100644 --- a/utils/solvers/c/GmshClient.h +++ b/utils/solvers/c/GmshClient.h @@ -2,7 +2,7 @@ #define _GMSH_CLIENT_H_ /* - * Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle + * Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -49,5 +49,4 @@ int Gmsh_Connect(char *sockname); void Gmsh_SendString(int socket, int type, char str[]); void Gmsh_Disconnect(int sock); - #endif diff --git a/utils/solvers/c/solver.c b/utils/solvers/c/solver.c index 06d1596c697ec405c9159d28353a4b0c9a01f18d..2846d45fa1310c85ef6543776af12260b50fc97b 100644 --- a/utils/solvers/c/solver.c +++ b/utils/solvers/c/solver.c @@ -1,4 +1,4 @@ -/* $Id: solver.c,v 1.3 2006-02-23 21:59:08 geuzaine Exp $ */ +/* $Id: solver.c,v 1.4 2006-02-25 00:15:01 geuzaine Exp $ */ /* * Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle * @@ -44,52 +44,13 @@ solver menu. */ -/* We start by including some standard headers. Under Windows, you - will need to install the cygwin tools (http://www.cygwin.com) to - compile this example (as well as your own solver), since the Gmsh - solver interface uses Unix sockets. */ - #include <stdlib.h> #include <stdio.h> #include <string.h> -#include <sys/types.h> -#include <sys/time.h> -#include <unistd.h> - -/* Now we include the Gmsh client interface definitions. At the time - of this writing, the client interface contains only three - functions: Gmsh_Connect, Gmsh_SendString and Gmsh_Disconnect. This - example shows how to use these functions in order to program some - simple interactions between a solver and Gmsh. */ - #include "GmshClient.h" -/* The following typedef defines the two actions of our dummy solver: - either output some valid option strings, or run a dummy computation - and output a post-processing map. */ - typedef enum { options, run } action; -/* Let's now define some fake CPU intensive functions: */ - -long worktime() -{ - struct timeval tp; - gettimeofday(&tp, (struct timezone *)0); - return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec; -} - -void work() -{ - long t1 = worktime(); - while(1) { - if(worktime() - t1 > 1.e5) - break; - } -} - -/* And here we go with the main routine of the solver: */ - int main(int argc, char *argv[]) { action what = run; @@ -180,7 +141,12 @@ int main(int argc, char *argv[]) for(i = 0; i < 10; i++) { sprintf(tmp, "%d %% complete", 10*i); Gmsh_SendString(s, GMSH_CLIENT_PROGRESS, tmp); - work(); + /* Fake some cpu-intensive calculation: */ +#if !defined(WIN32) || defined(__CYGWIN__) + usleep(500 * 1000); +#else + Sleep(500); +#endif } sprintf(tmp, "Done with %s!", name); Gmsh_SendString(s, GMSH_CLIENT_INFO, tmp);