[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[certi-cvs] certi libRTI/RTIambassador.cc libRTI/RTIambPriv...
From: |
certi-cvs |
Subject: |
[certi-cvs] certi libRTI/RTIambassador.cc libRTI/RTIambPriv... |
Date: |
Mon, 14 Sep 2009 20:51:52 +0000 |
CVSROOT: /sources/certi
Module name: certi
Changes by: Eric NOULARD <erk> 09/09/14 20:51:52
Modified files:
libRTI : RTIambassador.cc RTIambPrivateRefs.hh
RTIA : RTIA.cc RTIA.hh RTIA_cmdline.c main.cc RTIA.ggo
RTIA_cmdline.h Communications.hh
Communications.cc
libCERTI : SocketUN.hh SocketUN.cc SecureTCPSocket.cc
CMakeLists.txt
. : CMakeLists.txt
Added files:
libCERTI : socketpair_win32.c
Log message:
Merge in #patch #6893: avoid racy sleep to establish connection to rtia
from Mathias Frohlich.
This is the 3rd ans last patch which is merged-in.
Nice work.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/certi/libRTI/RTIambassador.cc?cvsroot=certi&r1=3.105&r2=3.106
http://cvs.savannah.gnu.org/viewcvs/certi/libRTI/RTIambPrivateRefs.hh?cvsroot=certi&r1=3.8&r2=3.9
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/RTIA.cc?cvsroot=certi&r1=3.25&r2=3.26
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/RTIA.hh?cvsroot=certi&r1=3.15&r2=3.16
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/RTIA_cmdline.c?cvsroot=certi&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/main.cc?cvsroot=certi&r1=3.25&r2=3.26
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/RTIA.ggo?cvsroot=certi&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/RTIA_cmdline.h?cvsroot=certi&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/Communications.hh?cvsroot=certi&r1=3.17&r2=3.18
http://cvs.savannah.gnu.org/viewcvs/certi/RTIA/Communications.cc?cvsroot=certi&r1=3.33&r2=3.34
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/SocketUN.hh?cvsroot=certi&r1=3.17&r2=3.18
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/SocketUN.cc?cvsroot=certi&r1=3.29&r2=3.30
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/SecureTCPSocket.cc?cvsroot=certi&r1=3.11&r2=3.12
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/CMakeLists.txt?cvsroot=certi&r1=1.25&r2=1.26
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/socketpair_win32.c?cvsroot=certi&rev=3.1
http://cvs.savannah.gnu.org/viewcvs/certi/CMakeLists.txt?cvsroot=certi&r1=1.69&r2=1.70
Patches:
Index: libRTI/RTIambassador.cc
===================================================================
RCS file: /sources/certi/certi/libRTI/RTIambassador.cc,v
retrieving revision 3.105
retrieving revision 3.106
diff -u -b -r3.105 -r3.106
--- libRTI/RTIambassador.cc 31 Aug 2009 13:25:45 -0000 3.105
+++ libRTI/RTIambassador.cc 14 Sep 2009 20:51:51 -0000 3.106
@@ -19,7 +19,7 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
-// $Id: RTIambassador.cc,v 3.105 2009/08/31 13:25:45 gotthardp Exp $
+// $Id: RTIambassador.cc,v 3.106 2009/09/14 20:51:51 erk Exp $
// ----------------------------------------------------------------------------
#include "RTI.hh"
@@ -131,6 +131,20 @@
if (rtiaenv) rtiacall = rtiaenv ;
else rtiacall = rtiaexec ;
+#if defined(RTIA_USE_TCP)
+ int port = privateRefs->socketUn->listenUN();
+ if (port == -1) {
+ D.Out( pdError, "Cannot listen to RTIA connection. Abort." );
+ throw RTI::RTIinternalError( "Cannot listen to RTIA connection" );
+ }
+#else
+ int pipeFd = privateRefs->socketUn->socketpair();
+ if (pipeFd == -1) {
+ D.Out( pdError, "Cannot get socketpair to RTIA connection. Abort." );
+ throw RTI::RTIinternalError( "Cannot get socketpair to RTIA connection"
);
+ }
+#endif
+
#ifdef _WIN32
STARTUPINFO si;
PROCESS_INFORMATION pi;
@@ -139,12 +153,31 @@
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
+ std::stringstream stream;
+#if defined(RTIA_USE_TCP)
+ stream << rtiacall << " -p " << port;
+#else
+ SOCKET newPipeFd;
+ if (!DuplicateHandle(GetCurrentProcess(),
+ (HANDLE)pipeFd,
+ GetCurrentProcess(),
+ (HANDLE*)&newPipeFd,
+ 0,
+ TRUE, // Inheritable
+ DUPLICATE_SAME_ACCESS)) {
+ D.Out( pdError, "Cannot duplicate socket for RTIA connection. Abort." );
+ throw RTI::RTIinternalError( "Cannot duplicate socket for RTIA connection.
Abort." );
+ }
+
+ stream << rtiacall << " -f " << newPipeFd;
+#endif
+
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
- (char*)rtiacall, // Command line.
+ (char*)stream.str().c_str(), // Command line.
NULL, // Process handle not
inheritable.
NULL, // Thread handle not
inheritable.
- FALSE, // Set handle inheritance to
FALSE.
+ TRUE, // Set handle inheritance to
TRUE.
0, // No creation flags.
NULL, // Use parent's environment
block.
NULL, // Use parent's starting
directory.
@@ -162,9 +195,13 @@
privateRefs->handle_RTIA = pi.hProcess;
privateRefs->pid_RTIA = pi.dwProcessId;
- sleep(1);
- privateRefs->socketUn->connectUN(privateRefs->pid_RTIA);
+#if !defined(RTIA_USE_TCP)
+ closesocket(pipeFd);
+ closesocket(newPipeFd);
+#endif
+
#else
+
sigset_t nset, oset;
// temporarily block termination signals
// note: this is to prevent child processes from receiving termination
signals
@@ -175,11 +212,35 @@
switch((privateRefs->pid_RTIA = fork())) {
case -1: // fork failed.
perror("fork");
+ // unbock the above blocked signals
+ sigprocmask(SIG_SETMASK, &oset, NULL);
+#if !defined(RTIA_USE_TCP)
+ close(pipeFd);
+#endif
throw RTI::RTIinternalError("fork failed in RTIambassador
constructor");
break ;
case 0: // child process (RTIA).
- execlp(rtiacall, NULL);
+ // close all open filedescriptors except the pipe one
+ for (int fdmax = sysconf(_SC_OPEN_MAX), fd = 3; fd < fdmax; ++fd) {
+#if !defined(RTIA_USE_TCP)
+ if (fd == pipeFd)
+ continue;
+#endif
+ close(fd);
+ }
+ {
+ std::stringstream stream;
+#if defined(RTIA_USE_TCP)
+ stream << port;
+ execlp(rtiacall, rtiacall, "-p", stream.str().c_str(), NULL);
+#else
+ stream << pipeFd;
+ execlp(rtiacall, rtiacall, "-f", stream.str().c_str(), NULL);
+#endif
+ }
+ // unbock the above blocked signals
+ sigprocmask(SIG_SETMASK, &oset, NULL);
msg << "Could not launch RTIA process (execlp): "
<< strerror(errno)
<< endl
@@ -187,25 +248,26 @@
throw RTI::RTIinternalError(msg.str().c_str());
default: // father process (Federe).
- // We sleep before trying to connect to the socket
- // our child RTIA process should have open
- // FIXME EN: this is poorly designed because
- // we don't know if the child ever get a chance
- // to be schedule by the Operating System
- sleep(1);
-
- if( privateRefs->socketUn->connectUN(privateRefs->pid_RTIA) )
- {
- D.Out( pdError, "Cannot connect to RTIA. Abort." ) ;
- kill( privateRefs->pid_RTIA, SIGINT ) ;
- throw RTI::RTIinternalError( "Cannot connect to RTIA" );
- };
+ // unbock the above blocked signals
+ sigprocmask(SIG_SETMASK, &oset, NULL);
+#if !defined(RTIA_USE_TCP)
+ close(pipeFd);
+#endif
break ;
}
+#endif
- // unbock the above blocked signals
- sigprocmask(SIG_SETMASK, &oset, NULL);
+#if defined(RTIA_USE_TCP)
+ if (privateRefs->socketUn->acceptUN(10*1000) == -1) {
+#ifdef WIN32
+ TerminateProcess(privateRefs->pid_RTIA, 0);
+#else
+ kill( privateRefs->pid_RTIA, SIGINT );
#endif
+ throw RTI::RTIinternalError( "Cannot connect to RTIA" );
+ }
+#endif
+
G.Out(pdGendoc,"exit RTIambassador::RTIambassador");
}
@@ -2955,4 +3017,4 @@
privateRefs->executeService(&req, &rep);
}
-// $Id: RTIambassador.cc,v 3.105 2009/08/31 13:25:45 gotthardp Exp $
+// $Id: RTIambassador.cc,v 3.106 2009/09/14 20:51:51 erk Exp $
Index: libRTI/RTIambPrivateRefs.hh
===================================================================
RCS file: /sources/certi/certi/libRTI/RTIambPrivateRefs.hh,v
retrieving revision 3.8
retrieving revision 3.9
diff -u -b -r3.8 -r3.9
--- libRTI/RTIambPrivateRefs.hh 2 Apr 2009 19:58:07 -0000 3.8
+++ libRTI/RTIambPrivateRefs.hh 14 Sep 2009 20:51:51 -0000 3.9
@@ -19,7 +19,7 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
-// $Id: RTIambPrivateRefs.hh,v 3.8 2009/04/02 19:58:07 erk Exp $
+// $Id: RTIambPrivateRefs.hh,v 3.9 2009/09/14 20:51:51 erk Exp $
// ----------------------------------------------------------------------------
#include "RTI.hh"
@@ -60,4 +60,4 @@
MessageBuffer msgBufSend,msgBufReceive ;
};
-// $Id: RTIambPrivateRefs.hh,v 3.8 2009/04/02 19:58:07 erk Exp $
+// $Id: RTIambPrivateRefs.hh,v 3.9 2009/09/14 20:51:51 erk Exp $
Index: RTIA/RTIA.cc
===================================================================
RCS file: /sources/certi/certi/RTIA/RTIA.cc,v
retrieving revision 3.25
retrieving revision 3.26
diff -u -b -r3.25 -r3.26
--- RTIA/RTIA.cc 8 Apr 2009 10:47:17 -0000 3.25
+++ RTIA/RTIA.cc 14 Sep 2009 20:51:51 -0000 3.26
@@ -18,7 +18,7 @@
// along with this program ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// $Id: RTIA.cc,v 3.25 2009/04/08 10:47:17 approx Exp $
+// $Id: RTIA.cc,v 3.26 2009/09/14 20:51:51 erk Exp $
// ----------------------------------------------------------------------------
#include <config.h>
@@ -39,12 +39,12 @@
// ----------------------------------------------------------------------------
//! RTIA constructor.
-RTIA::RTIA(int RTIA_port)
+RTIA::RTIA(int RTIA_port, int RTIA_fd)
{
// No SocketServer is passed to the RootObject.
rootObject = new RootObject(NULL);
- comm = new Communications(RTIA_port);
+ comm = new Communications(RTIA_port, RTIA_fd);
queues = new Queues ;
fm = new FederationManagement(comm,&stat);
om = new ObjectManagement(comm, fm, rootObject);
@@ -202,4 +202,4 @@
}} // namespace certi/rtia
-// $Id: RTIA.cc,v 3.25 2009/04/08 10:47:17 approx Exp $
+// $Id: RTIA.cc,v 3.26 2009/09/14 20:51:51 erk Exp $
Index: RTIA/RTIA.hh
===================================================================
RCS file: /sources/certi/certi/RTIA/RTIA.hh,v
retrieving revision 3.15
retrieving revision 3.16
diff -u -b -r3.15 -r3.16
--- RTIA/RTIA.hh 10 Oct 2008 14:04:48 -0000 3.15
+++ RTIA/RTIA.hh 14 Sep 2009 20:51:51 -0000 3.16
@@ -18,7 +18,7 @@
// along with this program ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// $Id: RTIA.hh,v 3.15 2008/10/10 14:04:48 gotthardp Exp $
+// $Id: RTIA.hh,v 3.16 2009/09/14 20:51:51 erk Exp $
// ----------------------------------------------------------------------------
#ifndef _CERTI_RTIA_HH
@@ -40,7 +40,7 @@
class RTIA
{
public:
- RTIA(int RTIA_port);
+ RTIA(int RTIA_port, int RTIA_fd);
~RTIA();
void execute();
@@ -72,4 +72,4 @@
#endif // _CERTI_RTIA_HH
-// $Id: RTIA.hh,v 3.15 2008/10/10 14:04:48 gotthardp Exp $
+// $Id: RTIA.hh,v 3.16 2009/09/14 20:51:51 erk Exp $
Index: RTIA/RTIA_cmdline.c
===================================================================
RCS file: /sources/certi/certi/RTIA/RTIA_cmdline.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- RTIA/RTIA_cmdline.c 30 Oct 2008 10:20:06 -0000 1.3
+++ RTIA/RTIA_cmdline.c 14 Sep 2009 20:51:51 -0000 1.4
@@ -1,5 +1,5 @@
/*
- File autogenerated by gengetopt version 2.21
+ File autogenerated by gengetopt version 2.22.1
generated with the following command:
gengetopt -i RTIA.ggo -F RTIA_cmdline -G
@@ -29,11 +29,17 @@
const char *gengetopt_args_info_help[] = {
" -h, --help Print help and exit",
" -V, --version Print version and exit",
+ " -f, --fd=INT file descriptor number to be used to communicate
with \n FederateAmbassador",
" -p, --port=INT tcp port to be used to communicate with \n
FederateAmbassador",
" -v, --verbose=SHORT verbose mode",
0
};
+typedef enum {ARG_NO
+ , ARG_INT
+ , ARG_SHORT
+} cmdline_parser_arg_type;
+
static
void clear_given (struct gengetopt_args_info *args_info);
static
@@ -52,6 +58,7 @@
{
args_info->help_given = 0 ;
args_info->version_given = 0 ;
+ args_info->fd_given = 0 ;
args_info->port_given = 0 ;
args_info->verbose_given = 0 ;
}
@@ -59,6 +66,7 @@
static
void clear_args (struct gengetopt_args_info *args_info)
{
+ args_info->fd_orig = NULL;
args_info->port_orig = NULL;
args_info->verbose_orig = NULL;
@@ -67,10 +75,13 @@
static
void init_args_info(struct gengetopt_args_info *args_info)
{
+
+
args_info->help_help = gengetopt_args_info_help[0] ;
args_info->version_help = gengetopt_args_info_help[1] ;
- args_info->port_help = gengetopt_args_info_help[2] ;
- args_info->verbose_help = gengetopt_args_info_help[3] ;
+ args_info->fd_help = gengetopt_args_info_help[2] ;
+ args_info->port_help = gengetopt_args_info_help[3] ;
+ args_info->verbose_help = gengetopt_args_info_help[4] ;
}
@@ -80,20 +91,26 @@
printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}
-void
-cmdline_parser_print_help (void)
-{
- int i = 0;
+static void print_help_common(void) {
cmdline_parser_print_version ();
if (strlen(gengetopt_args_info_purpose) > 0)
printf("\n%s\n", gengetopt_args_info_purpose);
- printf("\n%s\n\n", gengetopt_args_info_usage);
+ if (strlen(gengetopt_args_info_usage) > 0)
+ printf("\n%s\n", gengetopt_args_info_usage);
+
+ printf("\n");
if (strlen(gengetopt_args_info_description) > 0)
- printf("%s\n", gengetopt_args_info_description);
+ printf("%s\n\n", gengetopt_args_info_description);
+}
+void
+cmdline_parser_print_help (void)
+{
+ int i = 0;
+ print_help_common();
while (gengetopt_args_info_help[i])
printf("%s\n", gengetopt_args_info_help[i++]);
}
@@ -106,39 +123,89 @@
init_args_info (args_info);
}
-struct cmdline_parser_params *
-cmdline_parser_params_init()
+void
+cmdline_parser_params_init(struct cmdline_parser_params *params)
{
- struct cmdline_parser_params *params =
- (struct cmdline_parser_params *)malloc(sizeof(struct
cmdline_parser_params));
-
if (params)
{
params->override = 0;
- params->initialize = 0;
- params->check_required = 0;
+ params->initialize = 1;
+ params->check_required = 1;
params->check_ambiguity = 0;
+ params->print_errors = 1;
}
+}
+struct cmdline_parser_params *
+cmdline_parser_params_create(void)
+{
+ struct cmdline_parser_params *params =
+ (struct cmdline_parser_params *)malloc(sizeof(struct
cmdline_parser_params));
+ cmdline_parser_params_init(params);
return params;
}
static void
+free_string_field (char **s)
+{
+ if (*s)
+ {
+ free (*s);
+ *s = 0;
+ }
+}
+
+
+static void
cmdline_parser_release (struct gengetopt_args_info *args_info)
{
- if (args_info->port_orig)
- {
- free (args_info->port_orig); /* free previous argument */
- args_info->port_orig = 0;
+ free_string_field (&(args_info->fd_orig));
+ free_string_field (&(args_info->port_orig));
+ free_string_field (&(args_info->verbose_orig));
+
+
+
+ clear_given (args_info);
+}
+
+
+static void
+write_into_file(FILE *outfile, const char *opt, const char *arg, char
*values[])
+{
+ if (arg) {
+ fprintf(outfile, "%s=\"%s\"\n", opt, arg);
+ } else {
+ fprintf(outfile, "%s\n", opt);
}
- if (args_info->verbose_orig)
+}
+
+
+int
+cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
+{
+ int i = 0;
+
+ if (!outfile)
{
- free (args_info->verbose_orig); /* free previous argument */
- args_info->verbose_orig = 0;
+ fprintf (stderr, "%s: cannot dump options to stream\n",
CMDLINE_PARSER_PACKAGE);
+ return EXIT_FAILURE;
}
- clear_given (args_info);
+ if (args_info->help_given)
+ write_into_file(outfile, "help", 0, 0 );
+ if (args_info->version_given)
+ write_into_file(outfile, "version", 0, 0 );
+ if (args_info->fd_given)
+ write_into_file(outfile, "fd", args_info->fd_orig, 0);
+ if (args_info->port_given)
+ write_into_file(outfile, "port", args_info->port_orig, 0);
+ if (args_info->verbose_given)
+ write_into_file(outfile, "verbose", args_info->verbose_orig, 0);
+
+
+ i = EXIT_SUCCESS;
+ return i;
}
int
@@ -155,30 +222,9 @@
return EXIT_FAILURE;
}
- if (args_info->help_given) {
- fprintf(outfile, "%s\n", "help");
- }
- if (args_info->version_given) {
- fprintf(outfile, "%s\n", "version");
- }
- if (args_info->port_given) {
- if (args_info->port_orig) {
- fprintf(outfile, "%s=\"%s\"\n", "port", args_info->port_orig);
- } else {
- fprintf(outfile, "%s\n", "port");
- }
- }
- if (args_info->verbose_given) {
- if (args_info->verbose_orig) {
- fprintf(outfile, "%s=\"%s\"\n", "verbose", args_info->verbose_orig);
- } else {
- fprintf(outfile, "%s\n", "verbose");
- }
- }
-
+ i = cmdline_parser_dump(outfile, args_info);
fclose (outfile);
- i = EXIT_SUCCESS;
return i;
}
@@ -188,9 +234,7 @@
cmdline_parser_release (args_info);
}
-
-/* gengetopt_strdup() */
-/* strdup.c replacement of strdup, which is not standard */
+/** @brief replacement of strdup, which is not standard */
char *
gengetopt_strdup (const char *s)
{
@@ -237,6 +281,7 @@
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
+ params.print_errors = 1;
result = cmdline_parser_internal (argc, argv, args_info, ¶ms, NULL);
@@ -697,7 +742,7 @@
int print_errors, struct custom_getopt_data *d)
{
char c = *d->nextchar++;
- char *temp = strchr(optstring, c);
+ const char *temp = strchr(optstring, c);
/* Increment `custom_optind' when we start to process its last
character. */
if (*d->nextchar == '\0')
@@ -878,6 +923,111 @@
opt_index);
}
+
+static char *package_name = 0;
+
+/**
+ * @brief updates an option
+ * @param field the generic pointer to the field to update
+ * @param orig_field the pointer to the orig field
+ * @param field_given the pointer to the number of occurrence of this option
+ * @param prev_given the pointer to the number of occurrence already seen
+ * @param value the argument for this option (if null no arg was specified)
+ * @param possible_values the possible values for this option (if specified)
+ * @param default_value the default value (in case the option only accepts
fixed values)
+ * @param arg_type the type of this option
+ * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
+ * @param override @see cmdline_parser_params.override
+ * @param no_free whether to free a possible previous value
+ * @param multiple_option whether this is a multiple option
+ * @param long_opt the corresponding long option
+ * @param short_opt the corresponding short option (or '-' if none)
+ * @param additional_error possible further error specification
+ */
+static
+int update_arg(void *field, char **orig_field,
+ unsigned int *field_given, unsigned int *prev_given,
+ char *value, char *possible_values[], const char *default_value,
+ cmdline_parser_arg_type arg_type,
+ int check_ambiguity, int override,
+ int no_free, int multiple_option,
+ const char *long_opt, char short_opt,
+ const char *additional_error)
+{
+ char *stop_char = 0;
+ const char *val = value;
+ int found;
+
+ stop_char = 0;
+ found = 0;
+
+ if (!multiple_option && prev_given && (*prev_given || (check_ambiguity &&
*field_given)))
+ {
+ if (short_opt != '-')
+ fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
+ package_name, long_opt, short_opt,
+ (additional_error ? additional_error : ""));
+ else
+ fprintf (stderr, "%s: `--%s' option given more than once%s\n",
+ package_name, long_opt,
+ (additional_error ? additional_error : ""));
+ return 1; /* failure */
+ }
+
+
+ if (field_given && *field_given && ! override)
+ return 0;
+ if (prev_given)
+ (*prev_given)++;
+ if (field_given)
+ (*field_given)++;
+ if (possible_values)
+ val = possible_values[found];
+
+ switch(arg_type) {
+ case ARG_INT:
+ if (val) *((int *)field) = strtol (val, &stop_char, 0);
+ break;
+ case ARG_SHORT:
+ if (val) *((short *)field) = (short)strtol (val, &stop_char, 0);
+ break;
+ default:
+ break;
+ };
+
+ /* check numeric conversion */
+ switch(arg_type) {
+ case ARG_INT:
+ case ARG_SHORT:
+ if (val && !(stop_char && *stop_char == '\0')) {
+ fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
+ return 1; /* failure */
+ }
+ break;
+ default:
+ ;
+ };
+
+ /* store the original value */
+ switch(arg_type) {
+ case ARG_NO:
+ break;
+ default:
+ if (value && orig_field) {
+ if (no_free) {
+ *orig_field = value;
+ } else {
+ if (*orig_field)
+ free (*orig_field); /* free previous string */
+ *orig_field = gengetopt_strdup (value);
+ }
+ }
+ };
+
+ return 0; /* OK */
+}
+
+
int
cmdline_parser_internal (int argc, char * const *argv, struct
gengetopt_args_info *args_info,
struct cmdline_parser_params *params, const char
*additional_error)
@@ -892,6 +1042,8 @@
int check_required;
int check_ambiguity;
+ package_name = argv[0];
+
override = params->override;
initialize = params->initialize;
check_required = params->check_required;
@@ -904,29 +1056,28 @@
optarg = 0;
optind = 0;
- opterr = 1;
+ opterr = params->print_errors;
optopt = '?';
while (1)
{
int option_index = 0;
- char *stop_char;
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
+ { "fd", 1, NULL, 'f' },
{ "port", 1, NULL, 'p' },
{ "verbose", 1, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
- stop_char = 0;
custom_optarg = optarg;
custom_optind = optind;
custom_opterr = opterr;
custom_optopt = optopt;
- c = custom_getopt_long (argc, argv, "hVp:v:", long_options,
&option_index);
+ c = custom_getopt_long (argc, argv, "hVf:p:v:", long_options,
&option_index);
optarg = custom_optarg;
optind = custom_optind;
@@ -947,46 +1098,42 @@
cmdline_parser_free (&local_args_info);
exit (EXIT_SUCCESS);
- case 'p': /* tcp port to be used to communicate with
FederateAmbassador. */
- if (local_args_info.port_given || (check_ambiguity &&
args_info->port_given))
- {
- fprintf (stderr, "%s: `--port' (`-p') option given more than
once%s\n", argv[0], (additional_error ? additional_error : ""));
- goto failure;
- }
- if (args_info->port_given && ! override)
- continue;
- local_args_info.port_given = 1;
- args_info->port_given = 1;
- args_info->port_arg = strtol (optarg, &stop_char, 0);
- if (!(stop_char && *stop_char == '\0')) {
- fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0],
optarg);
+ case 'f': /* file descriptor number to be used to communicate
with FederateAmbassador. */
+
+
+ if (update_arg( (void *)&(args_info->fd_arg),
+ &(args_info->fd_orig), &(args_info->fd_given),
+ &(local_args_info.fd_given), optarg, 0, 0, ARG_INT,
+ check_ambiguity, override, 0, 0,
+ "fd", 'f',
+ additional_error))
goto failure;
- }
- if (args_info->port_orig)
- free (args_info->port_orig); /* free previous string */
- args_info->port_orig = gengetopt_strdup (optarg);
+
break;
+ case 'p': /* tcp port to be used to communicate with
FederateAmbassador. */
- case 'v': /* verbose mode. */
- if (local_args_info.verbose_given || (check_ambiguity &&
args_info->verbose_given))
- {
- fprintf (stderr, "%s: `--verbose' (`-v') option given more than
once%s\n", argv[0], (additional_error ? additional_error : ""));
- goto failure;
- }
- if (args_info->verbose_given && ! override)
- continue;
- local_args_info.verbose_given = 1;
- args_info->verbose_given = 1;
- args_info->verbose_arg = (short)strtol (optarg, &stop_char, 0);
- if (!(stop_char && *stop_char == '\0')) {
- fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0],
optarg);
+
+ if (update_arg( (void *)&(args_info->port_arg),
+ &(args_info->port_orig), &(args_info->port_given),
+ &(local_args_info.port_given), optarg, 0, 0, ARG_INT,
+ check_ambiguity, override, 0, 0,
+ "port", 'p',
+ additional_error))
goto failure;
- }
- if (args_info->verbose_orig)
- free (args_info->verbose_orig); /* free previous string */
- args_info->verbose_orig = gengetopt_strdup (optarg);
+
break;
+ case 'v': /* verbose mode. */
+
+ if (update_arg( (void *)&(args_info->verbose_arg),
+ &(args_info->verbose_orig), &(args_info->verbose_given),
+ &(local_args_info.verbose_given), optarg, 0, 0, ARG_SHORT,
+ check_ambiguity, override, 0, 0,
+ "verbose", 'v',
+ additional_error))
+ goto failure;
+
+ break;
case 0: /* Long option with no short option */
case '?': /* Invalid option. */
Index: RTIA/main.cc
===================================================================
RCS file: /sources/certi/certi/RTIA/main.cc,v
retrieving revision 3.25
retrieving revision 3.26
diff -u -b -r3.25 -r3.26
--- RTIA/main.cc 7 Dec 2008 20:16:10 -0000 3.25
+++ RTIA/main.cc 14 Sep 2009 20:51:51 -0000 3.26
@@ -18,7 +18,7 @@
// along with this program ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// $Id: main.cc,v 3.25 2008/12/07 20:16:10 gotthardp Exp $
+// $Id: main.cc,v 3.26 2009/09/14 20:51:51 erk Exp $
// ----------------------------------------------------------------------------
#include <config.h>
@@ -79,8 +79,12 @@
if (args.port_given) {
rtia_port = args.port_arg;
}
+ int rtia_fd = -1;
+ if (args.fd_given) {
+ rtia_fd = args.fd_arg;
+ }
- RTIA rtia(rtia_port);
+ RTIA rtia(rtia_port, rtia_fd);
try {
rtia.execute();
@@ -128,4 +132,4 @@
throw MemoryExhausted("RTIA has exhausted memory error");
}
-// EOF $Id: main.cc,v 3.25 2008/12/07 20:16:10 gotthardp Exp $
+// EOF $Id: main.cc,v 3.26 2009/09/14 20:51:51 erk Exp $
Index: RTIA/RTIA.ggo
===================================================================
RCS file: /sources/certi/certi/RTIA/RTIA.ggo,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- RTIA/RTIA.ggo 30 Oct 2008 10:20:05 -0000 1.4
+++ RTIA/RTIA.ggo 14 Sep 2009 20:51:52 -0000 1.5
@@ -9,5 +9,6 @@
args "-F RTIA_cmdline -G"
# Options
+option "fd" f "file descriptor number to be used to communicate
with FederateAmbassador" int optional
option "port" p "tcp port to be used to communicate with
FederateAmbassador" int optional
option "verbose" v "verbose mode" short optional
Index: RTIA/RTIA_cmdline.h
===================================================================
RCS file: /sources/certi/certi/RTIA/RTIA_cmdline.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- RTIA/RTIA_cmdline.h 30 Oct 2008 10:20:06 -0000 1.2
+++ RTIA/RTIA_cmdline.h 14 Sep 2009 20:51:52 -0000 1.3
@@ -1,6 +1,6 @@
/** @file RTIA_cmdline.h
* @brief The header file for the command line option parser
- * generated by GNU Gengetopt version 2.21
+ * generated by GNU Gengetopt version 2.22.1
* http://www.gnu.org/software/gengetopt.
* DO NOT modify this file, since it can be overwritten
* @author GNU Gengetopt by Lorenzo Bettini */
@@ -13,6 +13,8 @@
#include "config.h"
#endif
+#include <stdio.h> /* for FILE */
+
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -32,6 +34,9 @@
{
const char *help_help; /**< @brief Print help and exit help description. */
const char *version_help; /**< @brief Print version and exit help
description. */
+ int fd_arg; /**< @brief file descriptor number to be used to communicate
with FederateAmbassador. */
+ char * fd_orig; /**< @brief file descriptor number to be used to
communicate with FederateAmbassador original value given at command line. */
+ const char *fd_help; /**< @brief file descriptor number to be used to
communicate with FederateAmbassador help description. */
int port_arg; /**< @brief tcp port to be used to communicate with
FederateAmbassador. */
char * port_orig; /**< @brief tcp port to be used to communicate with
FederateAmbassador original value given at command line. */
const char *port_help; /**< @brief tcp port to be used to communicate with
FederateAmbassador help description. */
@@ -39,10 +44,11 @@
char * verbose_orig; /**< @brief verbose mode original value given at
command line. */
const char *verbose_help; /**< @brief verbose mode help description. */
- int help_given ; /**< @brief Whether help was given. */
- int version_given ; /**< @brief Whether version was given. */
- int port_given ; /**< @brief Whether port was given. */
- int verbose_given ; /**< @brief Whether verbose was given. */
+ unsigned int help_given ; /**< @brief Whether help was given. */
+ unsigned int version_given ; /**< @brief Whether version was given. */
+ unsigned int fd_given ; /**< @brief Whether fd was given. */
+ unsigned int port_given ; /**< @brief Whether port was given. */
+ unsigned int verbose_given ; /**< @brief Whether verbose was given. */
} ;
@@ -50,9 +56,10 @@
struct cmdline_parser_params
{
int override; /**< @brief whether to override possibly already present
options (default 0) */
- int initialize; /**< @brief whether to initialize the option structure
gengetopt_args_info (default 0) */
- int check_required; /**< @brief whether to check that all required options
were provided (default 0) */
+ int initialize; /**< @brief whether to initialize the option structure
gengetopt_args_info (default 1) */
+ int check_required; /**< @brief whether to check that all required options
were provided (default 1) */
int check_ambiguity; /**< @brief whether to check for options already
specified in the option structure gengetopt_args_info (default 0) */
+ int print_errors; /**< @brief whether getopt_long should print an error
message for a bad option (default 1) */
} ;
/** @brief the purpose string of the program */
@@ -100,6 +107,15 @@
struct cmdline_parser_params *params);
/**
+ * Save the contents of the option struct into an already open FILE stream.
+ * @param outfile the stream where to dump options
+ * @param args_info the option struct to dump
+ * @return 0 if everything went fine, NON 0 if an error took place
+ */
+int cmdline_parser_dump(FILE *outfile,
+ struct gengetopt_args_info *args_info);
+
+/**
* Save the contents of the option struct into a (text) file.
* This file can be read by the config file parser (if generated by gengetopt)
* @param filename the file where to save
@@ -119,11 +135,18 @@
void cmdline_parser_print_version(void);
/**
+ * Initializes all the fields a cmdline_parser_params structure
+ * to their default values
+ * @param params the structure to initialize
+ */
+void cmdline_parser_params_init(struct cmdline_parser_params *params);
+
+/**
* Allocates dynamically a cmdline_parser_params structure and initializes
- * all its fields to 0
- * @return the initialized cmdline_parser_params structure
+ * all its fields to their default values
+ * @return the created and initialized cmdline_parser_params structure
*/
-struct cmdline_parser_params *cmdline_parser_params_init();
+struct cmdline_parser_params *cmdline_parser_params_create(void);
/**
* Initializes the passed gengetopt_args_info structure's fields
Index: RTIA/Communications.hh
===================================================================
RCS file: /sources/certi/certi/RTIA/Communications.hh,v
retrieving revision 3.17
retrieving revision 3.18
diff -u -b -r3.17 -r3.18
--- RTIA/Communications.hh 12 Oct 2008 11:46:39 -0000 3.17
+++ RTIA/Communications.hh 14 Sep 2009 20:51:52 -0000 3.18
@@ -18,7 +18,7 @@
// along with this program ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// $Id: Communications.hh,v 3.17 2008/10/12 11:46:39 gotthardp Exp $
+// $Id: Communications.hh,v 3.18 2009/09/14 20:51:52 erk Exp $
// ----------------------------------------------------------------------------
#ifndef _CERTI_COMMUNICATIONS_HH
@@ -42,7 +42,7 @@
class Communications
{
public:
- Communications(int RTIA_port);
+ Communications(int RTIA_port, int RTIA_fd);
~Communications();
//! Send a message to RTIG.
@@ -91,4 +91,4 @@
#endif // _CERTI_COMMUNICATIONS_HH
-// $Id: Communications.hh,v 3.17 2008/10/12 11:46:39 gotthardp Exp $
+// $Id: Communications.hh,v 3.18 2009/09/14 20:51:52 erk Exp $
Index: RTIA/Communications.cc
===================================================================
RCS file: /sources/certi/certi/RTIA/Communications.cc,v
retrieving revision 3.33
retrieving revision 3.34
diff -u -b -r3.33 -r3.34
--- RTIA/Communications.cc 12 Oct 2008 11:46:39 -0000 3.33
+++ RTIA/Communications.cc 14 Sep 2009 20:51:52 -0000 3.34
@@ -18,7 +18,7 @@
// along with this program ; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// $Id: Communications.cc,v 3.33 2008/10/12 11:46:39 gotthardp Exp $
+// $Id: Communications.cc,v 3.34 2009/09/14 20:51:52 erk Exp $
// ----------------------------------------------------------------------------
#include <config.h>
@@ -93,7 +93,7 @@
// ----------------------------------------------------------------------------
//! Communications.
-Communications::Communications(int RTIA_port)
+Communications::Communications(int RTIA_port, int RTIA_fd)
{
char nom_serveur_RTIG[200] ;
const char *default_host = "localhost" ;
@@ -113,7 +113,14 @@
socketUDP = new SocketUDP();
// Federate/RTIA link creation.
- socketUN->acceptUN(RTIA_port);
+ if (0 <= RTIA_fd) {
+ socketUN->setSocketFD(RTIA_fd);
+ } else if (0 <= RTIA_port) {
+ if (socketUN->connectUN(RTIA_port) == -1)
+ exit(EXIT_FAILURE);
+ } else {
+ exit(EXIT_FAILURE);
+ }
// RTIG TCP link creation.
const char *certihost = NULL ;
@@ -362,4 +369,4 @@
}} // namespace certi/rtia
-// $Id: Communications.cc,v 3.33 2008/10/12 11:46:39 gotthardp Exp $
+// $Id: Communications.cc,v 3.34 2009/09/14 20:51:52 erk Exp $
Index: libCERTI/SocketUN.hh
===================================================================
RCS file: /sources/certi/certi/libCERTI/SocketUN.hh,v
retrieving revision 3.17
retrieving revision 3.18
diff -u -b -r3.17 -r3.18
--- libCERTI/SocketUN.hh 11 Oct 2008 12:53:53 -0000 3.17
+++ libCERTI/SocketUN.hh 14 Sep 2009 20:51:52 -0000 3.18
@@ -20,14 +20,12 @@
#ifndef CERTI_SOCKET_UN_HH
#define CERTI_SOCKET_UN_HH
-#if defined(RTIA_USE_TCP)
- #include "SocketTCP.hh"
-#else
+#include "SocketTCP.hh"
+#ifndef WIN32
#include <netdb.h>
#include <sys/socket.h>
#endif
-#define NOM_FICHIER_SOCKET ".comFedRTI"
#include "Socket.hh"
#include "PrettyDebug.hh"
@@ -65,20 +63,20 @@
SocketUN(SignalHandlerType theType = stSignalInterrupt);
~SocketUN();
- #ifdef _WIN32
- void connectUN(int Server_pid); // Called by
client to connect
- #else
- int connectUN(pid_t Server_pid);
- #endif
- void acceptUN(int RTIA_port);
//Called by server
+#ifndef _WIN32
+ typedef int SOCKET;
+#endif
+
+ int listenUN();
+ int connectUN(int port);
+ int acceptUN(unsigned msec);
+
+ SOCKET socketpair();
+ void setSocketFD(SOCKET fd) { _socket_un = fd; }
bool isDataReady();
- #ifdef _WIN32
SOCKET returnSocket();
- #else
- int returnSocket();
- #endif
void send(const unsigned char *, size_t) throw
(NetworkError, NetworkSignal);
void receive(const unsigned char *, size_t) throw (NetworkError,
NetworkSignal);
@@ -86,14 +84,7 @@
protected:
void error(const char *) throw (NetworkError);
- #ifdef _WIN32
SOCKET _socket_un;
- #else
- int _socket_un;
- #endif
-
- bool _est_serveur ;
- bool _est_init_un ;
SignalHandlerType HandlerType ;
@@ -104,9 +95,6 @@
Socket::ByteCount_t SentBytesCount ;
Socket::ByteCount_t RcvdBytesCount ;
- // la socket du serveur RTIA qui a ete cree par le federe-client
- int sock_connect ;
-
pdCDebug *pD ;
#ifdef SOCKUN_BUFFER_LENGTH
Index: libCERTI/SocketUN.cc
===================================================================
RCS file: /sources/certi/certi/libCERTI/SocketUN.cc,v
retrieving revision 3.29
retrieving revision 3.30
diff -u -b -r3.29 -r3.30
--- libCERTI/SocketUN.cc 4 Jun 2009 11:58:51 -0000 3.29
+++ libCERTI/SocketUN.cc 14 Sep 2009 20:51:52 -0000 3.30
@@ -21,20 +21,21 @@
#include "certi.hh"
#include "SocketUN.hh"
-#if defined(RTIA_USE_TCP)
- #include "SocketTCP.hh"
- #if not defined(_WIN32)
- #include <cstring>
- #include <cerrno>
- #endif
-#else
-#include <unistd.h>
-#include <strings.h>
+#include "SocketTCP.hh"
+
+#include <cstring>
+#include <cerrno>
#include <sstream>
#include <string>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/un.h>
+
+#ifdef WIN32
+# include <windows.h>
+# include <io.h>
+#else
+# include <unistd.h>
+# include <sys/un.h>
+# include <sys/types.h>
+# include <sys/socket.h>
#endif
#include <iostream>
@@ -45,212 +46,148 @@
using std::cout ;
using std::endl ;
+#ifdef WIN32
+extern "C" int socketpair_win32(SOCKET socks[2], int make_overlapped);
+#endif
+
namespace certi {
static PrettyDebug G("GENDOC",__FILE__);
-#define MAX_ATTEMPTS 3
-// ----------------------------------------------------------------------------
-//! Called by server to open the socket and wait for the connection.
-void
-SocketUN::acceptUN(int RTIA_port)
+#ifndef WIN32
+static void closesocket(int fd)
{
-#if defined(RTIA_USE_TCP)
- struct sockaddr_in nom_client, nom_serveur;
- int lg_nom;
- int result;
-
-#if defined(_WIN32)
- assert(SocketTCP::winsockInitialized());
- int socklen;
-#else
- socklen_t socklen;
+ close(fd);
+}
#endif
- if((sock_connect=socket(AF_INET,SOCK_STREAM,0)) < 0)
- error("socket");
+int
+SocketUN::listenUN()
+{
+ struct sockaddr_in addr;
+#ifdef _WIN32
+ int addrlen = sizeof(addr);
+#else
+ socklen_t addrlen = sizeof(addr);
+#endif
- memset(&nom_serveur, 0, sizeof(nom_serveur));
+ if ((_socket_un = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ return -1;
+ pD->Out(pdInit, "Server: Created IPV4 socket.");
- nom_serveur.sin_family = AF_INET;
- nom_serveur.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addr.sin_port = 0;
- // TEMPO : UNIX socket emulation with TCP/IP sockets
- int tcpport;
- if (RTIA_port>0) {
- tcpport = RTIA_port;
- } else {
- tcpport = getpid();
+ if (bind(_socket_un, (const struct sockaddr*) &addr, sizeof(addr)) == -1) {
+ closesocket(_socket_un);
+ _socket_un = -1;
+ return -1;
}
+ pD->Out(pdInit, "Server: Bound to anonymous IPV4 address.");
- // make sure it is contained in a short
- if (tcpport > 65535)
- throw RTIinternalError("TCP Port too big.");
-
- nom_serveur.sin_port = htons(tcpport);
-
- lg_nom = sizeof(nom_serveur);
-
- result = ::bind(sock_connect,(sockaddr *)&nom_serveur, lg_nom);
-
- if(result <0)
- {// Error on Bind. If the error is "Address already in use",
allow
- // the user to choose to "reuse address" and then try again.
- error("bind");
+ if (getsockname(_socket_un, (struct sockaddr*) &addr, &addrlen) == -1) {
+ closesocket(_socket_un);
+ _socket_un = -1;
+ return -1;
}
- pD->Out(pdInit, "Server: Bind succeeded, now listening.");
-#else
-
- struct sockaddr_un nom_client, nom_serveur ;
- socklen_t socklen ;
+ pD->Out(pdInit, "Server: Got server port address %d.", ntohs(addr.sin_port));
- pD->Out(pdInit, "Opening Server UNIX Socket.");
-
- // Socket
- if ((sock_connect = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
- {
- pD->Out(pdError, "Cannot open Server UNIX Socket.");
- error("socket");
+ if (listen(_socket_un, 1) == -1) {
+ closesocket(_socket_un);
+ _socket_un = -1;
+ return -1;
}
+ pD->Out(pdInit, "Server: Now listening.");
- pD->Out(pdInit, "Server has got UNIX Socket FpD->");
-
- // Set Server address
- memset(&nom_serveur, 0, sizeof(nom_serveur));
-
- nom_serveur.sun_family = AF_UNIX ;
-
- if (strlen(NOM_FICHIER_SOCKET) > 90)
- throw RTIinternalError("NOM_FICHIER_SOCKET too long.");
-
- char buffer[256] ;
- sprintf( buffer, "%s.%d", NOM_FICHIER_SOCKET, getpid() ) ;
- name = buffer ;
- strcpy( nom_serveur.sun_path, name.c_str() );
-
-
- // Bind
- if (bind(sock_connect, (struct sockaddr*)&nom_serveur,
- sizeof(struct sockaddr_un)) < 0)
- error("bind");
-
- pD->Out(pdInit, "Server: Bind succeeded, now listening.");
-#endif
-
-// Listen
-if (listen(sock_connect, 10) == -1)
- error("listen");
-
-pD->Out(pdInit, "Server: Listen returned, now accepting.");
-
-// Accept
-socklen = sizeof(struct sockaddr_in);
-if ((_socket_un = accept(sock_connect,
- (struct sockaddr*)&nom_client,
- &socklen)) < 0)
- // HPUX:(int*) &lg_nom)) < 0)
- error("accept");
-
-pD->Out(pdInit, "Server: Accept OK, server running.");
-
-_est_init_un = true ;
-_est_serveur = true ;
+ return ntohs(addr.sin_port);
}
// ----------------------------------------------------------------------------
//! Called by client to connect.
-#ifdef RTIA_USE_TCP
- void SocketUN::connectUN(int Server_pid)
-#else
- int SocketUN::connectUN(pid_t Server_pid)
-#endif
+int
+SocketUN::connectUN(int port)
{
-int Attempt = 0 ;
-int Result = 0 ;
+ if ((_socket_un = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ return -1;
+ pD->Out(pdInit, "Client: Created IPV4 socket.");
+
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addr.sin_port = htons(port);
+ if (connect(_socket_un, (const struct sockaddr*) &addr, sizeof(addr)) == -1)
+ return -1;
+ pD->Out(pdInit, "Client: Connected to IPV4 address at port %d.", port);
-#if defined(RTIA_USE_TCP)
- struct sockaddr_in nom_serveur;
- int lg_nom;
-#ifdef _WIN32
- struct hostent *hptr = NULL;
- assert(SocketTCP::winsockInitialized());
-#endif
-#else
- struct sockaddr_un nom_serveur;
-#endif
-
-char buffer[256] ;
-sprintf( buffer, "%s.%d", NOM_FICHIER_SOCKET, Server_pid ) ;
-name = buffer ;
-
-while (Attempt < MAX_ATTEMPTS)
- {
- pD->Out(pdInit, "Opening Client UNIX Socket.");
-
-// TCP Socket case--------------------------------------------------
-#if defined(RTIA_USE_TCP)
- if((_socket_un = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- error("socket");
-
- pD->Out(pdInit, "Client has got UNIX Socket FpD->");
-
- // Clear and set Server adress
- memset(&nom_serveur, 0, sizeof(nom_serveur));
-
- nom_serveur.sin_family = AF_INET;
- nom_serveur.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-
- if (Server_pid > 65535)
- throw RTIinternalError("NOM_FICHIER_SOCKET too long.");
- nom_serveur.sin_port= htons(Server_pid);
-
- lg_nom = sizeof(nom_serveur);
- Result = ::connect(_socket_un,(sockaddr *)&nom_serveur, lg_nom);
-
- //pD->Out(pdInit, "Client: Connect returned %d.", Result);
-#else
- if ((_socket_un = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
- error("socket");
-
- pD->Out(pdInit, "Client has got UNIX Socket FpD->");
-
- memset(&nom_serveur, 0, sizeof(nom_serveur)); // Clear and set Server
adress
- nom_serveur.sun_family = AF_UNIX ;
-
- if (strlen(NOM_FICHIER_SOCKET) > 90)
- throw RTIinternalError("NOM_FICHIER_SOCKET too long.");
- strcpy( nom_serveur.sun_path, name.c_str() );
- Result = connect(_socket_un,
- (struct sockaddr*)
&nom_serveur,
- sizeof(struct sockaddr_un));
- pD->Out(pdInit, "Client: Connect returned %d.", Result);
-#endif
+ return 0;
+}
- if (Result == 0) // Success ? Yes->break
- break ;
+// ----------------------------------------------------------------------------
+//! Called by server to open the socket and wait for the connection.
+int
+SocketUN::acceptUN(unsigned msec)
+{
+ fd_set fdset ;
+ FD_ZERO(&fdset);
+ FD_SET(_socket_un, &fdset);
+
+ struct timeval timeout;
+ timeout.tv_sec = msec/1000;
+ timeout.tv_usec = 1000*(msec%1000);
- // Failure
- pD->Out(pdError, "SocketUN: Connect, attempt #%d out of %d failed",
- Attempt + 1, MAX_ATTEMPTS);
- sleep(1);
- Attempt ++ ;
+ pD->Out(pdInit, "Server: Waiting for a connection to accept.");
+ if (select(_socket_un + 1, &fdset, NULL, NULL, &timeout) <= 0) {
+ closesocket(_socket_un);
+ _socket_un = -1;
+ return -1;
}
-pD->Out(pdInit, "Client: Done.");
+ pD->Out(pdInit, "Server: Accepting connection.");
+ SOCKET accepted;
+ if ((accepted = accept(_socket_un, NULL, NULL)) == -1) {
+ closesocket(_socket_un);
+ _socket_un = -1;
+ return -1;
+ }
+ pD->Out(pdInit, "Server: Accepted IPV4 connection.");
-if( Result == 0 )
- _est_init_un = true ;
+ closesocket(_socket_un);
+ _socket_un = accepted;
+ return 0;
+}
#ifdef _WIN32
-return ; //Result ;
+SOCKET
+SocketUN::socketpair()
+{
+ SOCKET fd[2];
+ if (::socketpair_win32(fd, 0) == -1) {
+ perror("socketpair");
+ return -1;
+ }
+ _socket_un = fd[0];
+ return fd[1];
+}
#else
-return Result ;
-#endif
+int
+SocketUN::socketpair()
+{
+ SOCKET fd[2];
+ if (::socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
+ perror("socketpair");
+ return -1;
+ }
+ _socket_un = fd[0];
+ return fd[1];
}
+#endif
// ----------------------------------------------------------------------------
//! Does not open the socket, see Init methods.
SocketUN::SocketUN(SignalHandlerType theType)
- : _socket_un(0), _est_serveur(false), _est_init_un(false),
+ : _socket_un(-1),
HandlerType(theType), SentBytesCount(0), RcvdBytesCount(0)
{
#ifdef _WIN32
@@ -269,31 +206,17 @@
//! Close the socket.
SocketUN::~SocketUN()
{
-if (_est_init_un)
- {
- #ifdef _WIN32
+ if (0 <= _socket_un)
closesocket(_socket_un);
- if (_est_serveur)
- closesocket(sock_connect);
- #else
- close(_socket_un);
- if (_est_serveur)
- close(sock_connect);
- unlink(name.c_str());
- #endif
- if (_est_serveur)
- pD->Out(pdTerm, "Server: Closed all sockets.");
- else
- pD->Out(pdTerm, "Client: Closed all sockets.");
- }
+ pD->Out(pdTerm, "SocketUN: Closed all sockets.");
#ifdef _WIN32
SocketTCP::winsockShutdown();
#endif
-pD->Out(pdCom, "Unix Socket %2d : total = %9d Bytes sent", _socket_un,
SentBytesCount ) ;
-pD->Out(pdCom, "Unix Socket %2d : total = %9d Bytes received", _socket_un,
RcvdBytesCount ) ;
+pD->Out(pdCom, "Unix Socket %2d : total = %9db sent", _socket_un,
SentBytesCount ) ;
+pD->Out(pdCom, "Unix Socket %2d : total = %9db received", _socket_un,
RcvdBytesCount ) ;
delete pD ;
}
@@ -311,7 +234,7 @@
unsigned long total_sent = 0 ;
// G.Out(pdGendoc,"enter SocketUN::send");
-assert(_est_init_un);
+assert(0 <= _socket_un);
pD->Out(pdTrace, "Beginning to send UN message...");
@@ -405,7 +328,7 @@
{
// G.Out(pdGendoc,"enter SocketUN::receive");
-assert(_est_init_un);
+assert(0 <= _socket_un);
long nReceived = 0 ;
Index: libCERTI/SecureTCPSocket.cc
===================================================================
RCS file: /sources/certi/certi/libCERTI/SecureTCPSocket.cc,v
retrieving revision 3.11
retrieving revision 3.12
diff -u -b -r3.11 -r3.12
--- libCERTI/SecureTCPSocket.cc 13 Oct 2008 11:27:51 -0000 3.11
+++ libCERTI/SecureTCPSocket.cc 14 Sep 2009 20:51:52 -0000 3.12
@@ -17,7 +17,7 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
-// $Id: SecureTCPSocket.cc,v 3.11 2008/10/13 11:27:51 gotthardp Exp $
+// $Id: SecureTCPSocket.cc,v 3.12 2009/09/14 20:51:52 erk Exp $
// ----------------------------------------------------------------------------
@@ -26,7 +26,9 @@
#include "PrettyDebug.hh"
#include <sys/types.h>
+#ifndef _WIN32
#include <unistd.h>
+#endif
#include <cstdlib>
//#include <pwd.h>
@@ -248,4 +250,4 @@
}
-// $Id: SecureTCPSocket.cc,v 3.11 2008/10/13 11:27:51 gotthardp Exp $
+// $Id: SecureTCPSocket.cc,v 3.12 2009/09/14 20:51:52 erk Exp $
Index: libCERTI/CMakeLists.txt
===================================================================
RCS file: /sources/certi/certi/libCERTI/CMakeLists.txt,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -b -r1.25 -r1.26
--- libCERTI/CMakeLists.txt 2 Apr 2009 19:58:08 -0000 1.25
+++ libCERTI/CMakeLists.txt 14 Sep 2009 20:51:52 -0000 1.26
@@ -120,6 +120,9 @@
)
SET(CERTI_SOCKET_SRCS ${CERTI_SOCKET_SRCS} SocketUDP.cc SocketMC.cc
SocketUN.cc SocketUDP.hh SocketMC.hh SocketUN.hh)
+IF (WIN32)
+ SET(CERTI_SOCKET_SRCS ${CERTI_SOCKET_SRCS} socketpair_win32.c)
+ENDIF (WIN32)
SOURCE_GROUP("Source Files\\Socket" FILES ${CERTI_SOCKET_SRCS})
SOURCE_GROUP("Source Files\\Federation" FILES ${CERTI_FEDERATION_SRCS})
Index: CMakeLists.txt
===================================================================
RCS file: /sources/certi/certi/CMakeLists.txt,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -b -r1.69 -r1.70
--- CMakeLists.txt 31 Aug 2009 13:25:44 -0000 1.69
+++ CMakeLists.txt 14 Sep 2009 20:51:52 -0000 1.70
@@ -56,20 +56,15 @@
OPTION(FORCE_NO_X11
"Force not to use X11 (i.e. no Billard GUI)" OFF)
-# On Win32 platform RTIA must use TCP Socket for RTIA <-->FedAmbd
-# On other platform TCP socket is an option
-IF(WIN32)
- SET(RTIA_USE_TCP ON)
-ELSE(WIN32)
- OPTION(RTIA_USE_TCP
+# The communication channel to RTIA
+OPTION(RTIA_USE_TCP
"Force TCP socket usage between RTIA and FederateAmbassador
(default is Unix Socket)" OFF)
-ENDIF(WIN32)
-
-SET(CMAKE_BUILD_TYPE "Debug")
IF(RTIA_USE_TCP)
ADD_DEFINITIONS(-DRTIA_USE_TCP)
ENDIF(RTIA_USE_TCP)
+SET(CMAKE_BUILD_TYPE "Debug")
+
# Force CMAKE_COMPILER_IS_GNUCC even if gcc used with ccache
IF("${CMAKE_C_COMPILER}" MATCHES "ccache")
IF("${CMAKE_C_COMPILER_ARG1}" MATCHES "gcc")
Index: libCERTI/socketpair_win32.c
===================================================================
RCS file: libCERTI/socketpair_win32.c
diff -N libCERTI/socketpair_win32.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libCERTI/socketpair_win32.c 14 Sep 2009 20:51:52 -0000 3.1
@@ -0,0 +1,89 @@
+/* socketpair.c
+ * Copyright 2007 by Nathan C. Myers <address@hidden>; all rights reserved.
+ * This code is Free Software. It may be copied freely, in original or
+ * modified form, subject only to the restrictions that (1) the author is
+ * relieved from all responsibilities for any use for any purpose, and (2)
+ * this copyright notice must be retained, unchanged, in its entirety. If
+ * for any reason the author might be held responsible for any consequences
+ * of copying or use, license is withheld.
+ */
+
+/* Changes:
+ * 2007-04-25:
+ * preserve value of WSAGetLastError() on all error returns.
+ * 2007-04-22: (Thanks to Matthew Gregan <address@hidden>)
+ * s/EINVAL/WSAEINVAL/ fix trivial compile failure
+ * s/socket/WSASocket/ enable creation of sockets suitable as stdin/stdout
+ * of a child process.
+ * add argument make_overlapped
+ */
+
+#include <string.h>
+
+#include <winsock2.h>
+#include <windows.h>
+#include <io.h>
+
+/* socketpair:
+ * If make_overlapped is nonzero, both sockets created will be usable for
+ * "overlapped" operations via WSASend etc. If make_overlapped is zero,
+ * socks[0] (only) will be usable with regular ReadFile etc., and thus
+ * suitable for use as stdin or stdout of a child process. Note that the
+ * sockets must be closed with closesocket() regardless.
+ */
+
+int socketpair_win32(SOCKET socks[2], int make_overlapped)
+{
+ struct sockaddr_in addr;
+ SOCKET listener;
+ int e;
+ int addrlen = sizeof(addr);
+ DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0);
+
+ if (socks == 0) {
+ WSASetLastError(WSAEINVAL);
+ return SOCKET_ERROR;
+ }
+
+ socks[0] = socks[1] = INVALID_SOCKET;
+ if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
+ return SOCKET_ERROR;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(0x7f000001);
+ addr.sin_port = 0;
+
+ e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
+ if (e == SOCKET_ERROR) {
+ e = WSAGetLastError();
+ closesocket(listener);
+ WSASetLastError(e);
+ return SOCKET_ERROR;
+ }
+ e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
+ if (e == SOCKET_ERROR) {
+ e = WSAGetLastError();
+ closesocket(listener);
+ WSASetLastError(e);
+ return SOCKET_ERROR;
+ }
+
+ do {
+ if (listen(listener, 1) == SOCKET_ERROR) break;
+ if ((socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags))
+ == INVALID_SOCKET) break;
+ if (connect(socks[0], (const struct sockaddr*) &addr,
+ sizeof(addr)) == SOCKET_ERROR) break;
+ if ((socks[1] = accept(listener, NULL, NULL))
+ == INVALID_SOCKET) break;
+ closesocket(listener);
+ return 0;
+ } while (0);
+ e = WSAGetLastError();
+ closesocket(listener);
+ closesocket(socks[0]);
+ closesocket(socks[1]);
+ WSASetLastError(e);
+ return SOCKET_ERROR;
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [certi-cvs] certi libRTI/RTIambassador.cc libRTI/RTIambPriv...,
certi-cvs <=