diff options
author | schnetter <schnetter@83718e91-0e4f-0410-abf4-91180603181f> | 2005-05-24 16:36:04 +0000 |
---|---|---|
committer | schnetter <schnetter@83718e91-0e4f-0410-abf4-91180603181f> | 2005-05-24 16:36:04 +0000 |
commit | d99f83734f0ea81db3ad7c465616cb24bde2f4c0 (patch) | |
tree | d002b16e8f1274391ecd420d595f34714a119a50 | |
parent | 28ff3a5f5499b2c4c732dfabbb326a5f91271cd7 (diff) |
Use C++ for the portal code; that's cleaner.
Store the Cactus source tree in the executable and output it into the
output directory.
git-svn-id: http://svn.cactuscode.org/arrangements/CactusUtils/Formaline/trunk@5 83718e91-0e4f-0410-abf4-91180603181f
-rw-r--r-- | src/connection.cc | 130 | ||||
-rw-r--r-- | src/connection.hh | 71 | ||||
-rw-r--r-- | src/formaline.c | 322 | ||||
-rw-r--r-- | src/make.code.defn | 4 | ||||
-rw-r--r-- | src/make.code.deps | 10 | ||||
-rw-r--r-- | src/make.configuration.deps | 35 | ||||
-rw-r--r-- | src/output_source.c | 60 | ||||
-rw-r--r-- | src/portal.cc | 147 | ||||
-rw-r--r-- | src/util/makeblob.c | 77 |
9 files changed, 532 insertions, 324 deletions
diff --git a/src/connection.cc b/src/connection.cc new file mode 100644 index 0000000..e38f43c --- /dev/null +++ b/src/connection.cc @@ -0,0 +1,130 @@ +// $Header$ + +#include <cassert> +#include <string> +#include <sstream> + +#include "cctk_Parameters.h" + +#include "connection.hh" + + + +using std::string; +using std::ostringstream; + + + +connection:: +connection (char const * const id) +{ + DECLARE_CCTK_PARAMETERS; + + sock = socket (PF_INET, SOCK_STREAM, 0); +#warning "TODO: handle errors" + + struct hostent * hostinfo; + hostinfo = gethostbyname (portal_hostname); +#warning "TODO: handle errors" + assert (hostinfo); + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = portal_port; + addr.sin_addr = * (struct in_addr *) hostinfo->h_addr; + + int const ierr = connect (sock, & addr, sizeof addr); +#warning "TODO: handle errors" + assert (! ERROR_CHECK (ierr)); + + ostringstream buf; + buf << "<simulation id=\"" << clean (id) << "\">\n"; + write (buf.str()); +} + + + +connection:: +~ connection () +{ + ostringstream buf; + buf << "</simulation>\n"; + write (buf.str()); + + CLOSESOCKET (sock); +#warning "TODO: handle errors" +} + + + +template<typename T> +void connection:: +store (char const * const key, + T const value) +{ + assert (key); + + ostringstream keybuf; + keybuf << key; + ostringstream valuebuf; + valuebuf << value; + + ostringstream buf; + buf << "<key>" << clean (keybuf.str()) << "</key> " + << "<value>" << clean (valuebuf.str()) << "</value>\n"; + + write (buf.str()); +} + +template +void connection:: +store (char const * key, + int value); + +template +void connection:: +store (char const * key, + char const * value); + +template +void connection:: +store (char const * key, + char * value); + + + +void connection:: +write (string const & msg) +{ + char const * const cmsg = msg.c_str(); + + size_t const len = strlen (cmsg); + ssize_t const nelems = send (sock, cmsg, len, MSG_NOSIGNAL); + assert (! ERROR_CHECK (nelems)); +#warning "TODO: handle errors" +#warning "TODO: handle overflow" + assert (nelems == len); +} + + + +string connection:: +clean (string const & txt) + const +{ + ostringstream buf; + + for (string::const_iterator p = txt.begin(); p != txt.end(); ++ p) + { + switch (* p) + { + case '<': buf << "<langle>"; break; + case '>': buf << "<rangle>"; break; + case '"': buf << "<quote>"; break; + case '\\': buf << "<backslash>"; break; + default: buf << * p; + } + } + + return buf.str(); +} diff --git a/src/connection.hh b/src/connection.hh new file mode 100644 index 0000000..2e8a3d7 --- /dev/null +++ b/src/connection.hh @@ -0,0 +1,71 @@ +// $Header$ + +#include <string> + +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif +#ifdef HAVE_SYS_TIME_H +# include <sys/time.h> +#endif +#ifdef HAVE_SYS_TYPES_H +# include <sys/types.h> +#endif +#ifdef HAVE_SYS_SOCKET_H +# include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +# include <netinet/in.h> +#endif +#ifdef HAVE_NETDB_H +# include <netdb.h> +#endif +#ifdef HAVE_ARPA_INET_H +# include <arpa/inet.h> +#endif +#ifdef HAVE_WINSOCK2_H +# include <winsock2.h> +#endif +#include <errno.h> + +#ifndef SOCKET +# define SOCKET int +#endif + +#ifdef SOCKET_ERROR +# define ERROR_CHECK(a) ((a) == SOCKET_ERROR) +#else +# define ERROR_CHECK(a) ((a) < 0) +#endif + +#ifdef HAVE_WINSOCK2_H +# define CLOSESOCKET(a) closesocket(a) +#else +# define CLOSESOCKET(a) close(a) +#endif + + + +class connection +{ + SOCKET sock; + +public: + connection (char const * id); + + ~ connection (); + + template<typename T> + void + store (char const * const key, + T const value); + +private: + + void + write (std::string const & msg); + + std::string + clean (std::string const & txt) + const; +}; diff --git a/src/formaline.c b/src/formaline.c deleted file mode 100644 index 22a61c5..0000000 --- a/src/formaline.c +++ /dev/null @@ -1,322 +0,0 @@ -/* $Header$ */ - -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "cctk.h" -#include "cctk_Arguments.h" -#include "cctk_Parameters.h" -#include "cctk_Version.h" - -#include "util_Network.h" - -#ifdef HAVE_UNISTD_H -# include <unistd.h> -#endif -#ifdef HAVE_SYS_TIME_H -# include <sys/time.h> -#endif -#ifdef HAVE_SYS_TYPES_H -# include <sys/types.h> -#endif -#ifdef HAVE_SYS_SOCKET_H -# include <sys/socket.h> -#endif -#ifdef HAVE_NETINET_IN_H -# include <netinet/in.h> -#endif -#ifdef HAVE_NETDB_H -# include <netdb.h> -#endif -#ifdef HAVE_ARPA_INET_H -# include <arpa/inet.h> -#endif -#ifdef HAVE_WINSOCK2_H -# include <winsock2.h> -#endif -#include <errno.h> - -#ifndef SOCKET -# define SOCKET int -#endif - -#ifdef SOCKET_ERROR -# define ERROR_CHECK(a) ((a) == SOCKET_ERROR) -#else -# define ERROR_CHECK(a) ((a) < 0) -#endif - -#ifdef HAVE_WINSOCK2_H -# define CLOSESOCKET(a) closesocket(a) -#else -# define CLOSESOCKET(a) close(a) -#endif - -#ifndef MSG_NOSIGNAL -# define MSG_NOSIGNAL 0 -#endif - - - -struct connection; - -static struct connection * -OpenConnection (char const * id); - -static void -CloseConnection (struct connection * conn); - -static void -WriteToConnection (struct connection * conn, - char const * msg); - - - -static void -StoreIntKey (struct connection * conn, - char const * key, - int value); - -static void -StoreStringKey (struct connection *, - char const * key, - char const * value); - - - -void -Formaline (CCTK_ARGUMENTS) -{ - /* Create a unique job id */ - char const * restrict const jobid = "not unique"; - - struct connection * restrict const conn = OpenConnection (jobid); - assert (conn); - - /* Cactus */ - - { - char const * const cactus_version = CCTK_FullVersion(); - StoreStringKey (conn, "Cactus version", cactus_version); - } - - - - /* Compiling */ - - { - char const * const compile_user = CCTK_CompileUser(); - StoreStringKey (conn, "compile user", compile_user); - } - - { - char const * const compile_date = CCTK_CompileDate(); - StoreStringKey (conn, "compile date", compile_date); - } - - { - char const * const compile_time = CCTK_CompileTime(); - StoreStringKey (conn, "compile time", compile_time); - } - - - - /* Running */ - - { - char const * const run_user = CCTK_RunUser(); - StoreStringKey (conn, "run user", run_user); - } - - { - char run_date [1000]; - Util_CurrentDate (sizeof run_date, run_date); - StoreStringKey (conn, "run date", run_date); - } - - { - char run_time [1000]; - Util_CurrentTime (sizeof run_time, run_time); - StoreStringKey (conn, "run time", run_time); - } - - { - char run_host [1000]; - Util_GetHostName (run_host, sizeof run_host); - StoreStringKey (conn, "run host", run_host); - } - - { - char const * run_title; - int type; - run_title = CCTK_ParameterGet ("run_title", "Cactus", & type); - assert (type == CCTK_VARIABLE_STRING); - StoreStringKey (conn, "run title", run_title); - } - - - - /* Parameters */ - - { - char ** argv; - int argc; - int n; - CCTK_CommandLine (& argv); - for (argc = 0; argv [argc]; ++ argc); - StoreIntKey (conn, "argc", argc); - for (n = 0; n < argc; ++ n) - { - char buffer [1000]; - snprintf (buffer, sizeof buffer, "argv[%d]", n); - StoreStringKey (conn, buffer, argv[n]); - } - } - - { - char parameter_filename [10000]; - CCTK_ParameterFilename (sizeof parameter_filename, parameter_filename); - StoreStringKey (conn, "parameter filename", parameter_filename); - } - - { - char parameter_filename [10000]; - char parameter_file [1000000]; - size_t count; - FILE * file; - CCTK_ParameterFilename (sizeof parameter_filename, parameter_filename); - file = fopen (parameter_filename, "r"); - count = fread (parameter_file, 1, sizeof parameter_file - 1, file); - fclose (file); - assert (count < sizeof parameter_file - 1); - parameter_file [count] = '\0'; - StoreStringKey (conn, "parameter file", parameter_file); - } - - { - char const * out_dir; - int type; - out_dir = CCTK_ParameterGet ("out_dir", "IO", & type); - assert (type == CCTK_VARIABLE_STRING); - StoreStringKey (conn, "out dir", out_dir); - } - - { - int nprocs; - nprocs = CCTK_nProcs (cctkGH); - StoreIntKey (conn, "nprocs", nprocs); - } - - CloseConnection (conn); -} - - - -/* - <simulation id="ID"> - <key>key</key> <value>value</value> - </simulation> - */ - - - -struct connection { - SOCKET socket; -}; - -static struct connection * -OpenConnection (char const * restrict const id) -{ - DECLARE_CCTK_PARAMETERS; - struct connection * const conn = malloc (sizeof * conn); -#warning "TODO: handle errors" - assert (conn); - - conn->socket = socket (PF_INET, SOCK_STREAM, 0); -#warning "TODO: handle errors" - - struct hostent * hostinfo; - hostinfo = gethostbyname (portal_hostname); -#warning "TODO: handle errors" - assert (hostinfo); - - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = portal_port; - addr.sin_addr = * (struct in_addr *) hostinfo->h_addr; - - int const ierr = connect (conn->socket, & addr, sizeof addr); -#warning "TODO: handle errors" - assert (! ERROR_CHECK (ierr)); - - char buf[1000]; - snprintf (buf, sizeof buf, "<simulation id=\"%s\">\n", id); - WriteToConnection (conn, buf); - - return conn; -} - -static void -CloseConnection (struct connection * restrict const conn) -{ - assert (conn); - - char buf[1000]; - snprintf (buf, sizeof buf, "</simulation>\n"); - WriteToConnection (conn, buf); - - CLOSESOCKET (conn->socket); -#warning "TODO: handle errors" - - free (conn); -} - -static void -WriteToConnection (struct connection * restrict const conn, - char const * restrict const msg) -{ - assert (conn); - assert (msg); - - size_t const len = strlen (msg); - ssize_t const nelems = send (conn, msg, len, MSG_NOSIGNAL); - assert (! ERROR_CHECK (nelems)); -#warning "TODO: handle errors" -#warning "TODO: handle overflow" - assert (nelems == len); -} - - - -static void -StoreIntKey (struct connection * restrict const conn, - char const * restrict const key, - int const value) -{ - assert (conn); - assert (key); - - char buf[1000]; -#warning "TODO: quote key and value" - snprintf (buf, sizeof buf, "<key>%s</key> <value>%d</value>\n", key, value); -#warning "TODO: handle overflow" - WriteToConnection (conn, buf); -} - -static void -StoreStringKey (struct connection * restrict const conn, - char const * restrict const key, - char const * restrict const value) -{ - assert (conn); - assert (key); - assert (value); - - char buf[1000]; -#warning "TODO: quote key and value" - snprintf (buf, sizeof buf, "<key>%s</key> <value>%s</value>\n", key, value); -#warning "TODO: handle overflow" - WriteToConnection (conn, buf); -} diff --git a/src/make.code.defn b/src/make.code.defn index 7407f5d..57413a2 100644 --- a/src/make.code.defn +++ b/src/make.code.defn @@ -1,8 +1,8 @@ -# Main make.code.defn file for thorn Formaline +# Main make.code.defn file for thorn Formaline -*-Makefile-*- # $Header$ # Source files in this directory -SRCS = formaline.c +SRCS = connection.cc output_source.c portal.cc # Subdirectories containing source files SUBDIRS = diff --git a/src/make.code.deps b/src/make.code.deps new file mode 100644 index 0000000..7eb6705 --- /dev/null +++ b/src/make.code.deps @@ -0,0 +1,10 @@ +# make.code.deps file for thorn Formaline -*-Makefile-*- +# $Header$ + +$(CCTK_TARGET): $(SCRATCH_BUILD)/makeblob + +$(SCRATCH_BUILD)/makeblob: $(SCRATCH_BUILD)/makeblob.o + cd $(SCRATCH_BUILD) && $(LD) $(LDFLAGS) -o $@ $^ + +$(SCRATCH_BUILD)/makeblob.o: $(SRCDIR)/util/makeblob.c + cd $(SCRATCH_BUILD) && $(CC) $(CFLAGS) -c $^ diff --git a/src/make.configuration.deps b/src/make.configuration.deps new file mode 100644 index 0000000..997f9f5 --- /dev/null +++ b/src/make.configuration.deps @@ -0,0 +1,35 @@ +# make.configuration.deps file for thorn Formaline -*-Makefile-*- +# $Header$ + + + +# Create a tarball of the source code whenever this thorn is linked +# into an executable +$(EXEDIR)$(DIRSEP)$(EXE): make-tarball + +CACTUSLIBLINKLINE += $(SCRATCH_BUILD)/cactus-source.a + + + +.PHONY: make-tarball +make-tarball: + @echo "Creating source tarball..." && \ + cd $(CCTK_HOME) && \ + files="CONTRIBUTORS COPYRIGHT Makefile" && \ + dirs="lib src" && \ + thorns="$(THORNS:%=arrangements/%)" && \ + filelist=$(SCRATCH_BUILD)/source-file-list && \ + find $$files $$dirs $$thorns -path 'arrangements/*/*/doc' -prune -o -path 'arrangements/*/*/test' -prune -o -print > $$filelist && \ + echo "Adding" `cat $$filelist | wc -l` "files..." && \ + tar czf "$(SCRATCH_BUILD)/cactus-source.tar.gz" --no-recursion -T $$filelist && \ + echo "Created tarball with" `cat $(SCRATCH_BUILD)/cactus-source.tar.gz | wc -c` "bytes..." && \ + cd $(SCRATCH_BUILD) && \ + rm -rf cactus-source && \ + mkdir cactus-source && \ + cd cactus-source && \ + ../makeblob < ../cactus-source.tar.gz && \ + $(CC) $(CFLAGS) -c *.c && \ + cd .. && \ + $(AR) $(ARFLAGS) cactus-source.a cactus-source/*.o && \ + echo "Added tarball to executable." && \ + echo $(DIVIDER) diff --git a/src/output_source.c b/src/output_source.c new file mode 100644 index 0000000..45bfb6a --- /dev/null +++ b/src/output_source.c @@ -0,0 +1,60 @@ +/* $Header$ */ + +#include <assert.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "cctk.h" +#include "cctk_Arguments.h" +#include "cctk_Parameters.h" + + + +struct chunkinfo +{ + char const * data; + size_t const * length; +}; + +extern struct chunkinfo cactus_source_chunks []; +extern size_t cactus_source_chunks_length; + + + +void +Formaline_OutputSource (CCTK_ARGUMENTS) +{ + DECLARE_CCTK_ARGUMENTS; + DECLARE_CCTK_PARAMETERS; + + char const sourcename [] = "cactus-source.tar.gz"; + size_t filenamelength; + char * filename; + FILE * file; + size_t chunk; + + if (CCTK_MyProc (cctkGH) != 0) return; + + filenamelength = strlen (out_dir) + strlen (sourcename) + 2; + filename = malloc (filenamelength); + assert (filename); + sprintf (filename, "%s/%s", out_dir, sourcename); + + CCTK_VInfo (CCTK_THORNSTRING, + "Writing tarball with Cactus sources to file \"%s\"", filename); + + file = fopen (filename, "w"); + assert (file); + for (chunk = 0; chunk < cactus_source_chunks_length; ++ chunk) + { + fwrite (cactus_source_chunks[chunk].data, + sizeof * cactus_source_chunks[chunk].data, + * cactus_source_chunks[chunk].length, + file); + } + fclose (file); + + free (filename); +} diff --git a/src/portal.cc b/src/portal.cc new file mode 100644 index 0000000..93fed01 --- /dev/null +++ b/src/portal.cc @@ -0,0 +1,147 @@ +// $Header$ + +#include <cassert> +#include <cstdio> +#include <cstdlib> +#include <cstring> + +#include "cctk.h" +#include "cctk_Arguments.h" +#include "cctk_Parameters.h" +#include "cctk_Version.h" +#include "util_Network.h" + +#include "connection.hh" + + + +extern "C" +void +Formaline_Portal (CCTK_ARGUMENTS) +{ + if (CCTK_MyProc (cctkGH) != 0) return; + + /* Create a unique job id */ + char const * restrict const jobid = "not unique"; + + connection conn (jobid); + + + + /* Cactus */ + + { + char const * const cactus_version = CCTK_FullVersion(); + conn.store ("Cactus version", cactus_version); + } + + + + /* Compiling */ + + { + char const * const compile_user = CCTK_CompileUser(); + conn.store ("compile user", compile_user); + } + + { + char const * const compile_date = CCTK_CompileDate(); + conn.store ("compile date", compile_date); + } + + { + char const * const compile_time = CCTK_CompileTime(); + conn.store ("compile time", compile_time); + } + + + + /* Running */ + + { + char const * const run_user = CCTK_RunUser(); + conn.store ("run user", run_user); + } + + { + char run_date [1000]; + Util_CurrentDate (sizeof run_date, run_date); + conn.store ("run date", run_date); + } + + { + char run_time [1000]; + Util_CurrentTime (sizeof run_time, run_time); + conn.store ("run time", run_time); + } + + { + char run_host [1000]; + Util_GetHostName (run_host, sizeof run_host); + conn.store ("run host", run_host); + } + + { + int type; + void const * const ptr + = CCTK_ParameterGet ("run_title", "Cactus", & type); + assert (type == CCTK_VARIABLE_STRING); + char const * const run_title = static_cast<char const *> (ptr); + conn.store ("run title", run_title); + } + + + + /* Parameters */ + + { + char ** argv; + int argc; + int n; + CCTK_CommandLine (& argv); + for (argc = 0; argv [argc]; ++ argc); + conn.store ("argc", argc); + for (n = 0; n < argc; ++ n) + { + char buffer [1000]; + snprintf (buffer, sizeof buffer, "argv[%d]", n); + conn.store (buffer, argv[n]); + } + } + + { + char parameter_filename [10000]; + CCTK_ParameterFilename (sizeof parameter_filename, parameter_filename); + conn.store ("parameter filename", parameter_filename); + } + + { + char parameter_filename [10000]; + char parameter_file [1000000]; + size_t count; + FILE * file; + CCTK_ParameterFilename (sizeof parameter_filename, parameter_filename); + file = fopen (parameter_filename, "r"); + count = fread (parameter_file, 1, sizeof parameter_file - 1, file); + fclose (file); + assert (count < sizeof parameter_file - 1); + parameter_file [count] = '\0'; + conn.store ("parameter file", parameter_file); + } + + { + int type; + void const * const ptr + = CCTK_ParameterGet ("out_dir", "IO", & type); + assert (type == CCTK_VARIABLE_STRING); + char const * const out_dir = static_cast<char const *> (ptr); + conn.store ("out dir", out_dir); + } + + { + int nprocs; + nprocs = CCTK_nProcs (cctkGH); + conn.store ("nprocs", nprocs); + } + +} diff --git a/src/util/makeblob.c b/src/util/makeblob.c new file mode 100644 index 0000000..60de456 --- /dev/null +++ b/src/util/makeblob.c @@ -0,0 +1,77 @@ +/* $Header$ */ + +#include <stdio.h> + +int +main (int argc, char * * argv) +{ + FILE * metafile; + unsigned long metacount; + char filename [100]; + FILE * file; + unsigned long count; + + for (metacount = 0; ; ++ metacount) + { + sprintf (filename, "cactus-source-%08lu.c", metacount); + file = fopen (filename, "w"); + fprintf (file, "/* This is an auto-generated file -- do not edit */\n"); + fprintf (file, "#include <stddef.h>\n"); + fprintf (file, "\n"); + fprintf (file, "char const cactus_source_%08lu [] = {", metacount); + for (count = 0; count < 1000000; ++ count) + { + int const ch = getc (stdin); + if (feof (stdin)) break; + if (ferror (stdin)) return 1; + if (count != 0) { + fprintf (file, ","); + } + if (count % 16 == 0) + { + fprintf (file, "\n"); + } + fprintf (file, "%3d", ch); + } + fprintf (file, "\n"); + fprintf (file, "};\n"); + fprintf (file, "size_t const cactus_source_length_%08lu = %lu;\n", metacount, count); + fclose (file); + + if (feof (stdin)) break; + } + + + + metafile = fopen ("cactus-source.c", "w"); + fprintf (metafile, "/* This is an auto-generated file -- do not edit */\n"); + fprintf (metafile, "#include <stddef.h>\n"); + fprintf (metafile, "\n"); + fprintf (metafile, "struct chunkinfo\n"); + fprintf (metafile, "{\n"); + fprintf (metafile, " char const * data;\n"); + fprintf (metafile, " size_t * length;\n"); + fprintf (metafile, "};\n"); + fprintf (metafile, "\n"); + for (count = 0; count <= metacount; ++ count) + { + fprintf (metafile, "extern char const cactus_source_%08lu [];\n", count); + fprintf (metafile, "extern size_t cactus_source_length_%08lu [];\n", count); + } + fprintf (metafile, "\n"); + fprintf (metafile, "struct chunkinfo cactus_source_chunks [] = {"); + for (count = 0; count < metacount; ++ count) + { + if (count != 0) { + fprintf (metafile, ","); + } + fprintf (metafile, "\n"); + fprintf (metafile, "{ cactus_source_%08lu, & cactus_source_length_%08lu }", count, count); + } + fprintf (metafile, "\n"); + fprintf (metafile, "};\n"); + fprintf (metafile, "size_t cactus_source_chunks_length = %lu;\n", metacount + 1); + fclose (metafile); + + return 0; +} |