int main(int argc, char **argv) { if(argc>1) { int i; for(i=1;i<argc;i++) { if(strcmp("-p",*(argv+i))==0) // push updates PUSH_UPDATES = 1; else { printf("Unrecognized argument.\n"); printf("Usage ---------------------->\n" "-p\tActivate Push Updates\n"); exit(EXIT_FAILURE); } } } start_listener(); // handles incoming messages. see listener.c listener.h for more info. start_sender(); // handles outgoing messages. see sender.c, sender.h "" "" "" clients_cache = new_cache(-1); for(;;) { } return 0; }
int main(int argc, char **argv) { pthread_mutex_init(&heart,NULL); // protects the beat counter pthread_mutex_init(&con,NULL); // protects the default server connection value from badly-timed updates pthread_mutex_init(&queue,NULL); // protects the cache from poorly-timed access cache = new_cache(-1); // creates query cache with default size (when arg is < 0, MAX_SIZE used. See cache.h clients = new_cache(100); // creates a cache of connected clients pending_queries = new_cache(100); // creates a cache of pending updates. When client requests a value that // must be retrieved from the database, the client's callback information is stored here until the DB query // is completed. /* Read command-line arguments */ if(argc>1) { int x; for(x=1;x<argc;x++) { // database addr if(strcmp("-d",*(argv + x))==0) { if(strlen( *(argv + x + 1) )>0) { strcpy(database_addr,*(argv + x + 1)); } else { usage(); } } else if(strcmp("-P",*(argv + x))==0) { pthread_t pull_thread; pthread_create(&pull_thread,NULL,(void *)pull_updater,(void *)cache); } else if(strcmp("-C",*(argv + x))==0) { pthread_t updater; pthread_create(&updater,NULL,(void *)caco,NULL); } else if(strcmp("-n",*(argv + x))==0) { strcpy(neighbor_addr,*(argv + x + 1)); } else if(strcmp("-S",*(argv + x))==0) { server_id = atoi(*(argv + x + 1)); } } } else { usage(); } start_listener(); start_sender(); // wait for all threads to come online sleep(2); while(server_id==0); /* Begin the heartbeat between this host and its neighbor */ char h_msg[CONTENT_MAX]; sprintf(h_msg,"%s,%d,NULL",hea,server_id); while(sender_queue==NULL); message_host(neighbor_addr,h_msg); /* End Heartbeat. Should be cycling now */ for(;;) { // neighbor recovered! if(beat_count == 0 && solo == 1) { // if we took over the Caco process for another server, shut it down now if(!CACO_SERVER) { printf("\n\n** ATTENTION! ***************************\n"); printf("****** CANCELING CACO UPDATER ************\n"); printf("******************************************\n\n"); pthread_cancel(caco_thread); } solo = 0; printf("\n\n** ATTENTION! ****************************\n"); printf("** NEIGHBOR BACK ONLINE *******************\n"); printf("*******************************************\n\n"); struct cache_entry *client; int x; /* Inform all adopted clients that the other server is back online */ for(x=0;x<clients->length;x++) { client = get_index(x,clients); // only message clients of the other front-end server if(client->server_id==server_id) continue; char r_msg[CONTENT_MAX]; sprintf(r_msg,"%s,%s,NULL",rea,neighbor_addr); message_host(client->value,r_msg); } } pthread_mutex_lock(&heart); beat_count++; // we'll wait until HEARTBEAT_DELAY has elapsed before we declare the other // server dead. // assume we've lost the neighbor if(beat_count==HEARTBEAT_DELAY) { // If we need to take over the Caco process for the other server, start it now. if(!CACO_SERVER) { printf("\n\n** NOTICE *************************\n"); printf("** taking over caco updater *******\n"); printf("***********************************\n\n"); pthread_create(&caco_thread,NULL,caco,NULL); } // sets the state-var solo, indicating that we are running alone solo = 1; printf("\n\n** NOTICE *************************.\n"); printf("** LOST CONTACT WITH NEIGHBOR SERVER **\n"); printf("** ASSUMING RESPONSIBILITY ************\n"); printf("***************************************\n\n"); struct cache_entry *client; int x; /* Adopt the clients from the other server. Notify them of the changes */ for(x=0;x<clients->length;x++) { client = get_index(x,clients); // only message clients of the other front-end server if(client->server_id==server_id) continue; char r_msg[CONTENT_MAX]; char addr[CONTENT_MAX]; get_local_addr(addr); sprintf(r_msg,"%s,%s,NULL",rea,addr); message_host(client->value,r_msg); } } pthread_mutex_unlock(&heart); sleep(1); } return 0; }
//main function for the program, listens on stdin for commands int main(int argc, char** argv){ char input[80]; //buffer for commands //start the voice process before doing anything significant setup_voice_control(); //default the flags to not started mic_kill_flag = 1; speaker_kill_flag = 1; period = setup_codecs(); //setup the codecs and get the period size //handler loop, runs until program is killed while(!global_kill){ if(fgets(input, 80, stdin)){ //get a command from stdin printf("Echo: %s",input); char* token = strtok(input, "\n"); //split string to ignore newline token = strtok(input, " "); //split string on whitespace, get command //Start Command if(0 == strcmp(token, "start")){ int direction = 0; char* addr = 0; int port = 0; unsigned int rate = 0; int channels = 0; while(token = strtok(0," ")){ //parse next token if(0 == strcmp(token, "mic")) direction = 1; //direction is input only else if(0 == strcmp(token, "spk")) direction = 2; //output only else if(0 == strcmp(token, "both")) direction = 3; //both directions else if(0 == strcmp(token, "-host")) addr = strtok(0," "); //get hostname from next input else if(0 == strcmp(token, "-port")) port = atoi(strtok(0," ")); //parse the integer port from next input else if(0 == strcmp(token, "-rate")) rate = atoi(strtok(0," ")); //parse the rate else if(0 == strcmp(token, "-ch")) channels = atoi(strtok(0," ")); //parse the channel count else{ printf("Audio Controller: Bad command argument: \"%s\"\n",token); continue; } } //handle missing information via defaults (rate/chn are fixed with codecs addr = addr?: DEFAULT_ADDR; port = port?: DEFAULT_PORT; //rate = rate?: DEFAULT_RATE; //channels = channels?: DEFAULT_CHNS; rate = DEFAULT_RATE; channels = DEFAULT_CHNS; printf("Rate: %d, Channels: %d, Period: %d\n", rate, channels, period); //setup the speaker if((direction & 2) && speaker_kill_flag){ snd_pcm_t* handle = start_snd_device(period, rate, (channels==2), PLAYBACK_DIR); //start the device audiobuffer* spk_buf = create_speaker_thread(handle, period, 2*channels); //spawn the speaker thread reciever_kill_flag = 0; //reset the kill flag if(start_reciever(port, spk_buf, &reciever_kill_flag)){ //start the reciever thread printf("Audio Controller: Could not start speaker\n"); speaker_kill_flag = 1; //kill the mic device thread } printf("Audio Controller: Started speaker server\n"); } //setup the mic if((direction & 1) && mic_kill_flag){ if(vc_flag){ vc_flag = 0; //stop voice control printf("Audio Controller: Stopping VC for comms setup\n"); wait(TIMEOUT); //let thread die } snd_pcm_t* handle = start_snd_device(period, rate, (channels==2), CAPTURE_DIR); //start the device sender_handle* sndr = start_sender(addr, port, 0, 0); //setup partial sender create_mic_thread(handle, sndr, period, 2*(channels), 0); //spawn the thread printf("Audio Controller: Started microphone transmission\n"); } //Stop Command } else if(0 == strcmp(token, "stop")){
void measure_signalling() { start_sender(); }