Example #1
0
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);
  }
}
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;
}