/** * Accept a connection from a client. * \param protocol the protocol to use (such as "tcpip" or "gm") * \param hostname optional hostname of the expected client (may be NULL) * \param port number of the port to accept on * \param mtu maximum transmission unit * \param broker either 1 or 0 to indicate if connection is brokered through * the mothership * \return new CRConnection object, or NULL */ CRConnection * crNetAcceptClient( const char *protocol, const char *hostname, unsigned short port, unsigned int mtu, int broker ) { CRConnection *conn; CRASSERT( cr_net.initialized ); conn = (CRConnection *) crCalloc( sizeof( *conn ) ); if (!conn) return NULL; /* init the non-zero fields */ conn->type = CR_NO_CONNECTION; /* we don't know yet */ conn->recv_credits = CR_INITIAL_RECV_CREDITS; conn->port = port; conn->mtu = mtu; conn->buffer_size = mtu; conn->broker = broker; conn->endianness = crDetermineEndianness(); conn->teac_id = -1; conn->teac_rank = -1; conn->tcscomm_id = -1; conn->tcscomm_rank = -1; /* now, just dispatch to the appropriate protocol's initialization functions. */ crDebug("In crNetAcceptClient( protocol=\"%s\" port=%d mtu=%d )", protocol, (int) port, (int) mtu); /* special case */ if ( !crStrncmp( protocol, "file", crStrlen( "file" ) ) || !crStrncmp( protocol, "swapfile", crStrlen( "swapfile" ) ) ) { char filename[4096]; char protocol_only[4096]; cr_net.use_file++; if (!crParseURL(protocol, protocol_only, filename, NULL, 0)) { crError( "Malformed URL: \"%s\"", protocol ); } conn->hostname = crStrdup( filename ); /* call the protocol-specific init routines */ // ktd (add) InitConnection(conn, protocol_only, mtu); // ktd (add) } else { /* call the protocol-specific init routines */ InitConnection(conn, protocol, mtu); } crNetAccept( conn, hostname, port ); return conn; }
/** * Find the index of the given enum value in the SPUOption's list of * possible enum values. * Return the enum index, or -1 if not found. */ int crSPUGetEnumIndex( const SPUOptions *options, const char *optName, const char *value ) { const SPUOptions *opt; const int valueLen = crStrlen(value); /* first, find the right option */ for (opt = options; opt->option; opt++) { if (crStrcmp(opt->option, optName) == 0) { char **values; int i; CRASSERT(opt->type == CR_ENUM); /* break into array of strings */ /* min string should be of form "'enum1', 'enum2', 'enum3', etc" */ values = crStrSplit(opt->min, ","); /* search the array */ for (i = 0; values[i]; i++) { /* find leading quote */ const char *e = crStrchr(values[i], '\''); CRASSERT(e); if (e) { /* test for match */ if (crStrncmp(value, e + 1, valueLen) == 0 && e[valueLen + 1] == '\'') { crFreeStrings(values); return i; } } } /* enum value not found! */ crFreeStrings(values); return -1; } } return -1; }
static void printspu_signal_handler(int signum) { /* If we receive a signal here, we should issue a marker. */ if (print_spu.fp != NULL && print_spu.marker_text != NULL) { char *text = print_spu.marker_text; char *nextVariable = crStrchr(text, '$'); /* Some of the variables will need this information */ struct timeval tv; struct timezone tz; struct tm *tm; gettimeofday(&tv, &tz); tm = localtime(&tv.tv_sec); /* While we still have variables, keep looking for more */ while (nextVariable != NULL) { /* Print the constant text up to the variable. */ if (nextVariable > text) { fprintf(print_spu.fp, "%.*s", (int) (nextVariable - text), text); text = nextVariable; } /* Make sure the '$' introduces a variable marker */ if (nextVariable[1] != '(') { /* Not a variable marker at all */ fprintf(print_spu.fp, "%c", '$'); text = nextVariable + 1; nextVariable = crStrchr(text, '$'); } else { /* Look for the last character */ char *endOfVariable = crStrchr(nextVariable, ')'); if (endOfVariable == NULL) { /* No end to the marker - spit it out as text */ text = nextVariable; nextVariable = NULL; } else { /* Compare it to the known variables */ /* Skip the initial '$(' and the final ')' */ char *variableName = nextVariable + 2; int variableLength = endOfVariable - nextVariable - 3 + 1; if (crStrncmp(variableName, "signal", variableLength) == 0) { fprintf(print_spu.fp, "%d", signum); } else if (crStrncmp(variableName, "date", variableLength) == 0) { fprintf(print_spu.fp, "%d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); } else if (crStrncmp(variableName, "time", variableLength) == 0) { fprintf(print_spu.fp, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); } else { /* Unknown variable. Print it verbatim. */ fprintf(print_spu.fp, "%.*s", (int) (endOfVariable - nextVariable + 1), nextVariable); } /* In all cases, advance our internal text pointer to after the endOfVariable indicator */ text = endOfVariable + 1; /* And look for the next variable */ nextVariable = crStrchr(text, '$'); } } /* end of processing a valid variable */ } /* end of processing a variable */ /* Print out any remaining text, even if it's an empty string, and the terminal newline */ fprintf(print_spu.fp, "%s\n", text); fflush(print_spu.fp); } /* Pass the signal through to the old signal handler, if any. */ if (print_spu.old_signal_handler != NULL) { (*print_spu.old_signal_handler)(signum); } }