Ejemplo n.º 1
0
int main(void)
{  

	delay_ms(100);
	led_setup();
	adc_init();
	LCD_SETUP(buffer);

	tcp_setup.Gateway = GateWay;//Gateway Address
	tcp_setup.MAC = MAC;
	tcp_setup.Source_IP = IP;//IP Address
	tcp_setup.Source_Port = 80; // Web Port
	tcp_setup.Subnet = SubNet;//SubnetMask Address
	tcp_setup.s = 0;
	tcp_socket_init(&tcp_setup);


	while(1)
	{
		if(check_for_connections(&tcp_setup)) // Wait for connection)
		{
			process_request(&tcp_setup);
			tcp_socket_init(&tcp_setup);
		}

		sprintf(buffer2,"%d",ADCRead(0));
		drawString8x12(10,60,buffer2);
	}

}
Ejemplo n.º 2
0
int main() {
    running = 1;
    signal(SIGINT, interrupt_handler);
    printf("Starting server\n");

    if(!init_server_socket(&sock, PORT))
        return -1;

    //A linked list might not be the most efficient for this
    llist client_sessions;
    llist_init(&client_sessions);

    queue message_queue;
    queue_init(&message_queue);

    //TODO: initialise thread pool
    const int num_workers = 4;
    pthread_t workers[num_workers];
    for(int i=0; i<num_workers; i++) {
        pthread_create(&workers[i], NULL, worker_run, (void*)&message_queue);
    }
    
    //select stuff
    fd_set readsocketset;
    fd_set writesocketset;
    fd_set errorsocketset;
    struct timeval timeout;

    while(running) {
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        
        printf("1\n");
        build_socket_list(&client_sessions, &readsocketset);
        FD_ZERO(&readsocketset);
        FD_SET(sock, &readsocketset);

        int s = select(client_sessions.len, &readsocketset, 0, 0, &timeout);
        printf("2\n");
        if(s < 0) {
            printf("ERROR: Select error\n");
            exit(1);
        }
        //if we have a new connection create a new session
        if(FD_ISSET(sock, &readsocketset)) {
            int csock = check_for_connections(sock);
            session clientSession;
            session_init(&clientSession, csock);
            llist_append(&client_sessions, (void*)&clientSession);
        }

        printf("2\n");
        //check if each session exists in the read socket thingE
        llist_node *cur = client_sessions.head;
        while(cur != NULL) {
            int sock = ((session*)cur->data)->sock;
            //check readsocketset
            if(FD_ISSET(sock, &readsocketset)) 
                client_read_data((session*)cur->data);
            //check writesocketset
            //check errorset
            cur = cur->next;
        }

        //TODO:
        //parse the messages
        //add parsed message to the queue

        //send messages on the queue (Should this be done here?)
    }
    printf("Exiting..\n");
    
    //free memory
    llist_node *cur = client_sessions.head;
    while(cur != NULL) {
        session *sess = (session*)cur->data;
        session_end(sess);
        free(sess);
        cur = cur->next;
    }
    llist_free(&client_sessions);
    close(sock);

    pthread_exit(NULL);
}