Exemplo n.º 1
0
//---------------------------------------------------------------------
void loop()
{
    waitForIncomingConnection();
    
    while ( client_.connected() )
    {
        uint8_t cmd = 255;
        
        if ( checkTcpTimeout() )
        {
#ifdef DEBUG_SERIAL
            Serial.println( "ERROR: connection timeout!" );
#endif
            break;
        }
        
        cmd = getCommand();
        if ( cmd == COMMAND_INVALID_COMMAND )
        {
#ifdef DEBUG_SERIAL
            Serial.print( "ERROR: invalid command received: ");
            Serial.println( cmd );
#endif
            break;
        }
        
        processCommand( cmd );
    }
    terminateConnection();
}
Exemplo n.º 2
0
int runClient(char *port) {
    char *name = "Client";

    //initialize masterServer, peerList and hostlist
    peerList = NULL;
    connectionList = NULL;
    connectionIdGenerator = 2; //1 is received for the server
    masterServer = NULL;

    int listernerSockfd = -1;
    struct connectionInfo *serverInfo = startServer(port, "CLIENT");
    if (serverInfo == NULL) {
        fprintf(stderr, "Did not get serverInfo from startServer()\n");
        return -1;
    }
    listernerSockfd = serverInfo->listernerSockfd;

    int STDIN = 0; //0 represents STDIN

    FD_ZERO(&masterFDList); // clear the master and temp sets
    FD_ZERO(&tempFDList);

    FD_SET(STDIN, &masterFDList); // add STDIN to master FD list
    fdmax = STDIN;

    FD_SET(listernerSockfd, &masterFDList); //add the listener to master FD list and update fdmax
    if (listernerSockfd > fdmax)
        fdmax = listernerSockfd;


    int actionComplete = 1;
    while (1) //keep waiting for input, connections and data
    {

        //this is ti identify is an activity is in progress
        if (actionComplete == 1) {
            printf("$");
            fflush(stdout); //print the terminal symbol
        }
        actionComplete = 1;

        tempFDList = masterFDList; //make a copy of masterFDList and use it as select() modifies the list

        //int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
        if (select(fdmax + 1, &tempFDList, NULL, NULL, NULL) ==
                -1) //select waits till it gets data in an fd in the list
        {
            fprintf(stderr, "Error in select\n");
            return -1;
        }

        // an FD has data so iterate through the list to find the fd with data
        int fd;
        for (fd = 0; fd <= 20; fd++) {
            if (FD_ISSET(fd, &tempFDList)) //found the FD with data
            {
                if (fd == STDIN) //data from commandLine(STDIN)
                {
                    size_t commandLength = 50;
                    char *command = (char *) malloc(commandLength);
                    getline(&command, &commandLength, stdin); //get line the variable if space is not sufficient
                    if (stringEquals(command, "\n")) //to handle the stray \n s
                        continue;
                    //printf("--Got data: %s--\n",command);
                    handleCommands(command, "CLIENT");
                }
                else if (fd == listernerSockfd) //new client trying to connect to listener
                {

                    struct sockaddr_storage clientaddr;
                    socklen_t addrlen = sizeof clientaddr;
                    int clientsockfd;
                    if ((clientsockfd = accept(listernerSockfd, (struct sockaddr *) &clientaddr, &addrlen)) == -1) {
                        fprintf(stderr, "Error accepting connection: %d %s\n", clientsockfd,
                                gai_strerror(clientsockfd));
                        return -1;
                    }
                    else {
                        //accept connection from client add it to the connectionList
                        struct host *client = (struct host *) malloc(sizeof(struct host));
                        struct sockaddr *hostAddress = (struct sockaddr *) &clientaddr;
                        client->id = connectionIdGenerator++;
                        client->sockfd = clientsockfd;
                        client->ipAddress = getIPAddress(hostAddress);
                        client->hostName = getHostFromIp(client->ipAddress);
                        client->port = getPort(hostAddress);
                        addNode(&connectionList, client);
                        FD_SET(client->sockfd, &masterFDList); // add fd to fdlist
                        if (client->sockfd > fdmax)
                            fdmax = client->sockfd;
                        printf("Accepted client: %s/%s.\n", client->hostName, client->port);

                    }

                }
                else if (masterServer != NULL && fd == masterServer->sockfd)// handle data from server
                {

                    struct packet *recvPacket = readPacket(fd);
                    if (recvPacket == NULL) {
                        printf("Master Server has terminated unexpectedly.\n");
                        int connectionId = getIDForFD(connectionList, fd);
                        terminateConnection(connectionId);
                        masterServer = NULL;
                        peerList = NULL;
                        continue;
                    }

//                    printf("Received packet: ");
//                    printPacket(recvPacket);
//                    printf("\n");

                    //received terminate from server
                    if (recvPacket->header->messageType == terminate) {
                        printf("Received TERMINATE command from server.\n");
                        int connectionId = getIDForFD(connectionList, fd);
                        terminateConnection(connectionId);
                        peerList = NULL;
                        masterServer = NULL;
                        continue;
                    }

                    if (recvPacket->header->messageType == hostList) {
                        //split the hostlist
                        int length = 0;
                        char **hostinfo = splitString(recvPacket->message, ' ', &length);
                        free(peerList);
                        peerList = NULL; //destroy the old peerList
                        int i;
                        for (i = 0; i < length; i = i + 2) {
                            if (i + 1 >= length)
                                fprintf(stderr, "Disproportionate terms in hostList sent by server.\n");

                            if (stringEquals(myIpAddress, hostinfo[i]) && stringEquals(myListenerPort, port)) {
                                //this is so that the client doesn't add itself in the peerList
                                continue;
                            }
                            //add all nodes
                            struct host *peer = (struct host *) malloc(sizeof(struct host));
                            peer->sockfd = -1; // we do not have a connection with it yet
                            peer->ipAddress = hostinfo[i];
                            peer->hostName = getHostFromIp(peer->ipAddress);
                            peer->port = hostinfo[i + 1];
                            addNode(&peerList, peer);
                        }
                        printf("New peerList received from server:\n");
                        printPeerList(peerList);
                        continue;
                    }

                    if (recvPacket->header->messageType == syncFiles) {
                        printf("Received a sync initiate request from Server.\n");
                        syncHostnameFiles();
                        continue;
                    }

                }
                else {

                    //handle data from the peers
                    struct packet *recvPacket = readPacket(fd);
                    if (recvPacket == NULL) {
                        //one of the clients terminated unexpectedly
                        int id = getIDForFD(connectionList, fd);
                        struct host *host = getHostByID(connectionList, id);
                        printf("%s/%s terminated unexpectedly. Removing it from the list.\n",
                               host->hostName, host->port, host->sockfd);
                        terminateConnection(id);
                        continue;
                    }
//                    printf("Received packet: ");
//                    printPacket(recvPacket);
//                    printf("\n");

                    //received terminate
                    if (recvPacket->header->messageType == terminate) {
                        int id = getIDForFD(connectionList, fd);
                        struct host *source = getHostByID(connectionList, id);
                        printf("Received TERMINATE command from %s/%s. Removing it.\n", source->hostName, source->port);
                        terminateConnection(id);
                        continue;
                    }//handle error message
                    else if (recvPacket->header->messageType == error) {
                        printf("Received error message: %s\n", recvPacket->message);
                        continue;
                    }//handle get request
                    else if (recvPacket->header->messageType == get) {
                        int connectionId = getIDForFD(connectionList, fd);
                        struct host *destination = getHostByID(connectionList, connectionId);
                        if (destination == NULL) {
                            fprintf(stderr, "Coudn't find the connection id.\n");
                            continue;
                        }
                        printf("Received a get request for file %s from client: %s/%s.\n",
                               recvPacket->header->fileName, destination->hostName, destination->port);
                        sendFile(connectionId, recvPacket->header->fileName);
                    }//hand a put packet
                    else if (recvPacket->header->messageType == put) {
                        int connectionId = getIDForFD(connectionList, fd);
                        receiveFileAsynchronously(connectionId, recvPacket);
                        actionComplete = 0; //action is not complete until a ok packet is received
                        continue;
                    }//handle a ok packet
                    else if (recvPacket->header->messageType == ok) {
                        int connectionId = getIDForFD(connectionList, fd);
                        okPacketHandler(connectionId, recvPacket);
                        actionComplete = 1; // activity completed
                        continue;
                    }

                }
            }
        }
    }
}