static int THRinit(void) { int i = 0; THRdata[0] = (void *) file_wastream(stdout, "stdout"); THRdata[1] = (void *) file_rastream(stdin, "stdin"); for (i = 0; i < THREADS; i++) { GDKthreads[i].tid = i + 1; } return 0; }
int main(int argc, char **argv) { int i; char *err = NULL; /*char name[MYBUFSIZ + 1];*/ char hostname[1024]; static SOCKET sockfd; Actuator ac = NULL; int option_index = 0; static struct option long_options[13] = { { "port", 1, 0, 'p' }, { "actuator", 1, 0, 'a' }, { "active", 0, 0, 'a' }, { "passive", 0, 0, 'p' }, { "statistics", 0, 0, 's' }, { "protocol", 1, 0, 'p' }, { "events", 1, 0, 'e' }, { "host", 1, 0, 'h' }, { "help", 1, 0, '?' }, { "timestamp", 0, 0, 't' }, { "trace", 0, 0, 't' }, { 0, 0, 0, 0 } }; THRdata[0] = (void *) file_wastream(stdout, "stdout"); THRdata[1] = (void *) file_rastream(stdin, "stdin"); for (i = 0; i < THREADS; i++) { GDKthreads[i].tid = i + 1; } for (;;) { /* int option_index=0;*/ int c = getopt_long(argc, argv, "p:a:a:p:s:p:e:h:?:t:t:0", long_options, &option_index); if (c == -1) break; switch (c) { case 't': if (strcmp(long_options[option_index].name, "trace") == 0) { trace = optarg ? atol(optarg) : 1; } else if (strcmp(long_options[option_index].name, "timestamp") == 0) { timestamp = optarg ? atol(optarg) : 1; } else { usage(); exit(0); } break; case 'e': if (strcmp(long_options[option_index].name, "events") == 0) { events = optarg ? atol(optarg) : 1; } else { usage(); exit(0); } break; case 'a': if (strcmp(long_options[option_index].name, "active") == 0) { mode = ACTIVE; break; } else actuator = optarg ? optarg : "dummy"; break; case 'p': if (strcmp(long_options[option_index].name, "port") == 0) { port = optarg ? atol(optarg) : -1; break; } else if (strcmp(long_options[option_index].name, "passive") == 0) { mode = PASSIVE; break; } else if (strcmp(long_options[option_index].name, "protocol") == 0) { if (strcmp(optarg, "TCP") == 0 || strcmp(optarg, "tcp") == 0) { protocol = TCP; break; } if (strcmp(optarg, "UDP") == 0 || strcmp(optarg, "udp") == 0) { protocol = UDP; break; } } break; case 's': if (strcmp(long_options[option_index].name, "statistics") == 0) { statistics = optarg ? atol(optarg) : 100; break; } else { usage(); exit(0); } break; case 'h': host = optarg; break; case '?': default: usage(); exit(0); } } signal(SIGABRT, stopListening); #ifdef SIGPIPE signal(SIGPIPE, stopListening); #endif #ifdef SIGHUP signal(SIGHUP, stopListening); #endif signal(SIGTERM, stopListening); signal(SIGINT, stopListening); /* display properties */ if (trace) { mnstr_printf(ACout, "--host=%s\n", host); mnstr_printf(ACout, "--port=%d\n", port); mnstr_printf(ACout, "--actuator=%s\n", actuator); mnstr_printf(ACout, "--events=%d\n", events); mnstr_printf(ACout, "--timestamp=%d\n", timestamp); mnstr_printf(ACout, "--statistics=%d\n", statistics); mnstr_printf(ACout, "--%s\n", mode == ACTIVE ? "active" : "passive"); } strncpy(hostname, host, 1024); if (strcmp(host, "localhost") == 0) gethostname(hostname, 1024); host = hostname; ac = ACnew(actuator); /*name[0] = 0;*/ err = NULL; if (mode == PASSIVE) { ac->fromServer = udp_rastream(host, port, actuator); if (ac->fromServer == 0) { mnstr_printf(ACout, "Failed to access stream %s:%d\n", host, port); return 0; } consumeStream(ac); #ifdef _DEBUG_ACTUATOR_ mnstr_printf(ACout, "stream consumed\n"); #endif } if (mode == ACTIVE) do { err = socket_client_connect(&sockfd, host, port); if (err) { mnstr_printf(ACout, "actuator connect fails: %s\n", err); MT_sleep_ms(1000); } } while (err); if (mode == PASSIVE) { err = socket_server_listen(sockfd, &(ac->newsockfd)); if (err) { mnstr_printf(ACout, "ACTUATOR:server listen fails:%s\n", err); return 0; } } do { if (mode == PASSIVE) { #ifdef _DEBUG_ACTUATOR_ mnstr_printf(ACout, "Actuator listens\n"); #endif if (protocol == UDP) ac->fromServer = udp_rastream(host, port, actuator); else ac->fromServer = socket_rastream(ac->newsockfd, actuator); if (ac->fromServer == NULL) { perror("Actuator: Could not open stream"); mnstr_printf(ACout, "stream %s.%d.%s\n", host, port, actuator); return 0; } consumeStream(ac); } else if (mode == ACTIVE) { #ifdef _DEBUG_ACTUATOR_ mnstr_printf(ACout, "Actuator connects\n"); #endif err = socket_client_connect(&(ac->newsockfd), host, port); if (err) { mnstr_printf(ACout, "ACTUATOR:start client:%s\n", err); continue; } #ifdef _DEBUG_ACTUATOR_ mnstr_printf(ACout, "Initialize stream\n"); #endif if (protocol == UDP) ac->fromServer = udp_rastream(host, port, actuator); else ac->fromServer = socket_rastream(ac->newsockfd, actuator); if (ac->fromServer == NULL) { perror("Actuator: Could not open stream"); mnstr_printf(ACout, "stream %s.%d.%s\n", host, port, actuator); continue; } } consumeStream(ac); } while (mode == PASSIVE && (events == -1 || tuples < events)); socket_close(sockfd); terminate(ac); return 0; }
int main(int argc, char **argv) { MT_Id pid; int i, j = 0; char *err = NULL; char name[MYBUFSIZ + 1]; char hostname[1024]; Sensor se = NULL; static SOCKET sockfd; static struct option long_options[18] = { { "increment", 0, 0, 'i' }, { "batch", 1, 0, 'b' }, { "columns", 1, 0, 'c' }, { "client", 0, 0, 'c' }, { "port", 1, 0, 'p' }, { "protocol", 1, 0, 'p' }, { "timestamp", 0, 0, 't' }, { "time", 1, 0, 't' }, { "events", 1, 0, 'e' }, { "sensor", 1, 0, 's' }, { "server", 0, 0, 's' }, { "replay", 0, 0, 'r' }, { "delay", 1, 0, 'd' }, { "file", 1, 0, 'f' }, { "host", 1, 0, 'h' }, { "trace", 0, 0, 't' }, { "help", 1, 0, '?' }, { 0, 0, 0, 0 } }; THRdata[0] = (void *) file_wastream(stdout, "stdout"); THRdata[1] = (void *) file_rastream(stdin, "stdin"); for (i = 0; i < THREADS; i++) { GDKthreads[i].tid = i + 1; } for (;;) { int option_index = 0; int c = getopt_long(argc, argv, "i:b:c:c:p:p:t:t:e:s:s:r:d:f:h:t:?:0", long_options, &option_index); if (c == -1) break; switch (c) { case 'b': batchsize = optarg ? atol(optarg) : -1; if (batchsize <= 0) { mnstr_printf(SEout, "Illegal batch %d\n", batchsize); exit(0); } break; case 'c': if (strcmp(long_options[option_index].name, "client") == 0) { server = 0; break; } columns = optarg ? atol(optarg) : -1; if (columns <= 0) { mnstr_printf(SEout, "Illegal columns %d\n", columns); exit(0); } break; case 'd': delay = optarg ? atol(optarg) : -1; if (delay < 0) { mnstr_printf(SEout, "Illegal delay %d\n", delay); exit(0); } break; case 'i': autoincrement = optarg ? atol(optarg) : 0; if (autoincrement < 0) { mnstr_printf(SEout, "Illegal increment %d\n", autoincrement); exit(0); } break; case 't': if (strcmp(long_options[option_index].name, "timestamp") == 0) { timestamp = 1; break; } if (strcmp(long_options[option_index].name, "time") == 0) { timecolumn = optarg ? atol(optarg) : 0; break; } if (strcmp(long_options[option_index].name, "trace") == 0) { trace = optarg ? atol(optarg) : 1; } else { usage(); exit(0); } break; case 'f': datafile = optarg && *optarg? optarg:0; break; case 'e': if (strcmp(long_options[option_index].name, "events") == 0) { events = optarg ? atol(optarg) : -1; if (events < -1) { mnstr_printf(SEout, "illegal events value, reset to -1\n"); events = -1; } else if (events == 0) { mnstr_printf(SEout, "Illegal events value %d\n", events); exit(0); } break; } else { usage(); exit(0); } break; case 'r': replay= 1; break; case 's': if (strcmp(long_options[option_index].name, "sensor") == 0) { sensor = optarg; break; } if (strcmp(long_options[option_index].name, "server") == 0) { server = 1; break; } else { usage(); exit(0); } break; case 'p': if (strcmp(long_options[option_index].name, "protocol") == 0) { char *name= optarg? optarg: "xyz"; if (strcmp(name, "TCP") == 0 || strcmp(name, "tcp") == 0) { protocol = TCP; break; } if (strcmp(name, "UDP") == 0 || strcmp(name, "udp") == 0) { protocol = UDP; break; } if (strcmp(name, "CSV") == 0 || strcmp(name, "csv") == 0) { protocol = CSV; break; } if (strcmp(name, "debug") == 0) { protocol = DEB; break; } } if (strcmp(long_options[option_index].name, "port") == 0) { port = optarg ? atol(optarg) : -1; #ifdef SENSOR_DEBUG mnstr_printf(SEout, "#PORT : %d\n", port); #endif break; } else { usage(); exit(0); } break; case 'h': host = optarg; break; case '?': default: usage(); exit(0); } } signal(SIGABRT, stopSend); #ifdef SIGPIPE signal(SIGPIPE, stopSend); #endif #ifdef SIGHUP signal(SIGHUP, stopSend); #endif signal(SIGTERM, stopSend); signal(SIGINT, stopSend); /* display properties */ if (trace) { mnstr_printf(SEout, "--host=%s\n", host); mnstr_printf(SEout, "--port=%d\n", port); mnstr_printf(SEout, "--sensor=%s\n", sensor); mnstr_printf(SEout, "--columns=%d\n", columns); mnstr_printf(SEout, "--autoincrement=%d\n", autoincrement); mnstr_printf(SEout, "--timestamp=%d\n", timestamp); mnstr_printf(SEout, "--time=%d\n", timecolumn); mnstr_printf(SEout, "--events=%d\n", events); mnstr_printf(SEout, "--batch=%d\n", batchsize); mnstr_printf(SEout, "--replay=%d\n", replay); mnstr_printf(SEout, "--delay=%d\n", delay); mnstr_printf(SEout, "--protocol %s\n", protocolname[protocol]); mnstr_printf(SEout, "--trace=%d\n", trace); mnstr_printf(SEout, "--server=%d\n", server); mnstr_printf(SEout, "--client=%d\n", server); if (datafile) mnstr_printf(SEout, "--input=%s\n", datafile); } estimateOverhead(); strncpy(hostname, host, 1024); if (strcmp(host, "localhost") == 0) gethostname(hostname, 1024); host = hostname; /* * We limit the protocols for the time being to what can be * considered a safe method. */ if (protocol == DEB) { /* save event stream in a file */ Sensor se = SEnew(sensor); if (events == -1 || batchsize != 1) { printf("Provide an event limit using --events=<nr> and --batch=1\n"); return 0; } if (datafile) se->toServer = open_wastream(datafile); else se->toServer = file_wastream(stdout, "stdout"); produceStream(se); } if (protocol == UDP) { Sensor se = SEnew(sensor); se->toServer = udp_wastream(host, port, sensor); if (se->toServer == NULL) { perror("Sensor: Could not open stream"); mnstr_printf(SEout, "#stream %s.%d.%s\n", host, port, sensor); return 0; } produceStream(se); } if (protocol == TCP) { if (server && (err = socket_server_connect(&sockfd, port))) { mnstr_printf(SEout, "#SENSOR:start server:%s\n", err); return 0; } do { int createThread = 0; snprintf(name, MYBUFSIZ - (strlen(sensor) + sizeof(j)), "%s%d", sensor, j++); se = SEnew(name); name[0] = 0; err = NULL; if (server) { #ifdef SENSOR_DEBUG mnstr_printf(SEout, "#listen %s as server is %d \n", se->name, server); #endif err = socket_server_listen(sockfd, &(se->newsockfd)); if (err) { mnstr_printf(SEout, "#SENSOR:server listen:%s\n", err); break; } } else { #ifdef SENSOR_DEBUG mnstr_printf(SEout, "#%s is client \n", se->name); #endif err = socket_client_connect(&(se->newsockfd), host, port); if (err) { mnstr_printf(SEout, "#SENSOR:client start:%s\n", err); break; } } se->toServer = socket_wastream(se->newsockfd, se->name); if (se->toServer == NULL) { perror("Sensor: Could not open stream"); mnstr_printf(SEout, "#stream %s.%d.%s\n", host, port, sensor); socket_close(se->newsockfd); return 0; } if (server) { createThread = MT_create_thread(&pid, (void (*)(void *))produceServerStream, se, MT_THR_DETACHED); #ifdef SENSOR_DEBUG if (createThread) mnstr_printf(SEout, "#Create thread is : %d \n", createThread); #else (void) createThread; #endif } else { /* client part */ produceServerStream(se); } } while (server); if (server) socket_close(sockfd); if (se) shutdown(se->newsockfd, SHUT_RDWR); } return 0; }