Example #1
0
void lifLEDArray::handle_msg() {
  std::string toDo;
  lifMsg nextMsg;
  runThread = true;
  bool sendStatus;
  std::string successMsg;
  while (runThread) {
    //Lock the msgMutex
    pthread_mutex_lock(&msgMutex);
    //Check if msgQueue is empty
    // - If it is empty, wait hear until you receive a signal that
    //   a message has been pushed onto mainMsgQueue
    if (msgQueue.empty()) {
      pthread_cond_wait(&msgQueuePushed, &msgMutex);
    }
    nextMsg = msgQueue.front();
    msgQueue.pop();
    pthread_mutex_unlock(&msgMutex);

    toDo = nextMsg.get_command();
    sendStatus = true;
    successMsg = "";
    try {
      if (toDo == "Initialize") {
        initialize();
      } else if (toDo == "TurnOnLED") {
        successMsg = turn_on_LED(to_num<unsigned int>(nextMsg.get_param_at(0)));
      } else if (toDo == "TurnOffLED") {
        turn_off_LED();
      } else if (toDo == "NextLED") {
        successMsg = turn_on_next_LED();
      } else if (toDo == "ListLEDs") {
        lifMsg LEDmsg;
        LEDmsg.set_target("MainComm");
        LEDmsg.set_source(devName);
        LEDmsg.set_command("Output");
        std::string LEDList;
        LEDList = "";
        std::string sp = " ";
        std::string sc = ";";
        for (int i = 0; i < LEDs.size(); i++)
          LEDList = LEDList + LEDs.at(i)->get_name() + sp + to_string(LEDs.at(i)->get_wavelength()) + sc;
        LEDmsg.add_params(LEDList);
        send_back_msg(LEDmsg);
      } else if (toDo == "Print") {

      } else if (toDo == "StatusReport") {
        sendStatus = false;
      } else {
        default_msg_handling(nextMsg);
      }
      if (sendStatus)
        status_report("ALL", toDo + "SUCCESS" + " " + successMsg);
    } catch (std::string errStr) {
      status_report("ALL", toDo + "ERROR " + errStr);
    }
  }
}
Example #2
0
void lifDIODevice::handle_msg() {
  std::string toDo;
  lifMsg nextMsg;
  runThread = true;
  bool sendStatus;
  std::string successMsg;

  while (runThread) {
    //Lock the msgMutex
    pthread_mutex_lock(&msgMutex);
    //Check if msgQueue is empty
    // - If it is empty, wait hear until you receive a signal that
    //   a message has been pushed onto mainMsgQueue
    if (msgQueue.empty()) {
      pthread_cond_wait(&msgQueuePushed, &msgMutex);
    }
    nextMsg = msgQueue.front();
    msgQueue.pop();
    pthread_mutex_unlock(&msgMutex);

    //PUT CODE TO DO STUFF HERE
    toDo = nextMsg.get_command();
    successMsg = "";
    sendStatus = true;

    try {
      if (toDo == "Initialize") {
        initialize();
      } else if (toDo == "Output") {
        output(nextMsg);
      } else if (toDo == "Input") {
        input(nextMsg);
      } else if (toDo == "High") {
        high(nextMsg);
      } else if (toDo == "Low") {
        low(nextMsg);
      } else if (toDo == "TurnOn") {
        turn_on(nextMsg);
      } else if (toDo == "TurnOff") {
        turn_off(nextMsg);
      } else if (toDo == "GetPinNames") {
        get_pin_names();
      } else if (toDo == "StatusReport") {
        sendStatus = false;
      } else {
        default_msg_handling(nextMsg);
      }
      if (sendStatus)
        status_report("ALL", toDo + "SUCCESS" + " " + successMsg);
    } catch (std::string errStr) {
      status_report("ALL", toDo + "ERROR " + errStr);
    }
  }
}
Example #3
0
/* 'Cleans' the request and calls the appropriate handlers.
 * This function alters the path to prepare it for processpath().
 */
