const char* env_init() { /* Setup the connection */ int envState = kEnvInit; unsigned int theTaskSpecLength = 0; unsigned int offset = 0; if (theBuffer.capacity == 0){ rlBufferCreate(&theBuffer, 65536); } /* env init-specific data */ rlBufferClear(&theBuffer); rlSendBufferData(rlGetEnvironmentConnection(), &theBuffer, envState); rlBufferClear(&theBuffer); rlRecvBufferData(rlGetEnvironmentConnection(), &theBuffer, &envState); assert(envState == kEnvInit); offset = 0; offset = rlBufferRead(&theBuffer, offset, &theTaskSpecLength, 1, sizeof(int)); if (theTaskSpecLength > 0) { if (theTaskSpec != 0) { free(theTaskSpec); theTaskSpec = 0; } /*Read the task spec off the wire and then add \0 at the end, to be sure? */ /*Are we actually stripping the \0 before we send it or is this just for good measure */ theTaskSpec = (char*)calloc(theTaskSpecLength+1, sizeof(char)); offset = rlBufferRead(&theBuffer, offset, theTaskSpec, theTaskSpecLength, sizeof(char)); theTaskSpec[theTaskSpecLength] = '\0'; } return theTaskSpec; }
/* Send the task spec to the agent */ void agent_init(const char * theTaskSpec) { int agentState = kAgentInit; unsigned int theTaskSpecLength = 0; unsigned int offset = 0; if (theTaskSpec != NULL) theTaskSpecLength = strlen(theTaskSpec); if (theBuffer.capacity == 0) rlBufferCreate(&theBuffer, 65536); /* send across agent_init specific data */ rlBufferClear(&theBuffer); offset = 0; /* Strings are always preceeded by their length, and do not include their null terminating character */ offset = rlBufferWrite(&theBuffer, offset, &theTaskSpecLength, 1, sizeof(int)); if (theTaskSpecLength > 0) { offset = rlBufferWrite(&theBuffer, offset, theTaskSpec, theTaskSpecLength, sizeof(char)); } rlSendBufferData(rlGetAgentConnection(), &theBuffer, agentState); /* Receive the receipt from the Client, to ensure that AgentInit has been completed */ rlBufferClear(&theBuffer); rlRecvBufferData(rlGetAgentConnection(), &theBuffer, &agentState); assert(agentState == kAgentInit); }
int main(int argc, char** argv) { int theConnection = 0; const char *usage = "The following environment variables are used by the agent to control its function:\n" "RLGLUE_HOST : If set the agent will use this ip or hostname to connect to rather than %s\n" "RLGLUE_PORT : If set the agent will use this port to connect on rather than %d\n"; struct hostent *host_ent; char* host = kLocalHost; short port = kDefaultPort; char* envptr = 0; if (argc > 1) { fprintf(stderr, usage, kLocalHost, kDefaultPort); exit(1); } host = getenv("RLGLUE_HOST"); if (host == 0) { host = kLocalHost; } envptr = getenv("RLGLUE_PORT"); if (envptr != 0) { port = strtol(envptr, 0, 10); if (port == 0) { port = kDefaultPort; } } if (isalpha(host[0])) { /*This method is apparently deprecated, we should update at some point*/ host_ent = gethostbyname(host); if(host_ent==0){ fprintf(stderr,"Couldn't find IP address for host: %s\n",host); exit(55); } host = inet_ntoa(*(struct in_addr*)host_ent->h_addr_list[0]); } fprintf(stdout, "RL-Glue C Agent Codec Version %s, Build %s\n\tConnecting to host=%s on port=%d...\n", VERSION,__rlglue_get_codec_svn_version(),host, port); fflush(stdout); /* Allocate what should be plenty of space for the buffer - it will dynamically resize if it is too small */ rlBufferCreate(&theBuffer, 4096); theConnection = rlWaitForConnection(host, port, kRetryTimeout); fprintf(stdout, "\tRL-Glue C Agent Codec :: Connected\n"); rlBufferClear(&theBuffer); rlSendBufferData(theConnection, &theBuffer, kAgentConnection); runAgentEventLoop(theConnection); rlClose(theConnection); rlBufferDestroy(&theBuffer); return 0; }
void rlCloseEnvironmentConnection() { rlBuffer theBuffer = {0}; rlBufferCreate(&theBuffer, 8); rlSendBufferData(envConnection, &theBuffer, kRLTerm); rlClose(envConnection); envConnection = 0; rlBufferDestroy(&theBuffer); }
static void forceConnection() { struct hostent *host_ent; char* host = kLocalHost; short port = kDefaultPort; char* envptr = 0; if (theExperimentConnection == 0) { host = getenv("RLGLUE_HOST"); if (host == 0) { host = kLocalHost; } envptr = getenv("RLGLUE_PORT"); if (envptr != 0) { port = strtol(envptr, 0, 10); if (port == 0) { port = kDefaultPort; } } if (isalpha(host[0])) { /*This method is apparently deprecated, we should update at some point*/ host_ent = gethostbyname(host); if(host_ent==0){ fprintf(stderr,"Couldn't find IP address for host: %s\n",host); exit(55); } host = inet_ntoa(*(struct in_addr*)host_ent->h_addr_list[0]); } fprintf(stdout, "RL-Glue C Experiment Codec Version %s, Build %s\n\tConnecting to host=%s on port=%d...\n", VERSION,__rlglue_get_codec_svn_version(),host, port); fflush(stdout); theExperimentConnection = rlWaitForConnection(host, port, kRetryTimeout); fprintf(stdout, "\tRL-Glue C Experiment Codec :: Connected\n"); /* Send the connection type */ atexit(cleanupExperimentAtExit); rlBufferCreate(&clientexp_rlbuffer, 65536); rlBufferClear(&clientexp_rlbuffer); rlSendBufferData(theExperimentConnection, &clientexp_rlbuffer, kExperimentConnection); } }
const char* env_message(const char* inMessage) { int envState = kEnvMessage; unsigned int theInMessageLength = 0; unsigned int theOutMessageLength = 0; unsigned int offset = 0; if (inMessage != NULL) { theInMessageLength = strlen(inMessage); } if (theBuffer.capacity == 0) rlBufferCreate(&theBuffer, 65356); rlBufferClear(&theBuffer); offset = 0; offset = rlBufferWrite(&theBuffer, offset, &theInMessageLength, 1, sizeof(int)); if (theInMessageLength > 0) { offset = rlBufferWrite(&theBuffer, offset, inMessage, theInMessageLength, sizeof(char)); } rlSendBufferData(rlGetEnvironmentConnection(), &theBuffer, envState); rlBufferClear(&theBuffer); rlRecvBufferData(rlGetEnvironmentConnection(), &theBuffer, &envState); assert(envState == kEnvMessage); offset = 0; offset = rlBufferRead(&theBuffer, offset, &theOutMessageLength, 1, sizeof(int)); /*Free and point the old message to null */ if (theOutMessage != 0) { free(theOutMessage); theOutMessage = 0; } /* Allocated memory for the new message, maybe just 1 byte for the terminator */ theOutMessage = (char*)calloc(theOutMessageLength+1, sizeof(char)); /* Fill up the string from the buffer */ if (theOutMessageLength > 0) { offset = rlBufferRead(&theBuffer, offset, theOutMessage, theOutMessageLength, sizeof(char)); } /* Set the terminator */ theOutMessage[theOutMessageLength] = '\0'; return theOutMessage; }