Statistics
| Branch: | Revision:

napa-baselibs / tests / MLTest / MLTest.c @ 507372bb

History | View | Annotate | Download (6 KB)

1
#include <event2/event.h>
2
#include <unistd.h>
3
#include <string.h>
4
#include <math.h>
5

    
6
#include "ml.h"
7
#include "napa_log.h"
8
#include "napa.h"
9
#include "transmissionHandler.h"
10

    
11
int peerID;
12

    
13
int numOfBytes;
14

    
15
char local_id[SOCKETID_STRING_SIZE];
16

    
17
/**
18
  * define a maximum number of connections
19
  */
20
#define maxConnections 10000
21

    
22
/**
23
  * an connections array
24
  */
25
int connections[maxConnections];
26

    
27
/**
28
  * the initial number of connections
29
  */
30
int nr_connections = 0;
31

    
32
int connection_argument;
33

    
34
int timeInterval;
35
int numOfMsg;
36
int msgLen;
37

    
38
#define EXECMAX 400
39

    
40
void send_data_to_peer(int fd, short event,void *arg){
41
        
42
        static int execNum = 0;
43
        static char letter = 0x41;        
44
        int connectionID = 0;        
45
        
46
        if (execNum == EXECMAX) {
47
                //mlCloseConnection(connectionID);
48
                //info("[MLTest} - Connection closed.");        
49
                return;
50
        }
51

    
52
        info("%d.[MLTest} - Sending data to peer 2...", execNum);
53

    
54
        char* buffer;
55
        buffer = (char*) malloc(msgLen);
56
        unsigned char msgtype = 12;
57
        int i;
58

    
59
        for (i=0; i < numOfMsg; i++) {        
60
                memset(buffer,letter, msgLen);
61
                buffer[msgLen-1] = 0;
62
                mlSendData(connectionID,buffer,msgLen,msgtype,NULL);
63
                info("[MLTest} - Message '%c' has been sent.",letter);
64
                letter++;
65
                if (letter > 0x7d ) letter = 0x41;                
66
        }
67
        
68
              free(buffer);
69

    
70
        info("[MLTest} - Sending next data in %d seconds...", timeInterval);
71
        struct timeval timeout_value = {timeInterval, 0};
72
        struct event *ev;
73
        ev = evtimer_new(eventbase,send_data_to_peer,NULL);
74
        event_add(ev,&timeout_value);
75

    
76
        execNum++;
77
}
78

    
79
void send_data_to_peer2(int fd, short event,void *arg){
80
      info("[MLTest} - Sending data to peer 2...");
81
      /// transmit data to peer 2  
82
      //int size = 30;
83
      char* buffer;
84
      buffer = (char*) malloc(numOfBytes);
85

    
86
      memset(buffer,'a',numOfBytes);
87
      buffer[numOfBytes-1] = 0;
88
      unsigned char msgtype = 12;
89
      int connectionID = 0;
90
      //strcpy(buffer, "Test message to peer 2");
91
//      mlSetThrottle(10, 1);
92
      mlSendData(connectionID,buffer,numOfBytes,msgtype,NULL);
93
      info("[MLTest} - Data has been sent.");
94
      mlCloseConnection(connectionID);
95
      info("[MLTest} - Connection closed.");
96
      free(buffer);
97
      //exit(0);
98
}
99

    
100

    
101
void receive_conn_cb (int connectionID, void *arg){
102

    
103
  info("[MLTest} - Received connection from callback: connectionID:%d", connectionID);
104

    
105
  connections[nr_connections] = connectionID;
106
  nr_connections++;
107

    
108
//  mlSetThrottle(8, 8);                        //kB,kB/s
109

    
110
  if(peerID == 1){
111
          info("[MLTest} - Sending data in 3 seconds...");
112
          struct timeval timeout_value = {3,0};
113
          struct event *ev;
114
          ev = evtimer_new(eventbase,send_data_to_peer,NULL);
115
          event_add(ev,&timeout_value);
116
  }
117

    
118
} 
119

    
120
socketID_handle getRemoteSocketID(int peerID){
121

    
122
  socketID_handle remote_socketID = malloc(SOCKETID_SIZE);
123
  int port;
124

    
125
  switch (peerID){
126

    
127
  case 1:
128
    port = 9001;
129
    break;
130

    
131
  case 2:
132
    port = 9002;
133
    break;
134
    
135
  case 3:
136
    port = 9003;
137
    break;
138

    
139
  }
140

    
141
  struct sockaddr_in udpaddr;
142
  udpaddr.sin_family = AF_INET;
143
  udpaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
144
  udpaddr.sin_port = htons(port);
145
  
146
  socketaddrgen udpgen;
147
  udpgen.udpaddr = udpaddr;
148
  remote_socketID->internal_addr = udpgen;
149
  remote_socketID->external_addr = udpgen;
150

    
151
  return remote_socketID;
152

    
153
}
154

    
155
void open_connection_cb(int fd, short event,void *arg){
156

    
157
        send_params defaultSendParams;
158
        char remote_id[SOCKETID_STRING_SIZE];        
159
        
160
        socketID_handle rm_socketID = getRemoteSocketID(2);
161
        mlSocketIDToString(rm_socketID, remote_id, sizeof(remote_id));        
162
        
163
        info("[MLTest} - Openning a connection to %s", remote_id);        
164
        int con_id = mlOpenConnection(rm_socketID, &receive_conn_cb, &connection_argument, defaultSendParams); 
165
          
166
        ///If the result is zero the event is resheduled into the event list.
167
        if(con_id < 0){
168

    
169
                error("[MLTest} - Openning connection failed. Openning next connection in 4 seconds...");
170
                /* ReTry to open a connection after 4 seconds */
171
                struct timeval timeout_value_open_conn = {4,0};
172
                struct event *ev;
173
                ev = evtimer_new(eventbase,open_connection_cb,NULL);
174
                event_add(ev,&timeout_value_open_conn);
175

    
176
          }
177

    
178
}
179

    
180

    
181
/* Called after the ML finished initialising */
182
void receive_local_socketID_cb(socketID_handle local_socketID, int status){
183
        int res, conID;
184
        char str[SOCKETID_STRING_SIZE];
185
        
186
        if(status) {
187
                info("[MLTest] - Still trying to do NAT traversal");
188
                return;
189
        }
190

    
191
        info("[MLTest] - Nat traversal completed");
192

    
193
        mlSocketIDToString(local_socketID, local_id, sizeof(str));
194
        info("[MLTest] - My local SocketId is: %s", local_id);
195

    
196
        
197
        if (peerID == 1) {
198
                /// Try to open a connection after 4 seconds
199
                info("[MLTest} - Openning connection in 4 seconds...");                
200
                struct timeval timeout_value_open_conn = {4,0};
201
                struct event *ev1;
202
                ev1 = evtimer_new(eventbase,open_connection_cb,NULL);
203
                event_add(ev1,&timeout_value_open_conn);        
204
        }
205
        
206
}
207

    
208
void recv_data_from_peer_cb(char *buffer,int buflen,unsigned char msgtype,void *arg){
209
        buffer[20] = 0;
210
  info("[MLTest] - Received data: %s\n",buffer);
211

    
212

    
213
}
214

    
215
/* Called if the connection opening fails */
216
void conn_fail_cb(int connectionID, void *arg){
217
        error("[MLTest] - Connection ID:%d could not be established!",connectionID);
218
}
219

    
220
int main(int argc, char *argv[]) {
221
        char *stun_server = "localhost";
222
        char *bind_ip = NULL;
223
        int verbosity = 100;
224

    
225
        if (argc < 2){
226

    
227
                printf("Usage: %s <peerID>\n", argv[0]);
228
                exit(1);
229

    
230
        }
231

    
232
        peerID = atoi(argv[1]);
233
        
234
        if (peerID < 1 || peerID > 2){
235

    
236
                printf("Usage: <peerID> has to be 1 or 2\n");
237
                exit(1);
238
        }
239
        
240
        int inputRate = 10 *1024;                //kB/s
241

    
242
        msgLen = 2500;
243
        timeInterval = 1;
244

    
245
        numOfMsg = inputRate / ((msgLen/1349 + 1)*23 + msgLen)*timeInterval;        
246

    
247
        //Init napa: log facility and libevent
248
        napaInit(event_base_new());
249

    
250
        // Initialize logging
251
        napaInitLog(verbosity, NULL, NULL);
252

    
253
        //initRateLimiter(eventbase);
254

    
255
        info("Running peer ID: %d", peerID);
256

    
257
        // Init messaging layer
258
        mlRegisterErrorConnectionCb(&conn_fail_cb);
259

    
260
        // Register a recv data callback  
261
        mlRegisterRecvDataCb(&recv_data_from_peer_cb,12);
262

    
263
        struct timeval timeout = {3,0};
264
        mlInit(true, timeout, 9000+atoi(argv[1]), bind_ip, 3478, stun_server,
265
                        &receive_local_socketID_cb, (void*)eventbase);
266

    
267
        //Start everything
268
        event_base_dispatch(eventbase);
269
}