PingPong
Dependencies: EthernetInterface_pm mbed-rtos mbed
main.cpp@0:c8f056a185d0, 2014-03-27 (annotated)
- Committer:
- rebonatto
- Date:
- Thu Mar 27 17:39:23 2014 +0000
- Revision:
- 0:c8f056a185d0
Para testar a rede.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:c8f056a185d0 | 1 | #include "mbed.h" |
rebonatto | 0:c8f056a185d0 | 2 | #include "EthernetInterface.h" |
rebonatto | 0:c8f056a185d0 | 3 | #include "string.h" |
rebonatto | 0:c8f056a185d0 | 4 | #include "stdio.h" |
rebonatto | 0:c8f056a185d0 | 5 | |
rebonatto | 0:c8f056a185d0 | 6 | #include "Configuracoes.h" |
rebonatto | 0:c8f056a185d0 | 7 | #include "CommTCP.h" |
rebonatto | 0:c8f056a185d0 | 8 | #include "Http_post.h" |
rebonatto | 0:c8f056a185d0 | 9 | |
rebonatto | 0:c8f056a185d0 | 10 | void InitializeEthernetLink(); |
rebonatto | 0:c8f056a185d0 | 11 | void *montamsg(char *msg, int seq, char *type); |
rebonatto | 0:c8f056a185d0 | 12 | void SendMessage(char *msg); |
rebonatto | 0:c8f056a185d0 | 13 | |
rebonatto | 0:c8f056a185d0 | 14 | EthernetInterface eth; |
rebonatto | 0:c8f056a185d0 | 15 | int errorCnt = 0; |
rebonatto | 0:c8f056a185d0 | 16 | int errorSnd = 0; |
rebonatto | 0:c8f056a185d0 | 17 | |
rebonatto | 0:c8f056a185d0 | 18 | DigitalOut myled(LED1); |
rebonatto | 0:c8f056a185d0 | 19 | |
rebonatto | 0:c8f056a185d0 | 20 | int main() { |
rebonatto | 0:c8f056a185d0 | 21 | char msg[50]; |
rebonatto | 0:c8f056a185d0 | 22 | int c=0; |
rebonatto | 0:c8f056a185d0 | 23 | |
rebonatto | 0:c8f056a185d0 | 24 | InitializeEthernetLink(); |
rebonatto | 0:c8f056a185d0 | 25 | printf("Test %s with2 %d messages\n", MSG, FINAL); |
rebonatto | 0:c8f056a185d0 | 26 | |
rebonatto | 0:c8f056a185d0 | 27 | //Start TCP daemon service |
rebonatto | 0:c8f056a185d0 | 28 | Thread TcpService(CommTCP::CommTCP_Thread); |
rebonatto | 0:c8f056a185d0 | 29 | |
rebonatto | 0:c8f056a185d0 | 30 | //wait(2); //Time to TCP bind and listen |
rebonatto | 0:c8f056a185d0 | 31 | while(1) { |
rebonatto | 0:c8f056a185d0 | 32 | |
rebonatto | 0:c8f056a185d0 | 33 | if (PING){ |
rebonatto | 0:c8f056a185d0 | 34 | montamsg(msg, c++, "Ping"); |
rebonatto | 0:c8f056a185d0 | 35 | SendMessage(msg); |
rebonatto | 0:c8f056a185d0 | 36 | } |
rebonatto | 0:c8f056a185d0 | 37 | else{ |
rebonatto | 0:c8f056a185d0 | 38 | montamsg(msg, c++, "Pong"); |
rebonatto | 0:c8f056a185d0 | 39 | SendMessage(msg); |
rebonatto | 0:c8f056a185d0 | 40 | } |
rebonatto | 0:c8f056a185d0 | 41 | //printf("%s\n", msg); |
rebonatto | 0:c8f056a185d0 | 42 | |
rebonatto | 0:c8f056a185d0 | 43 | TCPSocketConnection s; |
rebonatto | 0:c8f056a185d0 | 44 | HttpPost::DoPost(s, APACHE); |
rebonatto | 0:c8f056a185d0 | 45 | |
rebonatto | 0:c8f056a185d0 | 46 | if(c % 1000 == 0){ |
rebonatto | 0:c8f056a185d0 | 47 | printf("%s\n", msg); |
rebonatto | 0:c8f056a185d0 | 48 | //wait(10); |
rebonatto | 0:c8f056a185d0 | 49 | } |
rebonatto | 0:c8f056a185d0 | 50 | |
rebonatto | 0:c8f056a185d0 | 51 | if (c == FINAL) { |
rebonatto | 0:c8f056a185d0 | 52 | printf("Vai acabar |||\n"); |
rebonatto | 0:c8f056a185d0 | 53 | //while(CommTCP::getCont() < FINAL); |
rebonatto | 0:c8f056a185d0 | 54 | //wait(3); |
rebonatto | 0:c8f056a185d0 | 55 | break; |
rebonatto | 0:c8f056a185d0 | 56 | } |
rebonatto | 0:c8f056a185d0 | 57 | } |
rebonatto | 0:c8f056a185d0 | 58 | printf("Foram recebidas %d mensagens corretas\n", CommTCP::getCont() ); |
rebonatto | 0:c8f056a185d0 | 59 | printf("%d Erros of connection and %d Errors of send\n", errorCnt, errorSnd); |
rebonatto | 0:c8f056a185d0 | 60 | printf("%d Erros of HTTP connection and %d Errors of send\n", HttpPost::getCountCnt(), HttpPost::getCountSnd() ); |
rebonatto | 0:c8f056a185d0 | 61 | printf("%d Erros of TCP connection and %d Errors of send\n", CommTCP::getCountCnt(), CommTCP::getCountSnd() ); |
rebonatto | 0:c8f056a185d0 | 62 | //TcpService.terminate(); |
rebonatto | 0:c8f056a185d0 | 63 | |
rebonatto | 0:c8f056a185d0 | 64 | while(1) { |
rebonatto | 0:c8f056a185d0 | 65 | myled = 1; |
rebonatto | 0:c8f056a185d0 | 66 | wait(0.2); |
rebonatto | 0:c8f056a185d0 | 67 | myled = 0; |
rebonatto | 0:c8f056a185d0 | 68 | wait(0.2); |
rebonatto | 0:c8f056a185d0 | 69 | } |
rebonatto | 0:c8f056a185d0 | 70 | } |
rebonatto | 0:c8f056a185d0 | 71 | |
rebonatto | 0:c8f056a185d0 | 72 | void SendMessage(char *msg){ |
rebonatto | 0:c8f056a185d0 | 73 | TCPSocketConnection socket; |
rebonatto | 0:c8f056a185d0 | 74 | int r, i; |
rebonatto | 0:c8f056a185d0 | 75 | |
rebonatto | 0:c8f056a185d0 | 76 | |
rebonatto | 0:c8f056a185d0 | 77 | for(i = 0; i < MAXTRIES; i++){ |
rebonatto | 0:c8f056a185d0 | 78 | r = socket.connect(VIZINHO, TCPPORT); |
rebonatto | 0:c8f056a185d0 | 79 | if (r < 0) { |
rebonatto | 0:c8f056a185d0 | 80 | if (i == 0) |
rebonatto | 0:c8f056a185d0 | 81 | errorCnt++; |
rebonatto | 0:c8f056a185d0 | 82 | printf("Error %d Unable to connect %d to (%s) on port (%d) Try %d\n", errorCnt, r, VIZINHO, TCPPORT, i); |
rebonatto | 0:c8f056a185d0 | 83 | wait_ms(DELAYTRY); |
rebonatto | 0:c8f056a185d0 | 84 | } |
rebonatto | 0:c8f056a185d0 | 85 | else |
rebonatto | 0:c8f056a185d0 | 86 | break; |
rebonatto | 0:c8f056a185d0 | 87 | } |
rebonatto | 0:c8f056a185d0 | 88 | if (r == 0) { |
rebonatto | 0:c8f056a185d0 | 89 | for(i =0; i < MAXTRIES; i++){ |
rebonatto | 0:c8f056a185d0 | 90 | r = socket.send_all(msg, strlen(msg)); |
rebonatto | 0:c8f056a185d0 | 91 | if (r < 0) { |
rebonatto | 0:c8f056a185d0 | 92 | if (i == 0) |
rebonatto | 0:c8f056a185d0 | 93 | errorSnd++; |
rebonatto | 0:c8f056a185d0 | 94 | printf("Error %d Unable to Send Try %d\n", errorSnd, i); |
rebonatto | 0:c8f056a185d0 | 95 | wait_ms(DELAYTRY); |
rebonatto | 0:c8f056a185d0 | 96 | } |
rebonatto | 0:c8f056a185d0 | 97 | else |
rebonatto | 0:c8f056a185d0 | 98 | break; |
rebonatto | 0:c8f056a185d0 | 99 | } |
rebonatto | 0:c8f056a185d0 | 100 | wait_ms(DELAY); |
rebonatto | 0:c8f056a185d0 | 101 | //printf("Enviou Mensagem %s com %d bytes\n", msg, strlen(msg)); |
rebonatto | 0:c8f056a185d0 | 102 | socket.close(); |
rebonatto | 0:c8f056a185d0 | 103 | } |
rebonatto | 0:c8f056a185d0 | 104 | |
rebonatto | 0:c8f056a185d0 | 105 | return; |
rebonatto | 0:c8f056a185d0 | 106 | } |
rebonatto | 0:c8f056a185d0 | 107 | |
rebonatto | 0:c8f056a185d0 | 108 | |
rebonatto | 0:c8f056a185d0 | 109 | void *montamsg(char *msg, int seq, char *type){ |
rebonatto | 0:c8f056a185d0 | 110 | char aux[10]; |
rebonatto | 0:c8f056a185d0 | 111 | |
rebonatto | 0:c8f056a185d0 | 112 | memset(msg, 0, sizeof(msg)); |
rebonatto | 0:c8f056a185d0 | 113 | strcat(msg, MEUIP); |
rebonatto | 0:c8f056a185d0 | 114 | strcat(msg, ":"); |
rebonatto | 0:c8f056a185d0 | 115 | strcat(msg, type); |
rebonatto | 0:c8f056a185d0 | 116 | strcat(msg, ":"); |
rebonatto | 0:c8f056a185d0 | 117 | sprintf(aux, "%d", seq); |
rebonatto | 0:c8f056a185d0 | 118 | strcat(msg, aux); |
rebonatto | 0:c8f056a185d0 | 119 | |
rebonatto | 0:c8f056a185d0 | 120 | return NULL; |
rebonatto | 0:c8f056a185d0 | 121 | } |
rebonatto | 0:c8f056a185d0 | 122 | |
rebonatto | 0:c8f056a185d0 | 123 | void InitializeEthernetLink() |
rebonatto | 0:c8f056a185d0 | 124 | { |
rebonatto | 0:c8f056a185d0 | 125 | eth.init(MEUIP,"255.255.255.0","192.168.1.5"); |
rebonatto | 0:c8f056a185d0 | 126 | |
rebonatto | 0:c8f056a185d0 | 127 | eth.connect(); |
rebonatto | 0:c8f056a185d0 | 128 | printf("IP Address is %s\n", eth.getIPAddress()); |
rebonatto | 0:c8f056a185d0 | 129 | } |