static int
process_request (client_t *client, char *path)
{
   char *p;
   int rc, pathlen;

   pathlen = strlen(path);
   if (!pathlen)
       return 1;

   if (pathlen >= 12 && memcmp(path, "GET ", 4) == 0)
      {
         /* looks like a http request, validate it and update path_p */
         char *http_p = path + pathlen - 9;
         if (memcmp(http_p, " HTTP/1.0", 9) && memcmp(http_p, " HTTP/1.1", 9))
            return 1;
         *http_p = 0;
         pathlen = strlen(path + 4);
         memmove(path, path + 4, pathlen + 1);
         client->http = 1;
      }

   if (*path != '/')
       return 1;

   if (!strcmp(path, "/status"))
      {
         status_report(client);
         return 2;
      }

   /* Check for illegal characters in element */
   if (strcspn(path, ">!@#$%`;|\"\\'<") < pathlen)
      return 1;

   p = strchr(path, '?');
   if (p)
      {
         *p = 0;
         debug_msg("Found subtree %s and %s", path, p+1);
         rc = processfilter(client, p+1);
         if (rc) return 1;
      }

   return 0;
}
Example #4
0
void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(g_fovy,g_xy_aspect,1.0,10000.0);	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluLookAt(g_CameraPos.x, g_CameraPos.y, g_CameraPos.z, g_CameraPos.x + g_CameraForward.x, g_CameraPos.y + g_CameraForward.y, g_CameraPos.z + g_CameraForward.z, 0.0f, 1.0f, 0.0f);
	/*为了在程序打开时能观察到模型,请在此处自定义z轴负方向平移大小*/

	glPushMatrix();
	//call render function
	render();
	glPopMatrix();

	status_report();
	glCheckError("display");
	glutSwapBuffers();	
}
Example #5
0
void
library_computer(void)
{
  string sTemp;

  if (d[8] < 0.0)
    {
      printf("Library Computer inoperable\n");
      return;
    }

  printf("Computer active and awaiting command: ");

  reads(sTemp);
  printf("\n");

  if (! strncmp(sTemp, "0", 1))
    galactic_record();
  else if (! strncmp(sTemp, "1", 1))
    status_report();
  else if (! strncmp(sTemp, "2", 1))
    torpedo_data();
  else if (! strncmp(sTemp, "3", 1))
    nav_data();
  else if (! strncmp(sTemp, "4", 1))
    dirdist_calc();
  else if (! strncmp(sTemp, "5", 1))
    galaxy_map();
  else
    {
      printf("Functions available from Library-Computer:\n\n");
      printf("   0 = Cumulative Galactic Record\n");
      printf("   1 = Status Report\n");
      printf("   2 = Photon Torpedo Data\n");
      printf("   3 = Starbase Nav Data\n");
      printf("   4 = Direction/Distance Calculator\n");
      printf("   5 = Galaxy 'Region Name' Map\n\n");
    }
}
Example #6
0
void mydg_echo( int sockfd, SA *servaddr, socklen_t servlen, SA *cliaddr , socklen_t clilen, char *filename )
{
	int						n, persist_timer_flag=0;
	char					mesg[MAXLINE];
	socklen_t				len, slen, slen1;
	int 					connfd;
	struct sockaddr_in      ss, ss1;
	char 					IPServer[20], IPClient[20];
	FILE 					*ifp;
	ssize_t					bytes;
	char					sendline[MAXLINE], recvline[MAXLINE + 1];
	const int				on=1;
	int 					onlength;

	onlength = sizeof( on );

	printf( "\n******************* CHILD SERVER INITIATED *********************\n" );

	//printf( "Creating Datagram...\n" );
	if( ( connfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) == NULL )
	{
		printf( "socket error\n" );
		exit(1);
	}
	
	getsockopt( sockfd, SOL_SOCKET, SO_DONTROUTE, &on, &onlength ) ;
	setsockopt( connfd, SOL_SOCKET, SO_DONTROUTE, &on, sizeof( on ) );
	printf("Setting connection socket to SO_DONTROUTE..\n", on );
	/* Bind to IPServer and EPHEMERAL PORT and return EPHEMERAL PORT */

	bind( connfd, (SA *) servaddr, sizeof( struct sockaddr_in ) );
	slen = sizeof( ss );

	if( getsockname( connfd, (SA *)&ss, &slen ) < 0 )
	{
		printf( "sockname error\n" );
		exit(1);
	}      

	printf("BOUND SOCKET ADDRESSES : %s\n", inet_ntop( AF_INET, &(ss.sin_addr), IPServer, MAXLINE ));
	printf( "SERVER PORT: %d\n", ntohl(ss.sin_port) );

	/* Connect to IPClient */
	if( connect( connfd, cliaddr, clilen ) < 0 )
	{
		printf( "Error in connecting to server..\n" );
		exit(1);
	}	

	slen1 = sizeof( ss1 ); 

	if( getpeername( connfd, (SA *)&ss1, &slen1 ) < 0 )
	{
		printf( "peername error\n" );
		exit(1);
	}

	inet_ntop( AF_INET, &(ss1.sin_addr), IPClient, MAXLINE );

 	printf( "DESTINATION ADDRESS :  %s\n",IPClient );

	printf( "DESTINATION PORT : %d\n", ss1.sin_port);


	sprintf( mesg, "%d\n", ss.sin_port );
	printf("Sending the Ephemeral port number to client : %s..\n", mesg );

	if( sendto( sockfd, mesg, sizeof( mesg ), 0, cliaddr, clilen) < 0 ) 
	{
		printf("ERROR IN SENDTO : %d ",errno);
		exit(0);
	}	

	printf( "Reading from the newly connected socket..\n" );
	n = read( connfd, mesg, MAXLINE );
	printf("Received data : %s\n",mesg );
	
	if( n > 0 && strcmp( mesg, "ACK\n" ) == 0 )
	{
		printf("ACK recieved..\n");
		printf("Closing the listening socket on parent..\n");
		close( sockfd );
	}

	printf( "Reading file..\n" );
	ifp = fopen( filename, 	"r" );

	swnd1 = malloc( sender_window_size*sizeof(send_buffer) );
	int i;
	for( i = 0 ; i<sender_window_size; i++)
	{
			swnd1[i].data[0] = '\0';
	}	

	int buffer_position, send_counter = 0, t, previous_na;

	memset( sendline, '\0', sizeof( sendline ) );

	printf("Sending file to the client..\n");	
	int j, sender_usable_window, ack_recieved, closing_child=0 ;
	recv_advertisement = INT_MAX;
	ssthresh=recv_window_size;	
	printf("\nSSTHRESH initiated to: %d\n",ssthresh);	
	while(1)
	{	
		if( cwnd > ( nt - na ) )
			sender_usable_window = cwnd - ( nt - na ) ;	
		else
			sender_usable_window = 0 ;

		printf("\nCongestion window : \nSender usable window : %d\nReciever window advertisement : %d\n", cwnd, sender_usable_window, recv_advertisement );
		if(sender_usable_window==0)
		{
			printf( "\n*********************BUFFER FULL*************************\n");
		}	
		/* Check for if recv_adv == 0 i.e. no more packets to be sent. */
		j = MIN( sender_usable_window , recv_advertisement );
	
		if( (j == 0) || (send_counter == recv_advertisement) )
		{
			while(1)
			{	
		
				if( recv_advertisement == 0 
					&& persist_timer_flag == 0 ) 
				{
					persist_timer_flag = 1;
					//signal(SIGALRM, sig_alrm);
					//rtt_newpack( &rttinfo );
					printf("************** PERSIST TIMER **************\n");	
					printf("Sending Probe packet..\n");	
					dg_retransmit( connfd, -1 );					
				} 

//				previous_na = na;
				ack_recieved = dg_recieve_ack( connfd );

				if( persist_timer_flag == 1 )
				{
					if( recv_advertisement > 0) 
					{
						persist_timer_flag = 0;
						struct itimerval value, ovalue, pvalue;
        					value.it_interval.tv_sec = 0;        /* Zero seconds */
       						value.it_interval.tv_usec = 0;  /* Two hundred milliseconds */
        					value.it_value.tv_sec = 0;           /* Zero seconds */
        					value.it_value.tv_usec = 0;     /* Five hundred milliseconds */
	
						setitimer( ITIMER_REAL, &value, &ovalue );
	
						//alarm(0);
						break;	
					}
				}	

				if( dup_ack == 3 && persist_timer_flag == 0 )
				{
					/* DUP ACK's case */
					//dup_ack = 0;
					ssthresh = MIN( cwnd, recv_advertisement );
					ssthresh = ( MAX( ssthresh, 2 ) )/2;
					printf("\n****************** 3 DUPLICATE ACK'S REC'VED ******************\n");
					printf("Retransmitting the packet %d.. \n", ack_recieved);
					printf("Setting ssthresh to half of current window size : '%d'.. \n", ssthresh );
										
					dg_retransmit( connfd, ack_recieved );	
				}	

				if( ack_recieved == nt && persist_timer_flag == 0 )
				{	
					/* All Acks have been recieved.. */
					printf("All ACK's have been recieved for buffer..\n");
					
					struct itimerval value, ovalue, pvalue;
                                        value.it_interval.tv_sec = 0;        /* Zero seconds */
                                        value.it_interval.tv_usec = 0;  /* Two hundred milliseconds */
                                        value.it_value.tv_sec = 0;           /* Zero seconds */
                                        value.it_value.tv_usec = 0;     /* Five hundred milliseconds */
                                        setitimer( ITIMER_REAL, &value, &ovalue );
					send_counter = 0;
					break;
				}	
			}

			if( slowstart == 0 )
			{
				cwnd += 1;
			}
		}	
		else if( fread( sendline, 1, PACKET_SIZE, ifp ) != NULL )
		{
			printf("********************************* SENDING DATA ************************************\n");
			printf("DATA being sent : '%s'\n", sendline );
			printf("Size : %d\n", strlen(sendline) );
			printf("***********************************************************************************\n");
		
			printf("Starting RTT timer..\n");
			if( rttinit == 0 ) 
			{
				rtt_init( &rttinfo );		/* first time we're called */
				rttinit = 1;
				rtt_d_flag = 1;
			}

			if( send_counter == 0 )
			{	
				struct itimerval value, ovalue, pvalue;
				signal(SIGALRM, sig_alrm);
				rtt_newpack( &rttinfo );		/* initialize for this packet and sets retransmission counter to 0*/
	        	value=rtt_start(&rttinfo);
	        	setitimer( ITIMER_REAL, &value, &ovalue );
	        }	
	        	
			buffer_position = dg_send( connfd, sendline, strlen( sendline ) );
			
			if ( sigsetjmp( jmpbuf, 1 ) != 0 ) 
			{
				if ( rtt_timeout( &rttinfo ) < 0 && persist_timer_flag == 1 ) 
				{
					err_msg( "Error : no response from client, giving up" );
					rttinit = 0;	/* reinit in case we're called again */
					errno = ETIMEDOUT;
					return(-1);
				}
				goto sendagain;
			}

			send_counter++;
			status_report();

			printf("Buffer position : %d\n", buffer_position );	

			memset( sendline, '\0', sizeof( sendline ) );
		}
		else
		{
			/* the last packets */
			while( na != nt || (closing_child == 1) )
			{	
				ack_recieved = dg_recieve_ack( connfd );
				if( dup_ack == 3 )
				{
					/* DUP ACK's case */
					ssthresh = MIN( cwnd, recv_advertisement );
					ssthresh = ( MAX( ssthresh, 2 ) )/2;
					printf("****************** 3 DUPLICATE ACK'S REC'VED ******************\n");
					printf("Retransmitting the packet %d.. \n", ack_recieved);
					printf("Setting ssthresh to half of current window size : '%d'.. \n", ssthresh );
				
					dg_retransmit( connfd, ack_recieved );
				}	
				status_report();
			}
			printf("**************** SERVER CHILD SHUTDOWN PROCEDURE INTITIATED *****************\n");
			printf("\nCOMPLETED SENDING ALL PACKETS TO CLIENT..\n");
			printf("SENDING A FIN..\n");
			dg_retransmit(connfd,-3); //FIN PACKET
			dg_recieve_ack( connfd );
			dg_retransmit(connfd,-4);	//final ACK 
			closing_child = 1;

			printf("\nSHUTTING DOWN CHILD SERVER IN 2MSL seconds (ASSUMED AS 10 SECONDS)..\n");
			struct itimerval value, ovalue, pvalue;
            		value.it_interval.tv_sec = 0;        /* Zero seconds */
                    value.it_interval.tv_usec = 0;  /* Two hundred milliseconds */
                    value.it_value.tv_sec = 10;           /* Zero seconds */
                    value.it_value.tv_usec = 0;     /* Five hundred milliseconds */
                    setitimer( ITIMER_REAL, &value, &ovalue );

		}	
		continue;
		sendagain : 
					if( closing_child == 1 )
					{
						printf("CLOSING THE CHILD PROCESS ON THE SERVER..\n");
						exit(0);	
						break;
						// TERMINATE SAFELY	
					}	
					if( recv_advertisement == 0 )
					{
						dg_retransmit( connfd, -1 );
					}	
					else
					{
							printf("******************RTT TIMEOUT EXPERIENCED******************..\n");	
							printf("Retransmitting the packet %d.. )\n", na);
							dg_retransmit( connfd, na );
					}	
					ssthresh = MIN( cwnd, recv_advertisement );
					ssthresh = ( MAX( ssthresh, 2 ) )/2;
					cwnd = 1;
					slowstart = 1;
					printf("\n\nSetting ssthresh to half of current window size : '%d'.. \n", ssthresh );
				
	}	
}
Example #7
0
void
Optimizer_thread::
emit_status()
{
  emit (status_report(f_->status(timer_period_)));
}
Example #8
0
void
Meshing_thread::
emit_status()
{
  emit (status_report(f_->status(timer_period_)));
}