Esempio n. 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;
}
Esempio n. 2
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 = NULL;
  FILE *output = NULL;
  snet_stream_t *input_stream = NULL;
  snet_stream_t *output_stream = NULL;
  snet_info_t *info;
  snet_locvec_t *locvec;
  snetin_label_t *labels = NULL;
  snetin_interface_t *interfaces = NULL;

  if (0 == SNetInParseOptions(argc, argv, &input, &output)) {
    return 0;
  }

  /* 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);

  input_stream = SNetStreamCreate(0);
  output_stream = fun(input_stream, info, 0);
  output_stream = SNetRouteUpdate(info, output_stream, 0);

  SNetDistribStart();

  if (SNetDistribIsRootNode()) {
    /* 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;
}