PingPong

Dependencies:   EthernetInterface_pm mbed-rtos mbed

Codes/CommTCP.cpp

Committer:
rebonatto
Date:
2014-03-27
Revision:
1:b343d9cd769a
Parent:
0:c8f056a185d0

File content as of revision 1:b343d9cd769a:

/*
 * CommTCP.cpp
 *
 *  Created on: 01/09/2013
 *      Author: Rebonatto
 */
#include "CommTCP.h"

static int countCnt = 0;
static int countSnd = 0;
static int cont = 0;

int CommTCP::getCountCnt() { return countCnt; };
int CommTCP::getCountSnd() { return countSnd; };
int CommTCP::getCont()    { return cont; };

void CommTCP::CommTCP_Thread(void const *arg)
{
    char buffer[50];
    int r, i, c = 0;    
    
    TCPSocketServer ServerSocket;    
        
    ServerSocket.bind(TCPPORT);    
    ServerSocket.listen();        
    ServerSocket.set_blocking(true, 500);
    printf("TCP Thread starting... listen on %s:%d\r\n", MEUIP, TCPPORT);    
//    printf("ServerSocket %s:%d\n", get_address(), get_port());    
        
    while(1)
    {        
        TCPSocketConnection sock;
        
        for(i=0; i < MAXTRIES; i++){       
            //printf("Aguarda Conexao\n"); 
            r = ServerSocket.accept(sock);             
            if (r < 0){
                if (i == 0)
                    countCnt++;
                printf("Error %d Unable to receive connect Try %d\n", countCnt, i);
                //wait(0.5);
            }
            else
               break;
        } 
        if (r == 0){
            //printf("Conected %d at %s\n", r, sock.get_address());
            for(i=0; i < MAXTRIES; i++){
                r = sock.receive(buffer, sizeof(buffer));            
                if (r <= 0){
                    if (i == 0)
                        countSnd++;
                    printf("Erro %d ao Receber no socket TCP!! conect %d Try %d\n", countSnd, r, i);
                    wait_ms(DELAYTRY);
                }            
                else{
                     buffer[r] = '\0';
                    //if (cont % 60 == 0)
                     cont++;
                     if (cont >= FINAL-1)
                        printf("Recebida conexao %d msg TCP: %d chars: *%s*\n", cont, r, buffer);
                     break;
                }
            }            
         }
         wait_ms(DELAY);
        
        //printf("Received %d chars:\n*%s*\n", ret, buffer);
        sock.close();               
        if (++c == FINAL) {
            printf("Vai acabar Thread TCP |||\n");
            wait(3);
            break;
        }
    }
}

/*

void CommTCP::RequestAcom(){
    // Metodo para solicitar os acompanhamentos
    char msg[] = "3;-1";
    TCPSocketConnection s[NEIGHBORS];
    int escritos, i;
    //TCPSocketConnection s[NEIGHBORS];
*/        
    /*    
        Formato das mensagens de requisicao
        Tipo ; Tomada
        Tipos: 1 Solicitacao de fase
               2 Solicitacao de diferencial
               3 Acompanhamento (manda fase e diferencial)
        Tomada: Numero da tomada
                -1 (zero) quando de todas as tomadas
    */    
    /*Timer t;
    //t.start();

    for(i=0;i<Settings::get_NumNeighbors();i++){
        s[i].connect(Settings::get_Neighbor(i), Settings::get_PortTCP());   
    }          
    //t.stop();
    //printf("The time taken in connection was %d useconds\n", t.read_us());
    *
    for(i=0; i< Settings::get_NumNeighbors(); i++){    
        //talvez verificar se o socket est conectado. Se no estiver, destruir objeto e conectar
        TCPSocketConnection sock;
        sock.connect(Settings::get_Neighbor(i), Settings::get_PortTCP());        
        //escritos = Settings::get_Socket(i).send_all(msg, sizeof(msg)-1);
        //printf("Socket %d\n",s[i].is_connected());
        //Timer t1;
        //t1.start();
        escritos = sock.send_all(msg, strlen(msg));
        wait_ms(100);
        if(escritos != strlen(msg)){
            printf("Erro ao enviar mensagem para vizinho\n");
            break;
        }
        //t1.stop();
        //printf("The time taken in send was %d useconds\n", t1.read_us());

        sock.close();        
        
    }                    
}

void CommTCP::SendAcom(int tipo,int tomada){
    // Aqui chama a funço para enviar um acompanhamento
    for(int i=0;i<Settings::get_MaxChannels();i++)
    {
        EventDetector::get_Detector(i).ExternalTrigger();
    }
}
*/