Bonjour
J'ai un programme en C que j'exécute dans une console pour qu'il se connecte à un serveur MMTP normalement certaines données qui ont été transférés d'un serveur sont stockés dans un fichier avec une fonction qui est appelée OutputFile()
le problème c'est que je dois afficher dans ma console ces données au lieu d'afficher les étapes de connexions avec le serveur
le programme qu'il m'on m'a donné est :
les modifications que j'ai fait et qui m'ont donné aucun résultats :s :s
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709 #ifdef NT #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #if defined(AIX) || defined(SUNOS) || defined(UNIX_SV) || defined(HPUX) #ifndef UNIX #define UNIX #endif #include <signal.h> #include <sys/types.h> #include <sys/time.h> typedef int BOOL; #define TRUE 1 #define FALSE 0 #endif #include "libpga.h" static char* Receiver_c_Rev _UNUSED_="@(#) $Name: LIV_MMTP_SDK_2_14_p1 $ $Header: C:/receiver/receiver.c,v 1.5 1999/07/02 08:34:18 fred Exp $"; // Constants. // ---------- #define REPLY_TIMEOUT 10000 #define LOOP_DELAY 100 #define WINDOW_SIZE 50 // Global variables prototypes. // ---------------------------- // Handle of the MMTP session. MMTPSESSION Session; // Last sequence number received by the server. int LastSeqNb=0; // Indicate if a Ctrl+C keystroke has been received. static BOOL Interrupt=FALSE; // Must quit the program ? BOOL Quit=TRUE; // Handle of the output file. FILE* OutputFile=NULL; // Tcp port of the server. short TcpPort; // IP Address of the server. char IpAddr[50]; // User name. char User[50]; // Password of the user. char Password[50]; // MsgId of the last message received. static char LastMsgIdReceived[FIELD_SIZE_MSGID+1]; // Static functions prototypes. // ---------------------------- static FILE* OpenOutputFile(char* Filename); static BOOL ConnectServer(void); static void DisconnectServer(MMTPDcnxRsn Reason); static BOOL ReceiveServerMessage(void); static void ProcessConnectionAcceptance(void); static void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg); static void ProcessDataMessage(MMTPMsg* Msg); static int ProcessMmtpError(int Error); #ifdef NT BOOL WINAPI ControlHandler(DWORD Signal); #define WaitMs(x) Sleep(x) #elif defined(UNIX) void ControlHandler(int Signal); void WaitMs(unsigned long MilliSeconds); #endif /******************************************************************************* * int main(int argc,char** argv) * * Description * Main function of the program. * * Parameters * argc * Number of parameters * * argv * Parameters of the program. * * Return code * Always 0. *******************************************************************************/ int main(int argc,char** argv) { int rc; BOOL MustWait; if(argc!=6) { puts("Missing parameter(s)"); puts("Syntax : Receiver <Port> <IpAddr> <User> <Password> <Output file>"); return(0); } // Catch interrupt signals #ifdef NT SetConsoleCtrlHandler(ControlHandler,TRUE); #elif defined(UNIX) signal(SIGQUIT,ControlHandler); signal(SIGKILL,ControlHandler); signal(SIGTERM,ControlHandler); signal(SIGINT,ControlHandler); #endif TcpPort=(short)atoi(argv[1]); strcpy(IpAddr,argv[2]); strcpy(User,argv[3]); strcpy(Password,argv[4]); // Open the output file. OutputFile=OpenOutputFile(argv[5]); if(OutputFile!=NULL) { // Initialize the MMTP library (for one session only). rc=MMTPInitialize(1); if(rc==MMTP_OK) { // Create the session. rc=MMTPCreateSession(NULL,NULL,&Session); if(rc==MMTP_OK) { Quit=FALSE; } // Error during create session. else { printf( "Error in MMTPCreateSession : %d - %s\n",rc,MMTPErrorDescription(rc)); } } // Error during MMTP library initialization. else { printf("Error in MMTPInitialize : %d - %s\n",rc,MMTPErrorDescription(rc)); } } // While the end is not asked or the logical connection exists. while(Quit==FALSE || MMTPCheckLogicalConnection(Session)==TRUE) { MustWait=FALSE; if(Interrupt==TRUE) { printf("Program interrupted by signal\n"); DisconnectServer(MMTPDcnxRsn_Normal); Interrupt=FALSE; } // If the logical connection with the server is not established. if(MMTPCheckPhysicalConnection(Session)==FALSE) { if(Quit==FALSE) { if(ConnectServer()==FALSE) { MustWait=TRUE; } } } else { rc=MMTPTerminateWrite(Session); if(rc==MMTP_OK) { if(ReceiveServerMessage()==FALSE) { MustWait=TRUE; } } else if(rc!=MMTP_BUFFER_FULL) { ProcessMmtpError(rc); WaitMs(10); } } if(MustWait==TRUE) { WaitMs(LOOP_DELAY); } } // Delete the session if its handle is valid. if(MMTPCheckHandle(Session)==TRUE) { MMTPDeleteSession(Session); } // Close the library. MMTPTerminate(); // Close the file, if opened. if(OutputFile!=NULL) { fclose(OutputFile); } return(0); } /******************************************************************************* * FILE* OpenOutputFile(char* Filename) * * Description * Opens the file where the received message will be store. * If the file exists, get the MsgId of the last message received during * previous execution of the program. * * Parameters * Filename * Name of the output file. * * Return code * A handle of the file if the opening is successful. Otherwise NULL. *******************************************************************************/ static FILE* OpenOutputFile(char* Filename) { char Buffer[FIELD_SIZE_DATA+1]; int NbLines=0; FILE* f=NULL; LastMsgIdReceived[0]=0; // Try to open an existing file. f=fopen(Filename,"a+"); // If the opening is successful. if(f!=NULL) { // Find the last line of the file to gets the MsgId of the last msg received. while(fgets(Buffer,sizeof(Buffer),f)!=NULL) { NbLines++; } if(NbLines!=0 && (strlen(Buffer)<FIELD_SIZE_MSGID+3 || memcmp(Buffer+FIELD_SIZE_MSGID," - ",3))) { printf("The output file is invalid\n"); } else { if(NbLines!=0) { memcpy(LastMsgIdReceived,Buffer,FIELD_SIZE_MSGID); LastMsgIdReceived[FIELD_SIZE_MSGID]=0; } return(f); } } else { printf("Unable to open the file\n"); } if(f!=NULL) { fclose(f); } return(NULL); } /******************************************************************************* * BOOL ConnectServer() * * Description * Try to establish physical connection with the server. * * Parameters * None * * Return code * TRUE if the physical connection is established. Otherwise FALSE. *******************************************************************************/ static BOOL ConnectServer() { int rc; printf("Try to establish the physical connection with the server\n"); // Try to establish physical connection with the server. rc=MMTPOpenClientSession(Session,TcpPort,IpAddr,0); if(rc==MMTP_OK) { printf("Physical connection with the server established\n"); return(TRUE); } if(rc!=MMTP_NOT_CONNECTED) { ProcessMmtpError(rc); } return(FALSE); } /******************************************************************************* * void DisconnectServer(MMTPDcnxRsn Reason) * * Description * Closes the connection with the server. Sends a disconnection request if the * logical connection with the server is established. * * Parameters * Reason * The reason code of the disconnection. * * Return code * None *******************************************************************************/ static void DisconnectServer(MMTPDcnxRsn Reason) { Quit=TRUE; if(MMTPCheckLogicalConnection(Session)==TRUE && MMTPTerminateWrite(Session)==MMTP_OK) { printf( "Send disconnection request to the server (reason %d, SeqNb %d)\n", Reason,LastSeqNb); ProcessMmtpError( MMTPDisconnectionRequest(Session,Reason,LastSeqNb)); return; } if(MMTPCheckPhysicalConnection(Session)==TRUE) { printf("Physical connection with server is closed\n"); ProcessMmtpError(MMTPCloseSession(Session)); } } /******************************************************************************* * BOOL ReceiveServerMessage() * * Description * Receives messages from the server processes them. * * Parameters * Reason * The reason code of the disconnection. * * Return code * TRUE if a message has been received, FALSE otherwise. *******************************************************************************/ static BOOL ReceiveServerMessage() { int rc; int Duration; MMTPMsg Msg; // If the logical connection with the server is not established. if(MMTPCheckLogicalConnection(Session)==FALSE) { // Get the time passed since the sending of the connection request. Duration=MMTPCheckConnectionRequest(Session); // If no connection request has been sent, sends one. if(Duration==-1) { printf("Sends a connection request.\n"); ProcessMmtpError( MMTPConnectionRequest(Session,User,"0000000000000000",Password)); } // If timeout, Cancels the connection request. else if(Duration>REPLY_TIMEOUT) { printf("Timeout on connection request. Cancels it.\n"); ProcessMmtpError(MMTPCancelConnectionRequest(Session)); Quit=TRUE; return(TRUE); } } if(MMTPCheckDisconnectionRequest(Session)>REPLY_TIMEOUT) { printf("Timeout on connection request. Cancels it.\n"); ProcessMmtpError(MMTPCancelDisconnectionRequest(Session)); return(TRUE); } // Look if a message is received. rc=MMTPReceiveMessage(Session,0,&Msg); // Process the return code. switch(rc) { // If a message has been received. case MMTP_OK: // Process the type of the received message. switch(Msg.Type) { // The connection request is accepted. case CONX_ACK: ProcessConnectionAcceptance(); break; // The connection request is denied. case CONX_NACK: printf( "Connection request denied (reason %d)\n",Msg.Data.ConxNack.Reason); MMTPCloseSession(Session); Quit=TRUE; break; // Disconnection request. case DCNX_REQ: printf( "Disconnection request received (SeqNb %d)\n", Msg.Data.DcnxReq.SeqNb); MMTPAcceptDisconnection(Session,LastSeqNb); Quit=TRUE; break; // The disconnection request is accepted. case DCNX_ACK: printf( "Disconnection request accepted (SeqNb %d)\n", Msg.Data.DcnxAck.SeqNb); break; // Start request are refused. case START_REQ: printf( "%s received. Sending error\n",MMTPObtainPrimitiveName(Msg.Type)); MMTPSendError(Session,MMTPErrIndErr_OutOfContext,0,LastSeqNb,&Msg); break; case START_ACK: printf( "Start request accepted (MsgId <%s>, SeqNb %d)\n", Msg.Data.StartAck.MsgId,Msg.Data.StartAck.SeqNb); LastSeqNb=Msg.Data.StartAck.SeqNb-1; break; case START_NACK: printf( "Start request refused (reason %d)\n",Msg.Data.StartNack.Reason); break; // Data message. case DATA_MSG: ProcessDataMessage(&Msg); break; // Sync request. case SYNC_REQ: printf( "Sync request received. Replies to it (SeqNb %d, MsgId <%s>)\n", LastSeqNb,LastMsgIdReceived); ProcessMmtpError(MMTPAcceptSync(Session,LastSeqNb,LastMsgIdReceived)); break; // Service message. case SRVC_MSG: ProcessServiceMessage(&Msg.Data.SrvcMsg); break; // Other message. default: printf( "%s message received and ignored\n", MMTPObtainPrimitiveName(Msg.Type)); } break; // The physical connection is lost. case MMTP_CNX_LOST: printf("Physical connection lost\n"); break; // No message received. case MMTP_NO_MESSAGE: return(FALSE); // Other return code. default: ProcessMmtpError(rc); } return(TRUE); } /******************************************************************************* * void ProcessConnectionAcceptance() * * Description * Processes a conx-ack message. * Sends a start-req message to the server. * * Parameters * None * * Return code * None *******************************************************************************/ static void ProcessConnectionAcceptance() { printf("Connection request accepted\n"); printf("Send a start request (MsgId <%s>)\n",LastMsgIdReceived); ProcessMmtpError(MMTPStartRequest(Session,LastMsgIdReceived)); } /******************************************************************************* * void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg) * * Description * Processes a service message received from the server. * Sends a PONG service message if the service message received is a PING. * Ignores any other service messages. * * Parameters * SrvcMsg * Service message received from the server. * * Return code * None *******************************************************************************/ static void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg) { // if the service message is PING, reply a PONG. if(!strcmp(SrvcMsg->Type,"PING")) { MMTPSendServiceMessage(Session,"PONG",SrvcMsg->Size,SrvcMsg->Data); } // else, ignored the message. else { printf("The %s service message received is unknown\n",SrvcMsg->Type); } } /******************************************************************************* * void ProcessDataMessage(MMTP* Msg) * * Description * Processes a data message received from the server. * Stores it in the output file. * Send an err-ind message if there is a break in the sequence number. * * Parameters * Msg * Message received from the server. * * Return code * None *******************************************************************************/ static void ProcessDataMessage(MMTPMsg* Msg) { MMTPDataMsg* DataMsg=&Msg->Data.DataMsg; // For simplify the sample, we just store the message id // in memory. In a real application, it is necessary to // store it in a file or a database. strcpy(LastMsgIdReceived,DataMsg->AD.Data.Generic.MsgId); printf( "Data message received (SeqNb %d, MsgId %s)\n", DataMsg->SeqNb,LastMsgIdReceived); // If the message has already been received, close the session if(DataMsg->SeqNb<=LastSeqNb) { puts("Sequence number already received."); ProcessMmtpError( MMTPSendError(Session,MMTPErrIndErr_MsgDuplicate,0,LastSeqNb,Msg)); ProcessMmtpError(MMTPCloseSession(Session)); Quit=TRUE; } // If the sequence number of the message is not the one we are // waiting for, we close the session. else if(DataMsg->SeqNb!=LastSeqNb+1) { puts("Break in sequence number."); ProcessMmtpError( MMTPSendError(Session,MMTPErrIndErr_MsgLost,1,LastSeqNb,Msg)); ProcessMmtpError(MMTPCloseSession(Session)); Quit=TRUE; } // The sequence number of the message is the good one. else { LastSeqNb++; fprintf( OutputFile,"%-*s - %s\n", FIELD_SIZE_MSGID,LastMsgIdReceived,DataMsg->Data); } } /******************************************************************************* * int ProcessMmtpError(int Error) * * Description * Process MMTP return codes. * * Parameters * Error * Return code to process. * * Return code * The return code given in parameter. *******************************************************************************/ int ProcessMmtpError(int Error) { switch(Error) { case MMTP_OK: case MMTP_NO_MESSAGE: break; case MMTP_CNX_LOST: printf("MMTP physical connection lost\n"); break; case MMTP_NOT_CONNECTED: printf("MMTP logical connection not established\n"); break; case MMTP_BUFFER_FULL: printf("Warning : the server socket is full\n"); break; case MMTP_CANNOT_INIT_SOCKET: case MMTP_NOT_ENOUGH_MEMORY: case MMTP_TOO_MUCH_SESSIONS: case MMTP_SOCKET_ERROR: case MMTP_INVALID_ADDRESS: case MMTP_INVALID_ARG: case MMTP_INVALID_ARG1: case MMTP_INVALID_ARG2: case MMTP_INVALID_ARG3: case MMTP_INVALID_ARG4: case MMTP_INVALID_ARG5: case MMTP_INVALID_ARG6: case MMTP_INVALID_ARG7: case MMTP_INVALID_ARG8: case MMTP_INVALID_ARG9: case MMTP_CLIENT_FCT: case MMTP_SERVER_FCT: case MMTP_INVALID_SESSION: case MMTP_NOT_INITIALIZED: case MMTP_SYSTEM_EXCEPTION: printf( "Fatal error in MMTP connection : %d - %s\n", Error,MMTPErrorDescription(Error)); DisconnectServer(MMTPDcnxRsn_Abnormal); Quit=TRUE; break; default: printf( "Error in MMTP connection : %d - %s\n", Error,MMTPErrorDescription(Error)); } return(Error); } #ifdef NT BOOL WINAPI ControlHandler(DWORD Signal) { if(Signal==CTRL_BREAK_EVENT || Signal==CTRL_C_EVENT || Signal==CTRL_CLOSE_EVENT) { Interrupt=TRUE; return(TRUE); } return(FALSE); } #elif defined(UNIX) void ControlHandler(int Signal) { Interrupt=TRUE; } void WaitMs(unsigned long MilliSeconds) { struct timeval t={ MilliSeconds/1000, (MilliSeconds%1000)*1000 }; select(0,NULL,NULL,NULL,&t); } #endif
pleaaz help
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714 #ifdef NT #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #if defined(AIX) || defined(SUNOS) || defined(UNIX_SV) || defined(HPUX) #ifndef UNIX #define UNIX #endif #include <signal.h> #include <sys/types.h> #include <sys/time.h> typedef int BOOL; #define TRUE 1 #define FALSE 0 #endif #include "libpga.h" static char* Receiver_c_Rev _UNUSED_="@(#) $Name: LIV_MMTP_SDK_2_14_p1 $ $Header: C:/receiver/receiver.c,v 1.5 1999/07/02 08:34:18 fred Exp $"; // Constants. // ---------- #define REPLY_TIMEOUT 10000 #define LOOP_DELAY 100 #define WINDOW_SIZE 50 // Global variables prototypes. // ---------------------------- // Handle of the MMTP session. MMTPSESSION Session; // Last sequence number received by the server. int LastSeqNb=0; // Indicate if a Ctrl+C keystroke has been received. static BOOL Interrupt=FALSE; // Must quit the program ? BOOL Quit=TRUE; // Handle of the output file. FILE* OutputFile=NULL; // Tcp port of the server. short TcpPort; // IP Address of the server. char IpAddr[50]; // User name. char User[50]; // Password of the user. char Password[50]; // MsgId of the last message received. static char LastMsgIdReceived[FIELD_SIZE_MSGID+1]; // Static functions prototypes. // ---------------------------- //static FILE* OpenOutputFile(char* Filename); static BOOL ConnectServer(void); static void DisconnectServer(MMTPDcnxRsn Reason); static BOOL ReceiveServerMessage(void); static void ProcessConnectionAcceptance(void); static void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg); static void ProcessDataMessage(MMTPMsg* Msg); static int ProcessMmtpError(int Error); #ifdef NT BOOL WINAPI ControlHandler(DWORD Signal); #define WaitMs(x) Sleep(x) #elif defined(UNIX) void ControlHandler(int Signal); void WaitMs(unsigned long MilliSeconds); #endif /******************************************************************************* * int main(int argc,char** argv) * * Description * Main function of the program. * * Parameters * argc * Number of parameters * * argv * Parameters of the program. * * Return code * Always 0. *******************************************************************************/ int main(int argc,char** argv) { int rc; BOOL MustWait; if(argc!=5) { puts("Missing parameter(s)"); puts("Syntax : Receiver <Port> <IpAddr> <User> <Password> "); return(0); } // Catch interrupt signals #ifdef NT SetConsoleCtrlHandler(ControlHandler,TRUE); #elif defined(UNIX) signal(SIGQUIT,ControlHandler); signal(SIGKILL,ControlHandler); signal(SIGTERM,ControlHandler); signal(SIGINT,ControlHandler); #endif TcpPort=(short)atoi(argv[1]); strcpy(IpAddr,argv[2]); strcpy(User,argv[3]); strcpy(Password,argv[4]); char Buffer[FIELD_SIZE_DATA+1]; LastMsgIdReceived[0]=0; int a = 1; // Open the output file. //OutputFile=OpenOutputFile(argv[5]); if(a=1) { // Initialize the MMTP library (for one session only). memcpy(LastMsgIdReceived,Buffer,FIELD_SIZE_MSGID); LastMsgIdReceived[FIELD_SIZE_MSGID]=0; rc=MMTPInitialize(1); if(rc==MMTP_OK) { // Create the session. rc=MMTPCreateSession(NULL,NULL,&Session); if(rc==MMTP_OK) { puts(Buffer); Quit=FALSE; } // Error during create session. else { printf( "Error in MMTPCreateSession : %d - %s\n",rc,MMTPErrorDescription(rc)); } } // Error during MMTP library initialization. else { printf("Error in MMTPInitialize : %d - %s\n",rc,MMTPErrorDescription(rc)); }} // While the end is not asked or the logical connection exists. while(Quit==FALSE || MMTPCheckLogicalConnection(Session)==TRUE) { MustWait=FALSE; if(Interrupt==TRUE) { printf("Program interrupted by signal\n"); DisconnectServer(MMTPDcnxRsn_Normal); Interrupt=FALSE; } // If the logical connection with the server is not established. if(MMTPCheckPhysicalConnection(Session)==FALSE) { if(Quit==FALSE) { if(ConnectServer()==FALSE) { MustWait=TRUE; } } } else { rc=MMTPTerminateWrite(Session); if(rc==MMTP_OK) { if(ReceiveServerMessage()==FALSE) { MustWait=TRUE; } } else if(rc!=MMTP_BUFFER_FULL) { ProcessMmtpError(rc); // WaitMs(10); } } if(MustWait==TRUE) { // WaitMs(LOOP_DELAY); } } // Delete the session if its handle is valid. if(MMTPCheckHandle(Session)==TRUE) { MMTPDeleteSession(Session); } // Close the library. MMTPTerminate(); // Close the file, if opened. if(OutputFile!=NULL) { fclose(OutputFile); } return(0); } /******************************************************************************* * FILE* OpenOutputFile(char* Filename) * * Description * Opens the file where the received message will be store. * If the file exists, get the MsgId of the last message received during * previous execution of the program. * * Parameters * Filename * Name of the output file. * * Return code * A handle of the file if the opening is successful. Otherwise NULL. *******************************************************************************/ /*static FILE* OpenOutputFile(char* Filename) { char Buffer[FIELD_SIZE_DATA+1]; int NbLines=0; FILE* f=NULL; LastMsgIdReceived[0]=0; // Try to open an existing file. f=fopen(Filename,"a+"); // If the opening is successful. if(f!=NULL) { // Find the last line of the file to gets the MsgId of the last msg received. while(fgets(Buffer,sizeof(Buffer),f)!=NULL) { NbLines++; } if(NbLines!=0 && (strlen(Buffer)<FIELD_SIZE_MSGID+3 || memcmp(Buffer+FIELD_SIZE_MSGID," - ",3))) { printf("The output file is invalid\n"); } else { if(NbLines!=0) { memcpy(LastMsgIdReceived,Buffer,FIELD_SIZE_MSGID); LastMsgIdReceived[FIELD_SIZE_MSGID]=0; } return(f); } } else { printf("Unable to open the file\n"); } if(f!=NULL) { fclose(f); } return(NULL); }*/ /******************************************************************************* * BOOL ConnectServer() * * Description * Try to establish physical connection with the server. * * Parameters * None * * Return code * TRUE if the physical connection is established. Otherwise FALSE. *******************************************************************************/ static BOOL ConnectServer() { int rc; printf("Try to establish the physical connection with the server\n"); // Try to establish physical connection with the server. rc=MMTPOpenClientSession(Session,TcpPort,IpAddr,0); if(rc==MMTP_OK) { printf("Physical connection with the server established\n"); return(TRUE); } if(rc!=MMTP_NOT_CONNECTED) { ProcessMmtpError(rc); } return(FALSE); } /******************************************************************************* * void DisconnectServer(MMTPDcnxRsn Reason) * * Description * Closes the connection with the server. Sends a disconnection request if the * logical connection with the server is established. * * Parameters * Reason * The reason code of the disconnection. * * Return code * None *******************************************************************************/ static void DisconnectServer(MMTPDcnxRsn Reason) { Quit=TRUE; if(MMTPCheckLogicalConnection(Session)==TRUE && MMTPTerminateWrite(Session)==MMTP_OK) { printf( "Send disconnection request to the server (reason %d, SeqNb %d)\n", Reason,LastSeqNb); ProcessMmtpError( MMTPDisconnectionRequest(Session,Reason,LastSeqNb)); return; } if(MMTPCheckPhysicalConnection(Session)==TRUE) { printf("Physical connection with server is closed\n"); ProcessMmtpError(MMTPCloseSession(Session)); } } /******************************************************************************* * BOOL ReceiveServerMessage() * * Description * Receives messages from the server processes them. * * Parameters * Reason * The reason code of the disconnection. * * Return code * TRUE if a message has been received, FALSE otherwise. *******************************************************************************/ static BOOL ReceiveServerMessage() { int rc; int Duration; MMTPMsg Msg; // If the logical connection with the server is not established. if(MMTPCheckLogicalConnection(Session)==FALSE) { // Get the time passed since the sending of the connection request. Duration=MMTPCheckConnectionRequest(Session); // If no connection request has been sent, sends one. if(Duration==-1) { printf("Sends a connection request.\n"); ProcessMmtpError( MMTPConnectionRequest(Session,User,"0000000000000000",Password)); } // If timeout, Cancels the connection request. else if(Duration>REPLY_TIMEOUT) { printf("Timeout on connection request. Cancels it.\n"); ProcessMmtpError(MMTPCancelConnectionRequest(Session)); Quit=TRUE; return(TRUE); } } if(MMTPCheckDisconnectionRequest(Session)>REPLY_TIMEOUT) { printf("Timeout on connection request. Cancels it.\n"); ProcessMmtpError(MMTPCancelDisconnectionRequest(Session)); return(TRUE); } // Look if a message is received. rc=MMTPReceiveMessage(Session,0,&Msg); // Process the return code. switch(rc) { // If a message has been received. case MMTP_OK: // Process the type of the received message. switch(Msg.Type) { // The connection request is accepted. case CONX_ACK: ProcessConnectionAcceptance(); break; // The connection request is denied. case CONX_NACK: printf( "Connection request denied (reason %d)\n",Msg.Data.ConxNack.Reason); MMTPCloseSession(Session); Quit=TRUE; break; // Disconnection request. case DCNX_REQ: printf( "Disconnection request received (SeqNb %d)\n", Msg.Data.DcnxReq.SeqNb); MMTPAcceptDisconnection(Session,LastSeqNb); Quit=TRUE; break; // The disconnection request is accepted. case DCNX_ACK: printf( "Disconnection request accepted (SeqNb %d)\n", Msg.Data.DcnxAck.SeqNb); break; // Start request are refused. case START_REQ: printf( "%s received. Sending error\n",MMTPObtainPrimitiveName(Msg.Type)); MMTPSendError(Session,MMTPErrIndErr_OutOfContext,0,LastSeqNb,&Msg); break; case START_ACK: printf( "Start request accepted (MsgId <%s>, SeqNb %d)\n", Msg.Data.StartAck.MsgId,Msg.Data.StartAck.SeqNb); LastSeqNb=Msg.Data.StartAck.SeqNb-1; break; case START_NACK: printf( "Start request refused (reason %d)\n",Msg.Data.StartNack.Reason); break; // Data message. case DATA_MSG: ProcessDataMessage(&Msg); break; // Sync request. case SYNC_REQ: printf( "Sync request received. Replies to it (SeqNb %d, MsgId <%s>)\n", LastSeqNb,LastMsgIdReceived); ProcessMmtpError(MMTPAcceptSync(Session,LastSeqNb,LastMsgIdReceived)); break; // Service message. case SRVC_MSG: ProcessServiceMessage(&Msg.Data.SrvcMsg); break; // Other message. default: printf( "%s message received and ignored\n", MMTPObtainPrimitiveName(Msg.Type)); } break; // The physical connection is lost. case MMTP_CNX_LOST: printf("Physical connection lost\n"); break; // No message received. case MMTP_NO_MESSAGE: return(FALSE); // Other return code. default: ProcessMmtpError(rc); } return(TRUE); } /******************************************************************************* * void ProcessConnectionAcceptance() * * Description * Processes a conx-ack message. * Sends a start-req message to the server. * * Parameters * None * * Return code * None *******************************************************************************/ static void ProcessConnectionAcceptance() { printf("Connection request accepted\n"); printf("Send a start request (MsgId <%s>)\n",LastMsgIdReceived); ProcessMmtpError(MMTPStartRequest(Session,LastMsgIdReceived)); } /******************************************************************************* * void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg) * * Description * Processes a service message received from the server. * Sends a PONG service message if the service message received is a PING. * Ignores any other service messages. * * Parameters * SrvcMsg * Service message received from the server. * * Return code * None *******************************************************************************/ static void ProcessServiceMessage(MMTPSrvcMsg* SrvcMsg) { // if the service message is PING, reply a PONG. if(!strcmp(SrvcMsg->Type,"PING")) { MMTPSendServiceMessage(Session,"PONG",SrvcMsg->Size,SrvcMsg->Data); } // else, ignored the message. else { printf("The %s service message received is unknown\n",SrvcMsg->Type); } } /******************************************************************************* * void ProcessDataMessage(MMTP* Msg) * * Description * Processes a data message received from the server. * Stores it in the output file. * Send an err-ind message if there is a break in the sequence number. * * Parameters * Msg * Message received from the server. * * Return code * None *******************************************************************************/ static void ProcessDataMessage(MMTPMsg* Msg) { MMTPDataMsg* DataMsg=&Msg->Data.DataMsg; // For simplify the sample, we just store the message id // in memory. In a real application, it is necessary to // store it in a file or a database. strcpy(LastMsgIdReceived,DataMsg->AD.Data.Generic.MsgId); printf( "Data message received (SeqNb %d, MsgId %s)\n", DataMsg->SeqNb,LastMsgIdReceived); // If the message has already been received, close the session if(DataMsg->SeqNb<=LastSeqNb) { puts("Sequence number already received."); ProcessMmtpError( MMTPSendError(Session,MMTPErrIndErr_MsgDuplicate,0,LastSeqNb,Msg)); ProcessMmtpError(MMTPCloseSession(Session)); Quit=TRUE; } // If the sequence number of the message is not the one we are // waiting for, we close the session. else if(DataMsg->SeqNb!=LastSeqNb+1) { puts("Break in sequence number."); ProcessMmtpError( MMTPSendError(Session,MMTPErrIndErr_MsgLost,1,LastSeqNb,Msg)); ProcessMmtpError(MMTPCloseSession(Session)); Quit=TRUE; } // The sequence number of the message is the good one. else { LastSeqNb++; fprintf( OutputFile,"%-*s - %s\n", FIELD_SIZE_MSGID,LastMsgIdReceived,DataMsg->Data); } } /******************************************************************************* * int ProcessMmtpError(int Error) * * Description * Process MMTP return codes. * * Parameters * Error * Return code to process. * * Return code * The return code given in parameter. *******************************************************************************/ int ProcessMmtpError(int Error) { switch(Error) { case MMTP_OK: case MMTP_NO_MESSAGE: break; case MMTP_CNX_LOST: printf("MMTP physical connection lost\n"); break; case MMTP_NOT_CONNECTED: printf("MMTP logical connection not established\n"); break; case MMTP_BUFFER_FULL: printf("Warning : the server socket is full\n"); break; case MMTP_CANNOT_INIT_SOCKET: case MMTP_NOT_ENOUGH_MEMORY: case MMTP_TOO_MUCH_SESSIONS: case MMTP_SOCKET_ERROR: case MMTP_INVALID_ADDRESS: case MMTP_INVALID_ARG: case MMTP_INVALID_ARG1: case MMTP_INVALID_ARG2: case MMTP_INVALID_ARG3: case MMTP_INVALID_ARG4: case MMTP_INVALID_ARG5: case MMTP_INVALID_ARG6: case MMTP_INVALID_ARG7: case MMTP_INVALID_ARG8: case MMTP_INVALID_ARG9: case MMTP_CLIENT_FCT: case MMTP_SERVER_FCT: case MMTP_INVALID_SESSION: case MMTP_NOT_INITIALIZED: case MMTP_SYSTEM_EXCEPTION: printf( "Fatal error in MMTP connection : %d - %s\n", Error,MMTPErrorDescription(Error)); DisconnectServer(MMTPDcnxRsn_Abnormal); Quit=TRUE; break; default: printf( "Error in MMTP connection : %d - %s\n", Error,MMTPErrorDescription(Error)); } return(Error); } #ifdef NT BOOL WINAPI ControlHandler(DWORD Signal) { if(Signal==CTRL_BREAK_EVENT || Signal==CTRL_C_EVENT || Signal==CTRL_CLOSE_EVENT) { Interrupt=TRUE; return(TRUE); } return(FALSE); } #elif defined(UNIX) void ControlHandler(int Signal) { Interrupt=TRUE; } void WaitMs(unsigned long MilliSeconds) { struct timeval t={ MilliSeconds/1000, (MilliSeconds%1000)*1000 }; select(0,NULL,NULL,NULL,&t); } #endif![]()
Partager