示例#1
0
int main(int argc, char const *argv[])
{
	//freopen("debug.txt","w",stdout);
	int logfd = open(famlog,O_WRONLY);
	assert(logfd != -1);


	int i=0;
	for(i=0;i<MAXSERVER;i++)
	{
		//assert(pipe(fd[i]) == 0);
		assert(mkfifo(fifonames[i],0666) == 0);
		fd[i][READ] = open(fifonames[i],O_RDWR);
		fd[i][WRITE] = open(fifonames[i],O_RDWR);

		pfd[i] = fd[i][READ];
		pid_t pid = fork();
		if(pid == 0)
		{
			dup2(fd[i][WRITE],1);
			assert(execl(servernames[i],servernames[i],NULL)!=-1);
		}
		printf("process created==========\n");
	}

	///////Poliing server fd's ideally should be done in a different thread///////////
	printf("logserver waiting................\n");
	char buff[BUFFSIZE];
	char formatbuff[BUFFSIZE];
	time_t epoch_time;
	struct tm *tm_p;
	initSelect();
	while(1)
	{
		int status  = select(maxfd,&rset,&wset,&eset,NULL);
		//printf("bal\n");
		if(status > 0)
		{
			for(i=0;i<MAXSERVER;i++)
			{
				if(FD_ISSET(pfd[i],&rset))
				{
					printf("Msg received from server: %d\n",i+1);	
					read(fd[i][READ],buff,BUFFSIZE);
				    epoch_time = time( NULL );
				    tm_p = localtime( &epoch_time );
				    sprintf(formatbuff,"[%.2d:%.2d:%.2d] :server(%d): %s\n", tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,i+1,buff); //adding timestamp
					printf("Msg received from server: %d %s\n",i+1,formatbuff);	
					write(logfd,formatbuff,strlen(formatbuff));
				}
			}
		}
		initSelect();
	}
	return 0;
}
示例#2
0
void Server::handleSockets(void)
{
    fd_set                    readfds;
    fd_set                    writefds;
    int                          fd_max;
    struct timeval            tv;

    /*  Preparation et lancement de select() */
    fd_max = initSelect(tv, &readfds, &writefds);
    if (select(fd_max, &readfds, NULL, NULL, &tv) == -1)
    {
        throw std::runtime_error(std::string("Select Error : ") + strerror(errno));
    }
    else
    {
        /* Si une commande est tapée en console */
        if (FD_ISSET(0, &readfds))
            eventTerminal();
        /* Si un nouveau client essaye de se connecter */
        if (FD_ISSET(_socket->get(), &readfds))
            eventServer();
        /* On regarde si les clients nous envoies des infos */
        eventClients(&readfds, &writefds);
    }
}
示例#3
0
void				Client::start()
{
	int i = 0;

	this->music = new Music();
	this->launchGui = new std::thread(startGui, this);

	while (isConnected == false && exit == false)
	{
#ifdef			WIN32
		Sleep(TIMER_WAITING_ROOM);
#else
		usleep( TIMER_WAITING_ROOM * 1000);
#endif
	}
  while (isConnected == true && exit == false)
    {
      if (select.isFdSet('r', socket->getSocket()) == true)
        {
          try {
	    processPacketReception();
	  }
          catch (std::runtime_error e) {
	    std::cout << e.what() << std::endl;
          }
	  }
	  initSelect();
	  i++;
    }
}
示例#4
0
/**
 * Initializes all of the hardware including the programming of the meters with defaults from EEPROM.
 * */
void setup()
{
    // prescale of 2 after startup prescale of 8. 
    // This ensures that the atmega is running at 8 MHz assuming a 16Mhz clock.
    setClockPrescaler(CLOCK_PRESCALER_2);    

    // Start up serial ports
    dbg.begin(DBG_BAUD_RATE);
    mdm.begin(MDM_BAUD_RATE);
    cpu.begin(CPU_BAUD_RATE);

    // Write startup message to debug port
    dbg.println("\r\n\r\ntelduino power up");
    dbg.println("last compilation");
    dbg.println(__DATE__);
    dbg.println(__TIME__);

    DbgTelInit();				// Blink leds
    initSelect();				// Select Circuit done in sd_raw_init
    //sd_raw_init();			//SDCard
    SPI.begin();				// SPI
    SWinit();                   // Switches

    // Load circuit data from EEPROM
    for (int i=0; i < NCIRCUITS; i++) {
        Cload(&ckts[i],&cktsSave[i]);
    }
    for (int i=0; i < NCIRCUITS; i++) {
        Cprogram(&ckts[i]);
    }
}
示例#5
0
int FDSelect::_select(fd_set *readFds, fd_set *writeFds,
											unsigned long timeoutInMs, bool retry) {
	bool finished = false;
	int selected = -1;
	
	do {
		int maxFD = initSelect(timeoutInMs);
		selected = select(maxFD + 1, readFds, writeFds, NULL,
											(timeoutInMs == INFINITE_TIMEOUT) ? NULL : &tv);
		selected = select(maxFD + 1, readFds, writeFds, NULL,
											(timeoutInMs == INFINITE_TIMEOUT) ? NULL : &tv);
		finished = true;

		/* Even though select() is not supposed to set errno to EAGAIN
		 * (according to the linux man page), it seems that errno can be set
     * to EAGAIN on some cygwin systems. Thus, we need to catch that
     * here.
		 */
		if (retry) {
			if ((selected < 0) && ((errno == EAGAIN) ||
														 (errno == EINTR))) {
				finished = false;
			}
		}
	} while (!finished);

	return selected;
}
示例#6
0
int SocketManager::start() {
    int i;
    int ret;
    int nready = 0; 
    fd_set r_set;
    int max_sock = 0;      
    struct timeval timeout = {0, 0}; 
    char buffer[MAX_PAYLOAD] = {0};

    int j = 10;
    while (1) {
        
        printf("j: %d\n", j);
        j--; 
        if (j <0) {break;}

        initSelect(&r_set, &max_sock, &timeout);
       
        nready = select(max_sock + 1, &r_set, NULL, NULL, &timeout);
        printf("nready: %d\n", nready);

        if (nready < 0) { //error occur
            perror("select");
            exit(-1);
            
        } else if (nready == 0) { //timeout
            selectTimeoutHandle();
            continue;
        }
        
        if (readFromNetlink(&r_set, buffer, sizeof(buffer), nready) <= 0) { 
            continue;
        }

        if (readFromTcpConn(&r_set, buffer, sizeof(buffer), nready) <= 0) {
            continue;
        }

        if (acceptFromTcpListener(&r_set, nready) <= 0) {
            continue;
        }

    }

    return 0;
}
示例#7
0
void NetObject::writeSelect(int time)
{
	initSelect(time);
	select(max_fd+1, 0, &write_tmp, 0, &tv);
}
示例#8
0
void NetObject::readSelect(int time)
{
	initSelect(time);
	select(max_fd+1, &read_tmp, 0, 0, &tv);
}