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

- usleep takes microseconds
- work on c solver interf
parent 15edad9f
No related branches found
No related tags found
No related merge requests found
// $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
......@@ -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
// $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;
}
......
#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:
......
/* $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
}
......@@ -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
/* $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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment