PicoTCP TCP/UDP parallel traffic.

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Committer:
tass
Date:
Wed Jul 10 12:09:53 2013 +0000
Revision:
0:f9e04607effd
PicoTCP parallel TCP/UDP connections test (beta testing)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tass 0:f9e04607effd 1 #include "mbed.h"
tass 0:f9e04607effd 2 #include "rtos.h"
tass 0:f9e04607effd 3 #include "EthernetInterface.h"
tass 0:f9e04607effd 4
tass 0:f9e04607effd 5 #define ECHO_SERVER_PORT 7
tass 0:f9e04607effd 6 #define UDP_SERVER_PORT 2327
tass 0:f9e04607effd 7 #define BUFFER_QUANTITY (1024*1024)
tass 0:f9e04607effd 8 #define NUMBER_OF_THREADS 2u
tass 0:f9e04607effd 9
tass 0:f9e04607effd 10 Queue<void,NUMBER_OF_THREADS> clientList;
tass 0:f9e04607effd 11
tass 0:f9e04607effd 12
tass 0:f9e04607effd 13 /****** UDP Connection ******/
tass 0:f9e04607effd 14 void UDPThread(const void * arg);
tass 0:f9e04607effd 15 void startUDPThread(void)
tass 0:f9e04607effd 16 {
tass 0:f9e04607effd 17 Thread * threadUDPEcho = new Thread(UDPThread);
tass 0:f9e04607effd 18 }
tass 0:f9e04607effd 19
tass 0:f9e04607effd 20 // thread is sending out data on a rate of 1KB at each 10ms
tass 0:f9e04607effd 21 void UDPThread(const void * arg)
tass 0:f9e04607effd 22 {
tass 0:f9e04607effd 23
tass 0:f9e04607effd 24 UDPSocket server;
tass 0:f9e04607effd 25 Endpoint endp;
tass 0:f9e04607effd 26 char buffer[1024];
tass 0:f9e04607effd 27
tass 0:f9e04607effd 28 endp.set_address("255.255.255.255",UDP_SERVER_PORT);
tass 0:f9e04607effd 29 printf("Binding result :%d\n", server.bind(UDP_SERVER_PORT));
tass 0:f9e04607effd 30
tass 0:f9e04607effd 31 server.set_blocking(false,1500);
tass 0:f9e04607effd 32 printf("Started UDP Broadcast server...\n");
tass 0:f9e04607effd 33 server.set_broadcasting();
tass 0:f9e04607effd 34
tass 0:f9e04607effd 35 while(true)
tass 0:f9e04607effd 36 {
tass 0:f9e04607effd 37 server.sendTo(endp,buffer,sizeof(buffer));
tass 0:f9e04607effd 38 Thread::wait(10);
tass 0:f9e04607effd 39 }
tass 0:f9e04607effd 40
tass 0:f9e04607effd 41 server.close();
tass 0:f9e04607effd 42 }
tass 0:f9e04607effd 43
tass 0:f9e04607effd 44 /****** TCP Connection ******/
tass 0:f9e04607effd 45
tass 0:f9e04607effd 46 void TCPClientThread(const void * arg);
tass 0:f9e04607effd 47 void acceptNewClient(TCPSocketServer & server)
tass 0:f9e04607effd 48 {
tass 0:f9e04607effd 49 TCPSocketConnection *client =new TCPSocketConnection();
tass 0:f9e04607effd 50 server.accept(*client);
tass 0:f9e04607effd 51 client->set_blocking(false, 1500); // Timeout after (1.5)s
tass 0:f9e04607effd 52 printf("Connection from: %s\n", client->get_address());
tass 0:f9e04607effd 53 if(clientList.put((void *)client) != osOK) // queue is full?
tass 0:f9e04607effd 54 delete(client);
tass 0:f9e04607effd 55 }
tass 0:f9e04607effd 56
tass 0:f9e04607effd 57 void TCPClientThread(const void * arg)
tass 0:f9e04607effd 58 {
tass 0:f9e04607effd 59
tass 0:f9e04607effd 60 while(true)
tass 0:f9e04607effd 61 {
tass 0:f9e04607effd 62 TCPSocketConnection * client = NULL;
tass 0:f9e04607effd 63 char buffer[1024];
tass 0:f9e04607effd 64 int dataReceived = 0;
tass 0:f9e04607effd 65 int dataSent = 0;
tass 0:f9e04607effd 66
tass 0:f9e04607effd 67 // client stream
tass 0:f9e04607effd 68 osEvent evt = clientList.get();
tass 0:f9e04607effd 69 if(evt.status == osEventMessage)
tass 0:f9e04607effd 70 client = (TCPSocketConnection *)evt.value.p;
tass 0:f9e04607effd 71 else
tass 0:f9e04607effd 72 continue;
tass 0:f9e04607effd 73
tass 0:f9e04607effd 74 while(true)
tass 0:f9e04607effd 75 {
tass 0:f9e04607effd 76 printf("\n\n\nStarting the receiving part...\n");
tass 0:f9e04607effd 77 while(dataReceived < BUFFER_QUANTITY)
tass 0:f9e04607effd 78 {
tass 0:f9e04607effd 79 int n = client->receive(buffer, sizeof(buffer));
tass 0:f9e04607effd 80 if (n <= 0) {
tass 0:f9e04607effd 81 printf("Receive error\n");
tass 0:f9e04607effd 82 break;
tass 0:f9e04607effd 83 }
tass 0:f9e04607effd 84 dataReceived += n;
tass 0:f9e04607effd 85 }
tass 0:f9e04607effd 86
tass 0:f9e04607effd 87
tass 0:f9e04607effd 88 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY);
tass 0:f9e04607effd 89 if(dataReceived < BUFFER_QUANTITY)
tass 0:f9e04607effd 90 {
tass 0:f9e04607effd 91 printf("Receiving part of the test has failed. Exiting connection.\n");
tass 0:f9e04607effd 92 break;
tass 0:f9e04607effd 93 }
tass 0:f9e04607effd 94 else{
tass 0:f9e04607effd 95 printf("Receiving has passed...\n");
tass 0:f9e04607effd 96 }
tass 0:f9e04607effd 97
tass 0:f9e04607effd 98 printf("\n\n\nStarting the sending part...\n");
tass 0:f9e04607effd 99 while(dataSent < BUFFER_QUANTITY)
tass 0:f9e04607effd 100 {
tass 0:f9e04607effd 101 int n = client->send_all(buffer, sizeof(buffer));
tass 0:f9e04607effd 102 if (n <= 0) {
tass 0:f9e04607effd 103 printf("Send error : %d\n",n);
tass 0:f9e04607effd 104 break;
tass 0:f9e04607effd 105 }
tass 0:f9e04607effd 106 dataSent += n;
tass 0:f9e04607effd 107 }
tass 0:f9e04607effd 108
tass 0:f9e04607effd 109 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY);
tass 0:f9e04607effd 110 if(dataSent < BUFFER_QUANTITY)
tass 0:f9e04607effd 111 {
tass 0:f9e04607effd 112 printf("Sending part of the test has failed. Exiting connection.\n");
tass 0:f9e04607effd 113 break;
tass 0:f9e04607effd 114 }
tass 0:f9e04607effd 115 else
tass 0:f9e04607effd 116 {
tass 0:f9e04607effd 117 printf("Sending test has passed...\n");
tass 0:f9e04607effd 118 }
tass 0:f9e04607effd 119
tass 0:f9e04607effd 120
tass 0:f9e04607effd 121 printf("\n\n\nStarting echo part...\n");
tass 0:f9e04607effd 122 dataReceived = dataSent = 0;
tass 0:f9e04607effd 123 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:f9e04607effd 124 {
tass 0:f9e04607effd 125 int n = client->receive(buffer, sizeof(buffer));
tass 0:f9e04607effd 126 if (n <= 0) {
tass 0:f9e04607effd 127 printf("Receive error\n");
tass 0:f9e04607effd 128 break;
tass 0:f9e04607effd 129 }
tass 0:f9e04607effd 130 dataReceived += n;
tass 0:f9e04607effd 131
tass 0:f9e04607effd 132 n = client->send_all(buffer, n);
tass 0:f9e04607effd 133 if (n <= 0) {
tass 0:f9e04607effd 134 printf("Send error\n");
tass 0:f9e04607effd 135 break;
tass 0:f9e04607effd 136 }
tass 0:f9e04607effd 137 dataSent += n;
tass 0:f9e04607effd 138 }
tass 0:f9e04607effd 139
tass 0:f9e04607effd 140 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY);
tass 0:f9e04607effd 141 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:f9e04607effd 142 {
tass 0:f9e04607effd 143 printf("Echo test has failed.Exiting connection...\n");
tass 0:f9e04607effd 144 }
tass 0:f9e04607effd 145 else
tass 0:f9e04607effd 146 {
tass 0:f9e04607effd 147 printf("Echo test has passed...\n");
tass 0:f9e04607effd 148 }
tass 0:f9e04607effd 149
tass 0:f9e04607effd 150
tass 0:f9e04607effd 151 printf("Test was finished...\n");
tass 0:f9e04607effd 152 }
tass 0:f9e04607effd 153 // droping out client
tass 0:f9e04607effd 154 client->close();
tass 0:f9e04607effd 155 delete(client);
tass 0:f9e04607effd 156 break;
tass 0:f9e04607effd 157 }
tass 0:f9e04607effd 158 }
tass 0:f9e04607effd 159
tass 0:f9e04607effd 160 int main()
tass 0:f9e04607effd 161 {
tass 0:f9e04607effd 162 EthernetInterface eth;
tass 0:f9e04607effd 163 eth.init(); //Use DHCP
tass 0:f9e04607effd 164 eth.connect();
tass 0:f9e04607effd 165 printf("IP Address %s\n", eth.getIPAddress());
tass 0:f9e04607effd 166
tass 0:f9e04607effd 167 // startUDP Thread
tass 0:f9e04607effd 168 startUDPThread();
tass 0:f9e04607effd 169
tass 0:f9e04607effd 170
tass 0:f9e04607effd 171 TCPSocketServer server;
tass 0:f9e04607effd 172 server.bind(ECHO_SERVER_PORT);
tass 0:f9e04607effd 173 server.listen();
tass 0:f9e04607effd 174 // start client threads
tass 0:f9e04607effd 175 for(int i=0;i<NUMBER_OF_THREADS;i++)
tass 0:f9e04607effd 176 {
tass 0:f9e04607effd 177 new Thread(TCPClientThread);
tass 0:f9e04607effd 178 }
tass 0:f9e04607effd 179 printf("Started TCP server...\n");
tass 0:f9e04607effd 180
tass 0:f9e04607effd 181 while (true)
tass 0:f9e04607effd 182 {
tass 0:f9e04607effd 183 acceptNewClient(server);
tass 0:f9e04607effd 184 }
tass 0:f9e04607effd 185 }