Пример #1
0
void handleLeaveMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Leave message
    Leave *l = new Leave();
    //Decode leave message
    l->decode(inBuffer, size);
    //Search the users list for nickname
    bool deleted = false;

    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = *(it);
        //Remove nickname if it exists
        if (strcmp(l->getNickname(), user->getNickname()) == 0)
        {
            users.erase(it);
            deleted = true;
            break; //Erasing messes up iteration, so may as well just break out
        }
    }
    pthread_mutex_unlock(&userListMutex);

    //Create appropriate confirm message
    Confirm *c = new Confirm();
    c->setResponse(deleted);
    //Encode this message and return
    (*outSize)=c->encode(outBuffer);
    delete l;
    delete c;
}
Пример #2
0
void handleJoinMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Join message
    Join *j = new Join();
    //Decode buffer into join message
    j->decode(inBuffer, size);
    printf("Nickname: %s\n", j->getNickname());
    //Search the users list for nickname
    //If the name exits do not add
    bool accept = true;
    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = *(it);
        if (strcmp(j->getNickname(), user->getNickname()) == 0)
        {
            //Exact match
            accept = false;
        }
    }
    pthread_mutex_unlock(&userListMutex);
    if (accept)
    {
        users.push_back(j);
    }
    else
    {
        delete j;
    }
    //Respond with confirm message
    Confirm *c = new Confirm();

    c->setResponse(accept);
    //Encode the message into the outbuffer and
    //Set the outsize
    (*outSize)=c->encode(outBuffer);
    delete c;
}
Пример #3
0
void handleNewMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Chat Message
    ChatMessage *m = new ChatMessage();
    //Decode Chat Message
    m->decode(inBuffer, size);
    //Search the users list for nickname
    bool found = false;

    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = (*it);
        //Remove nickname if it exists
        if (strcmp(m->getSender(), user->getNickname()) == 0)
        {
            found = true;
        }
    }
    pthread_mutex_unlock(&userListMutex);

    //If the name exists add the name and message to the messages list
    if (found)
    {
        char* message = new char[strlen(m->getSender()) + 2 + strlen(m->getMessage()) + 1]; //sender + ": " + message + null
        sprintf(message, "%s: %s", m->getSender(), m->getMessage()); //easier than dealing with strcpy

        pthread_mutex_lock(&messageListMutex);
        messageList.push_back(message);
        pthread_mutex_unlock(&messageListMutex);
    }

    //Create appropriate confirm message
    Confirm *c = new Confirm();
    c->setResponse(found);
    //Encode this message and return
    (*outSize)=c->encode(outBuffer);

    delete m;
    delete c;
}
Пример #4
0
void ChatClient::on_joinButton_clicked()
{
    Join *j;
    Confirm *c;
    INet4Address *servaddr;
    Connection *myConnection;
    char* buffer; //for encoded messages
    int bytesToSend;
    int bytesToReceive;
    int bytesReceived;

    //Get the strings out of the address, port and nickname
    //boxes and convert to strings;
    QString qip = ui.addresLine->text();
    QString qport = ui.portLine->text();
    QString qnick = ui.nicknameLine->text();

    //Create new connection objects
    servaddr = new INet4Address(qip.toLatin1().data(),qport.toInt());
    myConnection = new Connection(servaddr);

    //Connect to server

    if(myConnection->createSocket() < 0)
    {
        printf("Socket Error\n");
        return;
    }

    if(myConnection->connectSocket() < 0)
    {
        printf("Connect Error\n");
        delete(myConnection);
        delete(servaddr);
        return;
    }

    //Make a join message
    j = new Join();
    j->setNickname(qnick.toLatin1().data());

    //Send join message (as bytes)
    bytesToSend = j->encode(&buffer);

    printf("Buffer: %s, Nick: %s ",buffer, j->getNickname());

    myConnection->writeSize(bytesToSend);
    myConnection->writen(buffer,bytesToSend);

    delete(buffer);

    //Read response - take action!
    //Read byte count
    bytesToReceive = myConnection->readSize();
    buffer = new char[bytesToReceive];

    //Read message
    bytesReceived=myConnection->readn(buffer,bytesToReceive);

    if(bytesReceived < bytesToReceive)
        printf("Server response truncated!");

    //Convert to message (confirm)
    c = new Confirm();
    c->decode(buffer,bytesToReceive);
    //Destroy socket
    myConnection->closeSocket();

    //check if nick accepted
    if(c->getResponse()==true)
    {
        nickAccepted = true;
        ui.addresLine->setEnabled(false);
        ui.portLine->setEnabled(false);
        ui.nicknameLine->setEnabled(false);
        ui.joinButton->setEnabled(false);

        //Turn on the timer
        timer->start();
    }
    else
    {
        nickAccepted = false;
        ui.nicknameLine->setText(tr("Rejected!"));
    }


    delete myConnection;
    delete servaddr;
}