/** * 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; }
/** * 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; }
snet_stream_t *SNetFeedbackDet( snet_stream_t *input, snet_info_t *info, int location, snet_variant_list_t *back_patterns, snet_expr_list_t *guards, snet_startup_fun_t box_a ) { snet_stream_t *output; snet_locvec_t *locvec; locvec = SNetLocvecGet(info); SNetLocvecFeedbackEnter(locvec); input = SNetRouteUpdate(info, input, location); if(SNetDistribIsNodeLocation(location)) { snet_stream_t *into_op, *from_op; snet_stream_t *back_bufin, *back_bufout; fbbuf_arg_t *fbbarg; fbcoll_arg_t *fbcarg; fbdisp_arg_t *fbdarg; /* create streams */ into_op = SNetStreamCreate(0); output = SNetStreamCreate(0); back_bufout = SNetStreamCreate(FEEDBACK_BACKCHAN_CAPACITY); #ifndef FEEDBACK_OMIT_BUFFER back_bufin = SNetStreamCreate(0); /* create the feedback buffer */ fbbarg = SNetMemAlloc( sizeof( fbbuf_arg_t)); fbbarg->in = back_bufin; fbbarg->out = back_bufout; fbbarg->out_capacity = FEEDBACK_BACKCHAN_CAPACITY; SNetThreadingSpawn( SNetEntityCreate( ENTITY_fbbuf, location, locvec, "<fbbuf>", FeedbackBufTask, (void*)fbbarg) ); #else back_bufin = back_bufout; #endif /* create the feedback collector */ fbcarg = SNetMemAlloc( sizeof( fbcoll_arg_t)); fbcarg->in = input; fbcarg->fbi = back_bufout; fbcarg->out = into_op; SNetThreadingSpawn( SNetEntityCreate( ENTITY_fbcoll, location, locvec, "<fbcoll>", FeedbackCollTask, (void*)fbcarg) ); /* create the instance network */ from_op = box_a(into_op, info, location); from_op = SNetRouteUpdate(info, from_op, location); /* create the feedback dispatcher */ fbdarg = SNetMemAlloc( sizeof( fbdisp_arg_t)); fbdarg->in = from_op; fbdarg->fbo = back_bufin; fbdarg->out = output; fbdarg->back_patterns = back_patterns; fbdarg->guards = guards; SNetThreadingSpawn( SNetEntityCreate( ENTITY_fbdisp, location, locvec, "<fbdisp>", FeedbackDispTask, (void*)fbdarg) ); } else { SNetVariantListDestroy(back_patterns); SNetExprListDestroy(guards); output = box_a(input, info, location); output = SNetRouteUpdate(info, output, location); } SNetLocvecFeedbackLeave(locvec); return( output); }
/* Feedback creation function */ snet_stream_t *SNetFeedback( snet_stream_t *input, snet_info_t *info, int location, snet_variant_list_t *back_patterns, snet_expr_list_t *guards, snet_startup_fun_t box_a) { snet_stream_t *output; node_t *node; feedback_arg_t *farg; snet_locvec_t *locvec; int detlevel; trace(__func__); if (SNetFeedbackDeterministic()) { return SNetDripBack(input, info, location, back_patterns, guards, box_a); } detlevel = SNetDetSwapLevel(0); locvec = SNetLocvecGet(info); SNetLocvecFeedbackEnter(locvec); input = SNetRouteUpdate(info, input, location); if (SNetDistribIsNodeLocation(location)) { output = SNetStreamCreate(0); node = SNetNodeNew(NODE_feedback, &input, 1, &output, 1, SNetNodeFeedback, SNetStopFeedback, SNetTermFeedback); farg = NODE_SPEC(node, feedback); /* fill in the node argument */ farg->input = input; farg->output = output; farg->back_patterns = back_patterns; farg->guards = guards; farg->stopping = 0; /* Create the instance network */ farg->instance = SNetNodeStreamCreate(node); farg->feedback = (*box_a)(farg->instance, info, location); farg->feedback = SNetRouteUpdate(info, farg->feedback, location); /* Feedback loop should end at this node. */ assert(STREAM_DEST(farg->feedback) == NULL); STREAM_DEST(farg->feedback) = node; /* Create two self-referencing streams. */ farg->selfref2 = SNetNodeStreamCreate(node); STREAM_DEST(farg->selfref2) = node; farg->selfref4 = SNetNodeStreamCreate(node); STREAM_DEST(farg->selfref4) = node; farg->entity = SNetEntityCreate( ENTITY_fbdisp, location, locvec, "<feedback>", NULL, (void*)farg); } else { SNetExprListDestroy( guards); SNetVariantListDestroy(back_patterns); output = input; } SNetLocvecFeedbackLeave(locvec); SNetDetSwapLevel(detlevel); return output; }