diff --git a/utils/solvers/c++/GmshClient.h b/utils/solvers/c++/GmshClient.h new file mode 100644 index 0000000000000000000000000000000000000000..3ae42dc05288e3cfae0f4b90abd8a4e77c89c66d --- /dev/null +++ b/utils/solvers/c++/GmshClient.h @@ -0,0 +1,197 @@ +#ifndef _GMSH_CLIENT_H_ +#define _GMSH_CLIENT_H_ + +/* + * Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished + * to do so, provided that the above copyright notice(s) and this + * permission notice appear in all copies of the Software and that + * both the above copyright notice(s) and this permission notice + * appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR + * ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY + * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + * + * Please report all bugs and problems to <gmsh@geuz.org>. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#ifdef _AIX +#include <strings.h> +#endif +#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> + +class GmshClient { + private: + int _sock; + void _SendData(int socket, void *buffer, int bytes) + { + int sofar, remaining, len; + char *buf; + buf = (char *)buffer; + sofar = 0; + remaining = bytes; + do { + len = write(socket, buf + sofar, remaining); + sofar += len; + remaining -= len; + } while(remaining > 0); + } + void _SendString(int socket, int type, char str[]) + { + int len = strlen(str); + _SendData(socket, &type, sizeof(int)); + _SendData(socket, &len, sizeof(int)); + _SendData(socket, str, len); + } + long _GetTime() + { + struct timeval tp; + gettimeofday(&tp, (struct timezone *)0); + return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec; + } + void _Idle(double delay) + { + long t1 = _GetTime(); + while(1) { + if(_GetTime() - t1 > 1.e6 * delay) + break; + } + } + public: + GmshClient() : _sock(0) {} + ~GmshClient(){} + int Connect(char *sockname) + { + struct sockaddr_un addr_un; + struct sockaddr_in addr_in; + int len, sock; + int tries; + struct hostent *server; + int portno, remotelen; + char remote[256], *port; + + /* slight delay to be sure that the socket is bound by the + server before we attempt to connect to it... */ + _Idle(0.1); + + if(strstr(sockname, "/") || strstr(sockname, "\\") || !strstr(sockname, ":")){ + /* UNIX socket (testing ":" is not enough with Windows paths) */ + portno = -1; + } + else{ + /* INET socket */ + port = strstr(sockname, ":"); + portno = atoi(port+1); + remotelen = strlen(sockname) - strlen(port); + if(remotelen > 0) + strncpy(remote, sockname, remotelen); + remote[remotelen] = '\0'; + } + + /* create socket */ + + if(portno < 0){ + sock = socket(PF_UNIX, SOCK_STREAM, 0); + if(sock < 0) + return -1; /* Error: Couldn't create socket */ + /* try to connect socket to given name */ + strcpy(addr_un.sun_path, sockname); + addr_un.sun_family = AF_UNIX; + len = strlen(addr_un.sun_path) + sizeof(addr_un.sun_family); + for(tries = 0; tries < 5; tries++) { + if(connect(sock, (struct sockaddr *)&addr_un, len) >= 0){ + _sock = sock; + return sock; + } + _Idle(0.1); + } + } + else{ + /* try to connect socket to given name */ + sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock < 0) + return -1; /* Error: Couldn't create socket */ + if(!(server = gethostbyname(remote))) + return -3; /* Error: No such host */ + memset((char *) &addr_in, 0, sizeof(addr_in)); + 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){ + _sock = sock; + return sock; + } + _Idle(0.1); + } + } + return -2; /* Error: Couldn't connect */ + } + void Start(int pid) + { + char tmp[256]; + sprintf(tmp, "%d", getpid()); + _SendString(_sock, 1, tmp); + } + void Stop() + { + _SendString(_sock, 2, "Goodbye!"); + } + void Info(char *str) + { + _SendString(_sock, 10, str); + } + void Warning(char *str) + { + _SendString(_sock, 11, str); + } + void Error(char *str) + { + _SendString(_sock, 12, str); + } + void Progress(char *str) + { + _SendString(_sock, 13, str); + } + void View(char *str) + { + _SendString(_sock, 20, str); + } + void Option(int num, char *str) + { + if(num < 1) num = 1; + if(num > 5) num = 5; + _SendString(_sock, 100 + num - 1, str); + } + void Disconnect() + { + close(_sock); + } +}; + +#endif diff --git a/utils/solvers/c++/Makefile b/utils/solvers/c++/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..54f0ccd27cecc7adbafa4f1483f4aba891634c8f --- /dev/null +++ b/utils/solvers/c++/Makefile @@ -0,0 +1,31 @@ +# $Id: Makefile,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ +# +# Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA. +# +# Please report all bugs and problems to <gmsh@geuz.org>. + +include ../../../variables + +andre: andre.cpp + ${CXX} ${OPTIM} -o andre.exe andre.cpp + +clean: + rm -f *.o *.exe *.pos + +depend: + true diff --git a/utils/solvers/c++/andre.cpp b/utils/solvers/c++/andre.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ab24cc41476ae925c0ab79d80f354addc82e998 --- /dev/null +++ b/utils/solvers/c++/andre.cpp @@ -0,0 +1,95 @@ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/time.h> +#include <unistd.h> +#include <math.h> +#include "GmshClient.h" + +typedef enum { send_options, run_code } action; + +int main(int argc, char *argv[]) +{ + action what_to_do = run_code; + char *name = NULL, *option = NULL, *socket = NULL; + + // parse command line + + int i = 0; + while(i < argc) { + if(argv[i][0] == '-') { + if(!strcmp(argv[i] + 1, "socket")) { + i++; + if(argv[i]) socket = argv[i++]; + } + else if(!strcmp(argv[i] + 1, "options")) { + i++; + what_to_do = send_options; + } + else if(!strcmp(argv[i] + 1, "run")) { + i++; + what_to_do = run_code; + if(argv[i]) option = argv[i++]; + } + } + else + name = argv[i++]; + } + + if(!socket) { + printf("No socket specified: running non-interactively...\n"); + exit(1); + } + + // connect to Gmsh + + GmshClient client; + if(client.Connect(socket) < 0){ + printf("Unable to connect to server\n"); + exit(1); + } + else{ + client.Start(getpid()); + if(what_to_do == send_options) { + // send the available options for this computation + client.Option(1, "FormulationH"); + client.Option(1, "\"Convergence Test\""); + client.Option(1, "\"Blabla blblablabli\""); + } + else if(what_to_do == run_code){ + // do the computation and merge some views + for(int i = 0; i < 10; i++){ + client.Info("Computing curve..."); + sleep(1); + client.Info("Done computing curve"); + FILE *file = fopen("andre.pos", "w"); + if(!file) + client.Error("Unable to open output file"); + else { + fprintf(file, "General.GraphicsWidth = 500;\n"); + fprintf(file, "General.GraphicsHeight = 450;\n"); + fprintf(file, "General.SmallAxes = 0;\n"); + fprintf(file, "View.Type = 2;\n"); + fprintf(file, "View.AutoPosition = 0;\n"); + fprintf(file, "View.PositionX = 100;\n"); + fprintf(file, "View.PositionY = 50;\n"); + fprintf(file, "View.Width = 350;\n"); + fprintf(file, "View.Height = 350;\n"); + fprintf(file, "Delete View[0];\n"); + fprintf(file, "View \"test\"{\n"); + for(int j = 0; j < 100; j++) + fprintf(file, "SP(%d,0,0){%g};\n", j,sin(j*i*M_PI/10.)); + fprintf(file, "};\n"); + fclose(file); + client.View("andre.pos"); + } + } + client.Info("Done!"); + } + + client.Stop(); + client.Disconnect(); + } +} diff --git a/utils/solvers/c++/andre.opt b/utils/solvers/c++/andre.opt new file mode 100644 index 0000000000000000000000000000000000000000..399b6b8eea5850322de7617af61b79723169e596 --- /dev/null +++ b/utils/solvers/c++/andre.opt @@ -0,0 +1,10 @@ +Solver.Name1 = "Solveur Andre"; +Solver.Help1 = "Mon petit solveur"; +Solver.Executable1 = "./andre.exe"; +Solver.OptionCommand1 = "-options"; +Solver.FirstOption1 = "Mes options"; +Solver.FirstButton1 = "Run !"; +Solver.FirstButtonCommand1 = "-run %s"; +Solver.ClientServer1 = 1; +Solver.MergeViews1 = 1; +Solver.PopupMessages1 = 1; diff --git a/utils/solvers/GmshClient.c b/utils/solvers/c/GmshClient.c similarity index 95% rename from utils/solvers/GmshClient.c rename to utils/solvers/c/GmshClient.c index e9bf922a7c84cda881ba5c77e188b0a22f4f9229..129e0b0646e80ae73c43becf5ab2363a22624063 100644 --- a/utils/solvers/GmshClient.c +++ b/utils/solvers/c/GmshClient.c @@ -1,4 +1,4 @@ -/* $Id: GmshClient.c,v 1.10 2005-01-01 18:57:19 geuzaine Exp $ */ +/* $Id: GmshClient.c,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ */ /* * Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle * @@ -130,9 +130,9 @@ int Gmsh_Connect(char *sockname) return -1; /* Error: Couldn't create socket */ if(!(server = gethostbyname(remote))) return -3; /* Error: No such host */ - bzero((char *) &addr_in, sizeof(addr_in)); + memset((char *) &addr_in, 0, sizeof(addr_in)); addr_in.sin_family = AF_INET; - bcopy((char *)server->h_addr, (char *)&addr_in.sin_addr.s_addr, server->h_length); + 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++) { diff --git a/utils/solvers/GmshClient.h b/utils/solvers/c/GmshClient.h similarity index 100% rename from utils/solvers/GmshClient.h rename to utils/solvers/c/GmshClient.h diff --git a/utils/solvers/Makefile b/utils/solvers/c/Makefile similarity index 78% rename from utils/solvers/Makefile rename to utils/solvers/c/Makefile index 7cde99e0d363371f8fb8f51520c44c7dc150835c..c7140df2718e8c54bdc005f7b382679e015566b0 100644 --- a/utils/solvers/Makefile +++ b/utils/solvers/c/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.5 2005-01-01 19:35:41 geuzaine Exp $ +# $Id: Makefile,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ # # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle # @@ -19,17 +19,13 @@ # # Please report all bugs and problems to <gmsh@geuz.org>. -include ../../variables +include ../../../variables mysolver: mysolver.c GmshClient.c ${CXX} ${OPTIM} -o mysolver.exe mysolver.c GmshClient.c -tgz: - tar zcvf mysolver.tgz mysolver.c mysolver.opt GmshClient.c GmshClient.h - tar zcvf myperlsolver.tgz myperlsolver.pl myperlsolver.opt GMSH_CLIENT.pm - clean: - rm -f *.o *.exe + rm -f *.o *.exe *.pos depend: true diff --git a/utils/solvers/mysolver.c b/utils/solvers/c/mysolver.c similarity index 99% rename from utils/solvers/mysolver.c rename to utils/solvers/c/mysolver.c index 0276385e2980468a0e760e9044bfbdaee4618474..f301cb10b04fa8ea957c3560f08674e86f3e3bbf 100644 --- a/utils/solvers/mysolver.c +++ b/utils/solvers/c/mysolver.c @@ -1,4 +1,4 @@ -/* $Id: mysolver.c,v 1.5 2004-05-22 01:24:19 geuzaine Exp $ */ +/* $Id: mysolver.c,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ */ /* * Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle * diff --git a/utils/solvers/mysolver.opt b/utils/solvers/c/mysolver.opt similarity index 100% rename from utils/solvers/mysolver.opt rename to utils/solvers/c/mysolver.opt diff --git a/utils/solvers/GMSH_CLIENT.pm b/utils/solvers/perl/GMSH_CLIENT.pm old mode 100644 new mode 100755 similarity index 95% rename from utils/solvers/GMSH_CLIENT.pm rename to utils/solvers/perl/GMSH_CLIENT.pm index ad2e2d566a2a0119769e5ab8b7ed11fb2d52bd3e..0d8eae44987167929057d9134b3728ffb647d618 --- a/utils/solvers/GMSH_CLIENT.pm +++ b/utils/solvers/perl/GMSH_CLIENT.pm @@ -1,4 +1,4 @@ -# $Id: GMSH_CLIENT.pm,v 1.1 2003-05-09 16:30:01 geuzaine Exp $ +# $Id: GMSH_CLIENT.pm,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ # # Copyright (c) 2002 Laurent CHAMPANEY <laurent.champaney@meca.uvsq.fr>. # All rights reserved. diff --git a/utils/solvers/myperlsolver.opt b/utils/solvers/perl/myperlsolver.opt old mode 100644 new mode 100755 similarity index 100% rename from utils/solvers/myperlsolver.opt rename to utils/solvers/perl/myperlsolver.opt diff --git a/utils/solvers/myperlsolver.pl b/utils/solvers/perl/myperlsolver.pl similarity index 98% rename from utils/solvers/myperlsolver.pl rename to utils/solvers/perl/myperlsolver.pl index 5098501c417e434ee832e7db23aa1ff70158054f..0185fb27e4cae1ffc39f69e92459abac367a37be 100755 --- a/utils/solvers/myperlsolver.pl +++ b/utils/solvers/perl/myperlsolver.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# $Id: myperlsolver.pl,v 1.1 2003-05-09 16:30:01 geuzaine Exp $ +# $Id: myperlsolver.pl,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ # # Copyright (c) 2002 Laurent CHAMPANEY <laurent.champaney@meca.uvsq.fr>. #