Example #1
0
/**
 * Main starting entry point of the SNet program
 */
int SNetInRun(int argc, char **argv,
              char *static_labels[], int number_of_labels,
              char *static_interfaces[], int number_of_interfaces,
              snet_startup_fun_t fun)
{
  FILE *input = stdin;
  FILE *output = stdout;
  snet_stream_t *input_stream = NULL;
  snet_stream_t *output_stream = NULL;
  int i = 0;
  snet_info_t *info = NULL;
  snet_locvec_t *locvec;
  snetin_label_t *labels = NULL;
  snetin_interface_t *interfaces = NULL;
  char *brk;
  char addr[256];
  int len;
  int port;
  
  /* Parse argv: */
  for(i = 1; i < argc; i++) {
    if(strcmp(argv[i], "-h") == 0) {
      /* Help */
      SNetRuntimeHelpText();

      if(input != stdin && input != NULL) {
	SNetInClose(input);
      }

      if(output != stdout && output != NULL) {
	SNetInClose(output);
      }

      return 0;

    } else if(strcmp(argv[i], "-i") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from file */
      i = i + 1;
      input =  SNetInOpenFile(argv[i], "r");
    } else if(strcmp(argv[i], "-I") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from socket */
      i = i + 1;
      input = SNetInOpenInputSocket(atoi(argv[i]));
    } else if(strcmp(argv[i], "-o") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to file */
      i = i + 1;
      output = SNetInOpenFile(argv[i], "w");
    } else if(strcmp(argv[i], "-O") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to socket */
      i = i + 1;
      brk = strchr(argv[i], ':');

      if(brk == NULL) {
	output = NULL;

	SNetUtilDebugFatal("Could not parse URL!\n");
      }

      len = brk - argv[i];
      strncpy((char *)addr, (const char *)argv[i], len);
      addr[len] = '\0';
      port = atoi(brk + 1);

      output = SNetInOpenOutputSocket(addr, port);
    }
  }

  if(input == NULL) {

    if(output != stdout && output != NULL) {
      SNetInClose(output);
    }

    SNetUtilDebugFatal("");
  }

  if(output == NULL) {

    if(input != stdin && input != NULL) {
      SNetInClose(input);
    }

    SNetUtilDebugFatal("");
  }


  /* Actual SNet network interface main: */

  /* check for number of interfaces */
  if (0 == number_of_interfaces) {
    SNetUtilDebugNotice("No language interfaces were specified by the source program!");
    exit(1);
  }
  
  labels     = SNetInLabelInit(static_labels, number_of_labels);
  interfaces = SNetInInterfaceInit(static_interfaces, number_of_interfaces);

  info = SNetInfoInit();


  SNetDistribInit(argc, argv, info);

  (void) SNetThreadingInit(argc, argv);

  //SNetObserverInit(labels, interfaces); 

  locvec = SNetLocvecCreate();
  SNetLocvecSet(info, locvec);

  SNetDistribStart();

  if (SNetDistribIsRootNode()) {
    input_stream = SNetStreamCreate(0);
    output_stream = fun(input_stream, info, 0);
    output_stream = SNetRouteUpdate(info, output_stream, 0);
    
    /* create output thread */
    SNetInOutputInit(output, labels, interfaces, output_stream);

    /* create input thread */
    SNetInInputInit(input, labels, interfaces, input_stream); 
    
    SNetRuntimeStartWait(input_stream, info, output_stream);  
    
    /* tell the threading layer that it is ok to shutdown,
     and wait until it has stopped such that it can be cleaned up */
    (void) SNetThreadingStop();
  }

  (void) SNetThreadingCleanup();
  SNetInfoDestroy(info);

  SNetLocvecDestroy(locvec);

  /* destroy observers */
  //SNetObserverDestroy();

  SNetInLabelDestroy(labels);
  SNetInInterfaceDestroy(interfaces);

  if(input != stdin) {
    SNetInClose(input);
  }

  if(output != stdout) {
    SNetInClose(output);
  }

  return 0;
}
Example #2
0
/**
 * Process commandline options.
 */
static int SNetInParseOptions(int argc, char **argv, FILE **input_ptr, FILE **output_ptr)
{
  FILE *input = stdin;
  FILE *output = stdout;
  int i = 0;
  char *brk;
  char addr[256];
  int len;
  int port;

  /* Parse argv: */
  for(i = 1; i < argc; i++) {
    if(strcmp(argv[i], "-h") == 0) {
      /* Help */
      SNetRuntimeHelpText();

      if(input != stdin && input != NULL) {
	SNetInClose(input);
      }

      if(output != stdout && output != NULL) {
	SNetInClose(output);
      }

      return false;
    } else if (strcmp(argv[i], "-V") == 0) {
      SNetRuntimeVersion();
      if (input != stdin && input != NULL) {
	SNetInClose(input);
      }
      if (output != stdout && output != NULL) {
	SNetInClose(output);
      }
      return false;
    } else if(strcmp(argv[i], "-i") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from file */
      i = i + 1;
      input =  SNetInOpenFile(argv[i], "r");
    } else if(strcmp(argv[i], "-I") == 0 && input == stdin && i + 1 <= argc) {
      /* Input from socket */
      i = i + 1;
      input = SNetInOpenInputSocket(atoi(argv[i]));
    } else if(strcmp(argv[i], "-o") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to file */
      i = i + 1;
      output = SNetInOpenFile(argv[i], "w");
    } else if(strcmp(argv[i], "-O") == 0 && output == stdout && i + 1 <= argc) {
      /* Output to socket */
      i = i + 1;
      brk = strchr(argv[i], ':');

      if(brk == NULL) {
	output = NULL;

	SNetUtilDebugFatal("Could not parse URL!\n");
      }

      len = brk - argv[i];
      strncpy((char *)addr, (const char *)argv[i], len);
      addr[len] = '\0';
      port = atoi(brk + 1);

      output = SNetInOpenOutputSocket(addr, port);
    }
  }

  *input_ptr = input;
  *output_ptr = output;

  if(input == NULL) {

    if(output != stdout && output != NULL) {
      SNetInClose(output);
    }

    SNetUtilDebugFatal("Could not open input");
  }

  if(output == NULL) {

    if(input != stdin && input != NULL) {
      SNetInClose(input);
    }

    SNetUtilDebugFatal("Could not open output");
  }

  return true;
}