forked from snap-stanford/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappsrv.cpp
1336 lines (1256 loc) · 44.5 KB
/
appsrv.cpp
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////
// Application-Client-Server
// generic arguments
TStr TAppCS::A_Result="Result";
TStr TAppCS::A_Msg="Msg";
TStr TAppCS::A_MsgType="MsgType";
TStr TAppCS::A_Continue="Continue";
TStr TAppCS::A_Browse="Browse";
TStr TAppCS::A_SesId="SesId";
TStr TAppCS::A_FetchId="FetchId";
TStr TAppCS::A_Redir="Redir";
TStr TAppCS::A_Agent="Agent";
TStr TAppCS::A_HostNm="HostNm";
TStr TAppCS::A_PortN="PortN";
TStr TAppCS::A_UsrNm="UsrNm";
TStr TAppCS::A_PwdStr="PwdStr";
TStr TAppCS::A_PrivNm="PrivNm";
TStr TAppCS::A_PrivId="PrivId";
TStr TAppCS::A_CltNm="CltNm";
TStr TAppCS::A_CltType="CltType";
TStr TAppCS::A_State="State";
TStr TAppCS::A_PrevState="PrevState";
TStr TAppCS::A_OldState="OldState";
TStr TAppCS::A_NewState="NewState";
TStr TAppCS::A_AbortState="AbortState";
TStr TAppCS::A_SetStateTm="SetStateTm";
TStr TAppCS::A_LogNm="LogNm";
// functions
TStr TAppCS::F_Info="Info";
TStr TAppCS::F_GetStateInfo="GetStateInfo";
TStr TAppCS::F_GetCltList="GetCltList";
TStr TAppCS::F_GetCltInfo="GetCltInfo";
TStr TAppCS::F_GetCltDesc="GetCltDesc";
TStr TAppCS::F_IsAlive="IsAlive";
TStr TAppCS::F_IsConn="IsConn";
TStr TAppCS::F_Log="Log";
TStr TAppCS::F_Connect="Connect";
TStr TAppCS::F_Disconnect="Disconnect";
TStr TAppCS::F_DelayTimer="DelayTimer";
TStr TAppCS::F_RegAgent="RegAgent";
TStr TAppCS::F_Confirm="Confirm";
TStr TAppCS::F_Cancel="Cancel";
TStr TAppCS::F_Notify="Notify";
TStr TAppCS::F_NotifyStateChange="NotifyStateChange";
TStr TAppCS::F_Abort="Abort";
// function hbeat-timer
TStr TAppCS::F_HBeatTimer="HBeatTimer";
TStr TAppCS::TmA_TimerId="TimerId";
TStr TAppCS::TmA_CtxNm="CtxNm";
TStr TAppCS::TmA_Ticks="Ticks";
// function-broadcast
TStr TAppCS::F_Broadcast="Broadcast";
TStr TAppCS::BcA_Cm="Cm";
TStr TAppCS::BcA_FldNm="FldNm";
TStr TAppCS::BcA_FldVal="FldVal";
TStr TAppCS::BcA_Msg="Msg";
// logging
TStr TAppCS::F_NotifyEvent="Log::NotifyEvent";
TStr TAppCS::F_NotifyRawEvent="Log::NotifyRawEvent";
TStr TAppCS::LogA_EventTypeNm="EventTypeNm";
TStr TAppCS::LogA_TestMode="TestMode";
TStr TAppCS::LogA_CreatorNm="CreatorNm";
TStr TAppCS::LogA_CreatorTime="CreatorTime";
// settings
int TAppCS::DTmrA_DelayTimerTout=2*1000;
// states
TStr TAppCS::S_Start="Start";
TStr TAppCS::S_End="End";
TStr TAppCS::S_ConnectRq="ConnectRq";
TStr TAppCS::S_ConnectResp="ConnectResp";
TStr TAppCS::S_Connected="Connected";
TStr TAppCS::S_Server="Server";
//////////////////////////////////////////////////
// Application-Client-Server-Logging
TAppCSLog::TAppCSLog(
const TStr& _LogFNm, const int64& _MxFLen, const PNotify& _Notify):
Notify(_Notify),
LogFNm(_LogFNm), LogSOut(), fLog(NULL), MxFLen(_MxFLen){
if (LogFNm.Empty()){
TNotify::OnNotify(Notify, ntInfo, "Log-File Not Generated.");
} else {
// prepare Log-FileName
if (LogFNm.GetFPath().Empty()){
LogFNm=GetHomeNrFPath()+LogFNm;}
// check existence and create/open
if (false&&TFile::Exists(LogFNm)){
LogSOut=TFOut::New(LogFNm, true); fLog=LogSOut->GetFileId();
TNotify::OnNotify(Notify, ntInfo, TStr("Log-File ")+LogFNm+" opened.");
} else {
LogSOut=TFOut::New(LogFNm); fLog=LogSOut->GetFileId();
TNotify::OnNotify(Notify, ntInfo, TStr("Log-File ")+LogFNm+" created.");
}
// write log-file header
PutLogHd();
}
}
void TAppCSLog::ResetLogIfTooBig(){
if ((fLog!=NULL)&&(MxFLen!=-1)){
int64 FLen=ftell(fLog);
if (FLen>MxFLen){
LogSOut=NULL; // close the file
try {
TStr OldLogFNm=LogFNm+".Old";
TFile::Del(OldLogFNm, false);
TNotify::OnNotify(Notify, ntInfo, TStr("Log-File ")+OldLogFNm+" deleted.");
TFile::Rename(LogFNm, OldLogFNm);
TNotify::OnNotify(Notify, ntInfo, TStr("Log-File ")+LogFNm+" renamed.");
LogSOut=TFOut::New(LogFNm, true);
TNotify::OnNotify(Notify, ntInfo, TStr("Log-File ")+LogFNm+" opened.");
PutLogHd();
} catch (PExcept Except){
TNotify::OnNotify(Notify, ntErr, Except->GetMsgStr());
}
}
}
}
void TAppCSLog::PutLogHd(){
// get current time
TTm CurTm=TTm::GetCurUniTm();
// output log-file line
if (fLog!=NULL){
fprintf(fLog, "#Software: Quintelligence Application-Web-Server 1.0\n");
fprintf(fLog, "#Version: 1.0\n");
fprintf(fLog, "#Date: %s %s\n",
CurTm.GetWebLogDateStr().CStr(), CurTm.GetWebLogTimeStr().CStr());
fprintf(fLog, "#Fields: date time c-ip cs-method cs-uri sc-status\n");
fflush(fLog);
}
ResetLogIfTooBig();
}
void TAppCSLog::PutLogRec(
const PHttpRq& HttpRq, const PHttpResp& HttpResp, const TStr& PeerHostNm){
// get current time
TTm CurTm=TTm::GetCurUniTm();
// output log-file line
if (fLog!=NULL){
fprintf(fLog, "%s %s %s %s %s %s\n",
CurTm.GetWebLogDateStr().CStr(),
CurTm.GetWebLogTimeStr().CStr(),
PeerHostNm.CStr(),
HttpRq->GetMethodNm().CStr(),
HttpRq->GetUrl()->GetUrlStr().CStr(),
TInt::GetStr(HttpResp->GetStatusCd()).CStr());
fflush(fLog);
}
// notification
TStr MsgStr=TStr("Http: ")+
PeerHostNm+" requested "+HttpRq->GetUrl()->GetUrlStr()+
" responding "+THttp::GetReasonPhrase(HttpResp->GetStatusCd())
+" ["+CurTm.GetStr()+"]";
// /*+" ["+CurTm.GetStr()+"]"*/;
TNotify::OnNotify(Notify, ntInfo, MsgStr);
ResetLogIfTooBig();
}
void TAppCSLog::PutLogMsg(const TStr& MsgType, const TStr& MsgStr){
// get current time
TTm CurTm=TTm::GetCurUniTm();
// output log-file line
if (fLog!=NULL){
fprintf(fLog, "#%s: %s\n",
MsgType.CStr(),
MsgStr.CStr());
/*fprintf(fLog, "#%s: %s [%s %s]\n",
MsgType.CStr(),
MsgStr.CStr(),
CurTm.GetWebLogDateStr().CStr(),
CurTm.GetWebLogTimeStr().CStr());*/
fflush(fLog);
}
// notification
TNotify::OnNotify(
Notify, ntInfo, MsgType+": "+MsgStr/*+" ["+CurTm.GetStr()+"]"*/);
ResetLogIfTooBig();
}
void TAppCSLog::PutLogMsg(
const TStr& MsgType, const TStr& MsgStr, const TStr& MsgArg){
PutLogMsg(MsgType, MsgStr+" ("+MsgArg+").");
}
//////////////////////////////////////////////////
// Application-Client-Server-Timer
TAppCSTimer::TAppCSTimer(
const TStr& _TimerNm, const TStr& _CtxNm,
const int& TimerTout, const bool& _RepeatP, TWebNetSrv* _WebNetSrv):
TTTimer(TimerTout), WebNetSrv(_WebNetSrv),
TimerNm(_TimerNm), RepeatP(_RepeatP), CtxNm(_CtxNm){
IAssertR(TXmlLx::IsTagNm(TimerNm),
TStr("Timer-name is not XML-Tag-Name (")+TimerNm+").");
}
void TAppCSTimer::OnTimeOut(){
// stop timer if not repeatable
if (!RepeatP){
StopTimer();}
// prepare soap-request
PSoapRq SoapRq=TSoapRq::New(TimerNm,
TAppCS::TmA_TimerId, TInt::GetStr(GetTimerId()),
TAppCS::TmA_CtxNm, CtxNm,
TAppCS::TmA_Ticks, TInt::GetStr(GetTicks()));
// prepare url
PUrl Url=SoapRq->GetAsUrl("localhost");
// prepare http-request
PHttpRq HttpRq=THttpRq::New(Url);
// trigger timer-event
WebNetSrv->OnHttpRq(-1, HttpRq);
}
//////////////////////////////////////////////////
// Application-Client-Server-State
TAppCSState::TAppCSState(const PAppCSLog& _Log):
StateId(), Log(_Log){
PrepState();
}
void TAppCSState::PrepState(){
// create environment
SoapEnv=TSoapEnv::New();
// set start-state
SetState(TAppCS::S_Start);
}
void TAppCSState::SetState(const TStr& _StateId){
// save previous state
TStr PrevStateId=StateId;
// set new state
StateId=_StateId;
// report message
if (!Log.Empty()){
Log->PutLogMsg("SetState", "From '"+PrevStateId+"' to '"+StateId+"'");
}
}
/////////////////////////////////////////////////
// Application-Server
TAppSrv::TAppSrv(
const int& PortN, const TStr& LogFNm, const int& HBeatTimerTout,
const PNotify& Notify):
TWebNetSrv(PortN, true), // inherited constructor
TWebNetCltV(), // inherited constructor
Log(TAppCSLog::New(LogFNm, -1, Notify)), // logging
SesIdToCltDescH(), // active clients by session-id
CltNmToSesIdH(), // active clients by unique-name
HBeatTimer(), // heart-beat-timer (moved because of usage of 'this')
ActConnBs(TAppCSActConnBs::New()), // active-connections
NmToLogH(),
Proxy(new TWebNetProxy(PortN+11,TStr("http://127.0.0.1:")+TInt::GetStr(PortN))) {
// heart-beat-timer
HBeatTimer=TAppCSTimer::New(TAppCS::F_HBeatTimer,
"Srv", HBeatTimerTout, true, this);
}
TAppSrv::~TAppSrv(){
HBeatTimer->StopTimer();
}
bool TAppSrv::IsLogSoapFuncNm(const TStr& SoapFuncNm){
return
(!SoapFuncNm.IsPrefix(TAppCS::F_Log))&&
(SoapFuncNm!=TAppCS::F_HBeatTimer)&&
(SoapFuncNm!=TAppCS::F_IsAlive);
}
TStr TAppSrv::GetNewSesId(){
// prepare session-id
TStr SesId;
// repeat until session id changes
do {
//SesId=TMd5::GetMd5SigStr(TTm::GetUniTm().GetStr());
SesId=TTm::GetCurUniTm().GetIdStr();
} while (SesIdToCltDescH.IsKey(SesId));
// return session-id
return SesId;
}
bool TAppSrv::IsGetCltDescBySesId(
const TStr& SesId, PAppCltDesc& CltDesc) const {
return SesIdToCltDescH.IsKeyGetDat(SesId, CltDesc);
}
bool TAppSrv::IsGetCltDescBySockId(
const int& SockId, PAppCltDesc& CltDesc) const {
int SesIdToCltDescP=SesIdToCltDescH.FFirstKeyId();
while (SesIdToCltDescH.FNextKeyId(SesIdToCltDescP)){
CltDesc=SesIdToCltDescH[SesIdToCltDescP];
if (CltDesc->SockId==SockId){
return true;}
}
CltDesc=NULL;
return false;
}
bool TAppSrv::IsGetCltDescByCltNm(
const TStr& CltNm, PAppCltDesc& CltDesc) const {
TStr SesId;
if (CltNmToSesIdH.IsKeyGetDat(CltNm, SesId)){
return IsGetCltDescBySesId(SesId, CltDesc);
} else {
return false;
}
}
PAppCltDesc TAppSrv::AddNewCltDesc(
const TStr& HostNm, const int& PortN, const int& SockId,
const TStr& CltNm, const TStr& CltType){
// create new session-id
TStr SesId=GetNewSesId();
// create new client-descriptor
PAppCltDesc CltDesc=TAppCltDesc::New(SesId, HostNm, PortN);
CltDesc->SockId=SockId;
CltDesc->CltNm=CltNm;
CltDesc->CltType=CltType;
// add client-descriptor under session-id
SesIdToCltDescH.AddDat(SesId, CltDesc);
// add client-name
TStr PrevSesId;
if (CltNmToSesIdH.IsKeyGetDat(CltNm, PrevSesId)){
DelIfCltDesc(PrevSesId);}
CltNmToSesIdH.AddDat(CltNm, SesId);
// add client-type
CltDesc->CltType=CltType;
// return client descriptor
return CltDesc;
}
void TAppSrv::DelIfCltDesc(const TStr& SesId){
// delete client from client-name table
PAppCltDesc CltDesc; TStr CltNm;
if (IsGetCltDescBySesId(SesId, CltDesc)){
CltNm=CltDesc->CltNm;
CltNmToSesIdH.DelIfKey(CltDesc->CltNm);
}
// delete client from session-id table
SesIdToCltDescH.DelIfKey(SesId);
// report to log
Log->PutLogMsg("Info",
TStr::Fmt("Delete client '%s' with session '%s'.", CltNm.CStr(), SesId.CStr()));
/*// report event
FetchSoapRq(TSoapRq::New(TAppCS::F_NotifyEvent,
TAppCS::LogA_CreatorTime, TTm::GetCurUniTm().GetWebLogDateTimeStr(),
TAppCS::LogA_EventTypeNm, "CancelSession",
"SesId", SesId,
"CltNm", CltNm));*/
}
void TAppSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq){
// collect important information
TTm CurTm=TTm::GetCurUniTm(); // get current-time
TStr FetchIdStr=HttpRq->GetFldVal(THttp::FetchIdFldNm); // fetch-id
PUrl Url=HttpRq->GetUrl(); // get url
TStr UrlStr=Url->GetUrlStr(); // get url-string
TStr PeerHostNm=GetPeerHostNm(SockId); // get peer-name
PSoapRq SoapRq=TSoapRq::New(HttpRq); // get soap-request
TStr SoapFuncNm=SoapRq->GetFuncNm();
TStr SoapRqMsgStr=UrlStr+" from "+PeerHostNm; // get soap-request message
// report to log
if (IsLogSoapFuncNm(SoapFuncNm)){
Log->PutLogMsg("SoapRequest", SoapRqMsgStr);}
// process soap-request and generate http-response
PHttpResp HttpResp;
if (SoapRq->IsOk()){
if (SoapFuncNm==TAppCS::F_Info){
HttpResp=OnHttpRq_Info();
} else
if (SoapFuncNm==TAppCS::F_GetCltList){
HttpResp=OnHttpRq_GetCltList(SoapRq);
} else
if ((SoapFuncNm==TAppCS::F_GetCltInfo)||(SoapFuncNm==TAppCS::F_GetCltDesc)){
HttpResp=OnHttpRq_GetCltInfo(SoapRq);
} else
if (SoapFuncNm==TAppCS::F_Connect){
HttpResp=OnHttpRq_Connect(SoapRq, PeerHostNm, SockId);
} else
if (SoapFuncNm==TAppCS::F_Disconnect){
HttpResp=OnHttpRq_Disconnect(SoapRq, PeerHostNm);
} else
if (SoapFuncNm==TAppCS::F_Log){
HttpResp=OnHttpRq_OnLog(SoapRq);
} else
if (SoapFuncNm==TAppCS::F_HBeatTimer){
OnHttpRq_OnHBeatTimer();
} else
if (IsCltOk(SoapRq, PeerHostNm)){
if (SoapFuncNm==TAppCS::F_IsAlive){
HttpResp=TSoapResp::GetHttpResp(TAppCS::F_IsAlive);
} else
if (SoapFuncNm==TAppCS::F_Broadcast){
HttpResp=OnHttpRq_Broadcast(SoapRq);
} else {
HttpResp=OnFuncRq(SockId, SoapRq, Url);
}
} else {
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Client admin info mismatch.");
}
} else {
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::DataEncodingUnknownCodeNm,
"Invalid SOAP HTTP Request.");
}
// send response and/or log
if (SockId!=-1){
if (HttpResp.Empty()){
Log->PutLogMsg("Warning", TStr("Empty Http-Response for ")+SoapRqMsgStr);
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm, "Empty Response.");
}
// fetch-id
if (!FetchIdStr.Empty()){
HttpResp->AddFldVal(THttp::FetchIdFldNm, FetchIdStr);}
// send response
SendHttpResp(SockId, HttpResp);
// write log
if (IsLogSoapFuncNm(SoapFuncNm)){
// write log record
Log->PutLogRec(HttpRq, HttpResp, PeerHostNm);
// write soap-response
PSoapResp SoapResp=TSoapResp::New(HttpResp);
if (SoapResp->IsOk()){
Log->PutLogMsg("SoapResponse", SoapResp->GetAsUrlPathStr());
}
}
}
}
void TAppSrv::OnHttpRqError(const int& SockId, const TStr& MsgStr){
Log->PutLogMsg("HttpRqError", MsgStr);
PAppCltDesc CltDesc;
if (IsGetCltDescBySockId(SockId, CltDesc)){
DelIfCltDesc(CltDesc->SesId);
}
}
TStr TAppSrv::GetPeerHostNm(const int& SockId) const {
if (SockId==-1){
return "internal";
} else {
TStr PeerHostNm=GetCltSock(SockId)->GetPeerIpNum();
#if 0
// doesn't work on hosts with multiple adapters, fixed by ResolveCltIP
if ((PeerHostNm.GetLc()=="localhost")||(PeerHostNm=="127.0.0.1")){
PSockHost LocalSockHost=TSockHost::GetLocalSockHost();
if (LocalSockHost->IsOk()){
return LocalSockHost->GetIpNum();
}
}
#else
if (PeerHostNm.GetLc()=="localhost") PeerHostNm="127.0.0.1";
#endif
return PeerHostNm;
}
}
void TAppSrv::ResolveCltIP(const PAppCltDesc CltDesc, int SockId, TStr &DestHost, TStr &DestProxy) {
static const TStr LoopbackIP("127.0.0.1");
static const TStr LocalhostNm("localhost");
if (SockId==-1) {
DestHost=LoopbackIP;
} else {
PSockHost Localhost=TSockHost::GetLocalSockHost();
PSock Sock=GetCltSock(SockId);
TStr PeerIP=Sock->GetPeerIpNum();
TStr AdapterIP=Sock->GetLocalIpNum();
TStr CltHostNm = CltDesc->HostNm;
bool LocalPeerP = (Localhost->HasIpNum(PeerIP)||(PeerIP==LoopbackIP)||(PeerIP==LocalhostNm));
bool LocalRedirP = (Localhost->HasIpNum(CltHostNm)||(CltHostNm==LoopbackIP)||(CltHostNm==LocalhostNm));
if (LocalRedirP) { // redirect to local computer
DestHost=LocalPeerP?LoopbackIP:AdapterIP;
IAssert(LocalPeerP||(AdapterIP!=LoopbackIP));
} else { // redirect to remote host
DestHost=CltHostNm;
if (!LocalPeerP) {
unsigned long PeerAddr = ntohl(inet_addr(PeerIP.CStr()));
unsigned long DestAddr = ntohl(inet_addr(DestHost.CStr()));
if (((PeerAddr^DestAddr) & 0xFFFFFF00) != 0) {
DestProxy=AdapterIP;
}
//test - use proxy for all remote machines regardless of network routing
//DestProxy=AdapterIP;
}
}
}
}
bool TAppSrv::IsCltOk(const PSoapRq& SoapRq, const TStr& PeerHostNm) const {
TStr SesId=SoapRq->GetFldVal(TAppCS::A_SesId);
PAppCltDesc CltDesc;
if (IsGetCltDescBySesId(SesId, CltDesc)){
#if 0
// following code doesn't work as some peers might have multiple IP adapters
// and use round robin algorithms on them. There are also problems with
// proxied requests.
if (PeerHostNm==CltDesc->HostNm) {
return true;
}
#else
return true;
#endif
}
return false;
}
PHttpResp TAppSrv::OnHttpRq_Info() const {
TTm CurTm=TTm::GetCurUniTm();
TChA HtmlChA;
HtmlChA+="<html><head><title>Application Server Status</title></head>";
HtmlChA+="<body>";
HtmlChA+="<h1>Application Server Status</h1>";
HtmlChA+="GMT-Time: ";
HtmlChA+=CurTm.GetWebLogDateStr()+" "+CurTm.GetWebLogTimeStr();
HtmlChA+="<br>";
HtmlChA+="Active Clients: "; HtmlChA+=TInt::GetStr(GetClts());
HtmlChA+="<br>";
HtmlChA+="Server Port: ";
HtmlChA+=TInt::GetStr(GetPortN());
HtmlChA+="<br>";
HtmlChA+="Server Up-Time (secs): ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetSecs());
HtmlChA+="<br>";
HtmlChA+="Log-File-Name: ";
HtmlChA+=GetLog()->GetLogFNm();
HtmlChA+="<br>";
HtmlChA+="Heart-Beat-TimeOut (msecs): ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetTimeOut());
HtmlChA+="<br>";
HtmlChA+="Heart-Beat-Ticks: ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetTicks());
HtmlChA+="<br>";
HtmlChA+="</body>";
HtmlChA+="</html>";
PHttpResp HttpResp=THttpResp::New(
THttp::OkStatusCd, THttp::TextHtmlFldVal, false, TStrIn::New(HtmlChA));
return HttpResp;
}
PHttpResp TAppSrv::OnHttpRq_GetCltList(const PSoapRq& SoapRq){
// create soap-response
PSoapResp SoapResp=TSoapResp::New(SoapRq->GetFuncNm());
TAppCltDescV CltDescV; GetCltDescV(CltDescV);
for (int CltDescN=0; CltDescN<CltDescV.Len(); CltDescN++){
PAppCltDesc CltDesc=CltDescV[CltDescN];
if (!CltDesc->CltNm.Empty()){
SoapResp->AddFldNmVal(CltDesc->CltNm,
CltDesc->HostNm+"/"+TInt::GetStr(CltDesc->PortN));
}
}
// return http-reposnse
return SoapResp->GetHttpResp();
}
PHttpResp TAppSrv::OnHttpRq_GetCltInfo(const PSoapRq& SoapRq){
PHttpResp HttpResp;
// prepare client-name
TStr CltNm=SoapRq->GetFldVal(TAppCS::A_CltNm);
// search for client-descriptor
PAppCltDesc CltDesc;
if (IsGetCltDescByCltNm(CltNm, CltDesc)){
// create http response with host name and port
HttpResp=TSoapResp::GetHttpResp(SoapRq->GetFuncNm(),
TAppCS::A_Result, "T",
TAppCS::A_HostNm, CltDesc->HostNm,
TAppCS::A_PortN, TInt::GetStr(CltDesc->PortN),
TAppCS::A_CltNm, CltDesc->CltNm,
TAppCS::A_CltType, CltDesc->CltType);
} else {
// create http-response with message
HttpResp=TSoapResp::GetHttpResp(SoapRq->GetFuncNm(),
TAppCS::A_Result, "F",
TAppCS::A_Msg, TStr::Fmt("Client '%s' not registered.", CltNm.CStr()));
}
// return http-reponse
return HttpResp;
}
PHttpResp TAppSrv::OnHttpRq_Connect(
const PSoapRq& SoapRq, const TStr& PeerHostNm, const int& SockId){
IAssert(SoapRq->GetFuncNm()==TAppCS::F_Connect);
// get client-port
int CltPortN=SoapRq->GetFldVal(TAppCS::A_PortN).GetInt(-1);
if (CltPortN==-1){
return TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Port parameter missing in Login function call.");
} else {
// collect client-info
TStr CltNm=SoapRq->GetFldVal(TAppCS::A_CltNm);
TStr CltType=SoapRq->GetFldVal(TAppCS::A_CltType);
if (!IsCltByCltNm(CltNm)){
// create client descriptor
PAppCltDesc CltDesc=AddNewCltDesc(PeerHostNm, CltPortN, SockId, CltNm, CltType);
// create & return http-response
return TSoapResp::GetHttpResp(SoapRq->GetFuncNm(),
TAppCS::A_SesId, CltDesc->SesId);
} else {
// create & return http-response
return TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
TStr::Fmt("Client '%s' already registered.", CltNm.CStr()));
}
}
}
PHttpResp TAppSrv::OnHttpRq_Disconnect(
const PSoapRq& SoapRq, const TStr& PeerHostNm){
IAssert(SoapRq->GetFuncNm()==TAppCS::F_Disconnect);
// collect client-info
TStr SesId=SoapRq->GetFldVal(TAppCS::A_SesId); PAppCltDesc CltDesc;
if (!SesId.Empty()&&IsGetCltDescBySesId(SesId, CltDesc)){
DelIfCltDesc(CltDesc->SesId);
return TSoapResp::GetHttpResp(SoapRq->GetFuncNm());
} else {
// create & return http-response
return TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Invalid disconnect request.");
}
}
PHttpResp TAppSrv::OnHttpRq_OnLog(const PSoapRq& SoapRq){
IAssert(SoapRq->GetFuncNm()==TAppCS::F_Log);
// prepare log
PAppCSLog CltLog;
TStr LogNm=SoapRq->GetFldVal(TAppCS::A_LogNm);
if (!NmToLogH.IsKeyGetDat(LogNm, CltLog)){
CltLog=TAppCSLog::New(LogNm+".Log", -1, NULL);
NmToLogH.AddDat(LogNm, CltLog);
}
// save message
TStr MsgTypeNm=SoapRq->GetFldVal(TAppCS::A_MsgType);
TStr MsgStr=SoapRq->GetFldVal(TAppCS::A_Msg);
CltLog->PutLogMsg(MsgTypeNm, MsgStr);
// return ok
return TSoapResp::GetHttpResp(SoapRq->GetFuncNm());
}
void TAppSrv::OnHttpRq_OnHBeatTimer(){
// send is-alive requests to clients
TTm CurTm=TTm::GetCurUniTm();
TAppCltDescV CltDescV; GetCltDescV(CltDescV);
for (int CltDescN=0; CltDescN<CltDescV.Len(); CltDescN++){
PAppCltDesc CltDesc=CltDescV[CltDescN];
TTm LastHBeatTm=CltDesc->LastHBeatTm;
if (TTm::GetDiffMSecs(CurTm, LastHBeatTm)>HBeatTimer->GetTimeOut()){
if (CltDesc->ActiveHBeatRqP){
DelIfCltDesc(CltDesc->SesId);
} else {
CltDesc->ActiveHBeatRqP=true;
FetchSoapRq(CltDesc, TSoapRq::New(TAppCS::F_IsAlive));
}
}
}
}
PHttpResp TAppSrv::OnHttpRq_Broadcast(const PSoapRq& SoapRq){
IAssert(SoapRq->GetFuncNm()=="Broadcast");
TStr BcCmNm=SoapRq->GetFldVal("Cm");
TStr BcFldNm=SoapRq->GetFldVal("FldNm");
TStr BcFldVal=SoapRq->GetFldVal("FldVal");
TStr BcMsgStr=SoapRq->GetFldVal("Msg");
TAppCltDescV CltDescV; GetCltDescV(CltDescV);
for (int CltDescN=0; CltDescN<CltDescV.Len(); CltDescN++){
PAppCltDesc CltDesc=CltDescV[CltDescN];
FetchSoapRq(CltDesc, TSoapRq::New("Broadcast",
"Cm", BcCmNm, "FldNm", BcFldNm, "FldVal", BcFldVal, "Msg", BcMsgStr));
}
// return ok
return TSoapResp::GetHttpResp(SoapRq->GetFuncNm());
}
PHttpResp TAppSrv::OnFuncRq(const int &SockId, const PSoapRq& SoapRq, const PUrl& Url){
// get function-name
TStr FuncNm=SoapRq->GetFuncNm();
// two ways of detecting client: (1) client-parameter (2) FuncNm=CltNm::FuncNm
TStr CltNm;
// check for 'client' parameter in soap-request
if (SoapRq->IsFldNm(TAppCS::A_CltNm)){
CltNm=SoapRq->GetFldVal(TAppCS::A_CltNm);
}
// check for client-name in function-name
if (CltNm.Empty()){
// split function-name to client-name and client-function-name
TStr _CltFuncNm; FuncNm.SplitOnStr(CltNm, "::", _CltFuncNm);
}
// call function
PHttpResp HttpResp;
if (!CltNm.Empty()){
// get client
PAppCltDesc CltDesc;
if (IsGetCltDescByCltNm(CltNm, CltDesc)){
// add soap-request-creator client-info
{TStr SesId=SoapRq->GetFldVal(TAppCS::A_SesId);
PAppCltDesc SoapRqCltDesc;
if (IsGetCltDescBySesId(SesId, SoapRqCltDesc)){
SoapRq->AddFldNmVal(TAppCS::LogA_CreatorNm, SoapRqCltDesc->CltNm);
}}
// prepare redirection-url
SoapRq->AddFldNmVal(TAppCS::A_Redir, "T");
#if 0
TStr RedirUrlStr=TStr()+
"http://"+CltDesc->HostNm+":"+TInt::GetStr(CltDesc->PortN)+"/"+
SoapRq->GetAsUrlPathStr();
// prepare redirection-http-response
HttpResp=THttpResp::New(300, "", false, NULL, RedirUrlStr);
#else
TStr DestHost, DestProxy, RedirHost;
ResolveCltIP(CltDesc,SockId,DestHost,DestProxy);
DestHost += ":"+TInt::GetStr(CltDesc->PortN);
if (!DestProxy.Empty()) {
DestProxy += ":"+TInt::GetStr(Proxy->GetPortN());
RedirHost=DestProxy;
} else {
RedirHost=DestHost;
}
// prepare redirection-http-response
TStr RedirUrlStr=TStr()+"http://"+RedirHost+"/"+SoapRq->GetAsUrlPathStr();
Log->PutLogMsg("Redirect", TStr()+GetPeerHostNm(SockId)+" -> "+RedirUrlStr);
HttpResp=THttpResp::New(300, "", false, NULL, RedirUrlStr);
if (!DestProxy.Empty()) HttpResp->AddFldVal(THttp::HostFldNm,DestHost);
#endif
} else {
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
TStr()+"Client for function call not active: "+CltNm, FuncNm);
}
} else {
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Unknown SOAP function call", FuncNm);
}
return HttpResp;
}
int TAppSrv::FetchSoapRq(const PAppCltDesc& CltDesc, const PSoapRq& SoapRq){
// add session-id for current client
SoapRq->AddFldNmVal(TAppCS::A_SesId, CltDesc->SesId);
// create url
PUrl SoapRqUrl=SoapRq->GetAsUrl(CltDesc->HostNm, CltDesc->PortN);
// fetch url
int FetchId=FetchUrl(SoapRqUrl);
// add active-connection
ActConnBs->AddActConn(FetchId,
TAppCSActConn::New(SoapRq, CltDesc->HostNm, CltDesc->PortN,1));
// fetch url
Log->PutLogMsg("FetchSoapRq",
TInt::GetStr(FetchId, "[FetchId:%d]")+SoapRq->GetAsUrlPathStr());
// return fetch-id
return FetchId;
}
void TAppSrv::OnHttpResp(const int& FetchId, const PHttpResp& HttpResp){
// get soap-response
PSoapResp SoapResp=TSoapResp::New(HttpResp);
// analyse soap-response
if (SoapResp->IsOk()){
if (SoapResp->IsResult()){
// get soap-response function-name and result-value
TStr SoapRespFuncNm=SoapResp->GetFuncNm();
TStr SoapRespResultVal=SoapResp->GetFldVal(TAppCS::A_Result);
// get matching soap-request and its function-name
PSoapRq SoapRq=ActConnBs->GetActConn(FetchId)->GetSoapRq();
TStr SoapRqFuncNm=SoapRq->GetFuncNm();
IAssert(SoapRqFuncNm==SoapRespFuncNm);
// delete matching soap-request
ActConnBs->DelActConn(FetchId);
// process known functions
if (SoapRespFuncNm==TAppCS::F_IsAlive){
TStr SoapRqSesId=SoapRq->GetFldVal(TAppCS::A_SesId); // retrieve session-id
TStr SoapRespSesId=SoapResp->GetFldVal(TAppCS::A_SesId); // retrieve session-id
if (SoapRqSesId==SoapRespSesId){
PAppCltDesc CltDesc;
if (IsGetCltDescBySesId(SoapRespSesId, CltDesc)){ // get client-descriptor
CltDesc->LastHBeatTm=TTm::GetCurUniTm(); // update touch-time
CltDesc->ActiveHBeatRqP=false; // unactivate touch wait-flag
}
} else {
Log->PutLogMsg("Warning",
"Session numbers (request & response) for IsAlive don't match.");
}
} else {
// soap-response not handeled
Log->PutLogMsg("Warning",
"Unhandled Soap-Response", SoapResp->GetAsUrlPathStr());
}
} else {
IAssert(SoapResp->IsFault());
// fail-soap-response - pass to on-error handler
OnHttpRespError(FetchId, TStr("SoapFault: ")+SoapResp->GetFaultReasonStr());
}
} else {
// invalid soap-response - pass to on-error handler
PSoapResp SoapResp=TSoapResp::New(HttpResp);
OnHttpRespError(FetchId, "Invalid SOAP Response");
}
}
void TAppSrv::OnHttpRespError(const int& FetchId, const TStr& MsgStr){
// get matching soap-request
PSoapRq SoapRq=ActConnBs->GetActConn(FetchId)->GetSoapRq();
// report error
Log->PutLogMsg("Error", MsgStr, SoapRq->GetAsUrlPathStr());
// delete matching soap-request
ActConnBs->DelActConn(FetchId);
// retrieve & delete session-id
TStr SesId=SoapRq->GetFldVal(TAppCS::A_SesId);
DelIfCltDesc(SesId);
}
const int TAppSrv::DfPortN=8888;
/////////////////////////////////////////////////
// Application-Client
TAppClt::TAppClt(
const TStr& LogFNm,
const TStr& _CltNm,
const TStr& _CltType,
const int& HBeatTimerTout,
const bool& _AgentP,
const TStr& _SrvHostNm,
const int& _SrvPortN,
const int& CltPortN,
const PNotify& Notify):
TWebNetSrv(CltPortN, false), // inherited constructor
TWebNetCltV(), // inherited constructor
Log(TAppCSLog::New(LogFNm, -1, Notify)), // logging
CltNm(_CltNm), // unique client identification
CltType(_CltType), // client type
SrvHostNm(_SrvHostNm), SrvPortN(_SrvPortN), // server info
AgentP(_AgentP), // agent client
AgentHostNmPortNPrV(), // list of agents connected to client
HBeatTimer(), // hear-beat-timer (moved because of usage of 'this')
LastHBeatTm(TTm::GetCurUniTm()), // last hbeat time
ActiveIsAliveFId(-1), // active hbeat fetch-id
ActConnBs(TAppCSActConnBs::New()), // active-connections
SesId(), // session-id
State(TAppCSState::New(Log)), // state
DelayTimer(), // waiting for next connect trial
FuncFetchIdH(){ // fetch-ids
// heart-beat-timer
HBeatTimer=TAppCSTimer::New(TAppCS::F_HBeatTimer,
CltNm, HBeatTimerTout, true, this);
// delay-timer
DelayTimer=TAppCSTimer::New(TAppCS::F_DelayTimer, CltNm, 0, false, this);
}
TAppClt::~TAppClt(){
HBeatTimer->StopTimer();
}
const int TAppClt::DfPortN=TAppSrv::DfPortN+1;
void TAppClt::OnHttpRq(const int& SockId, const PHttpRq& HttpRq){
// collect important information
TTm CurTm=TTm::GetCurUniTm(); // get current-time
TStr FetchIdStr=HttpRq->GetFldVal(THttp::FetchIdFldNm);
PUrl Url=HttpRq->GetUrl(); // get url
TStr UrlStr=Url->GetUrlStr(); // get url-string
TStr PeerHostNm=GetPeerHostNm(SockId); // get peer-name
PSoapRq SoapRq=TSoapRq::New(HttpRq); // get soap-request
TStr SoapFuncNm=SoapRq->GetFuncNm();
TStr SoapRqMsgStr=UrlStr+" from "+PeerHostNm; // get soap-request message
// report to log
if (TAppSrv::IsLogSoapFuncNm(SoapFuncNm)){
PutLogMsg("SoapRequest", SoapRqMsgStr, true);}
// process soap-request and generate http-response
PHttpResp HttpResp;
if (SoapRq->IsOk()){
if (SoapFuncNm==TAppCS::F_Info){
HttpResp=OnHttpRq_Info();
} else
if (SoapFuncNm==TAppCS::F_GetStateInfo){
HttpResp=OnHttpRq_GetStateInfo(SoapRq);
} else
if (SoapFuncNm==TAppCS::F_HBeatTimer){
OnHttpRq_OnHBeatTimer();
} else
if ((SoapFuncNm==TAppCS::F_IsAlive)||(SoapFuncNm==TAppCS::F_IsConn)){
HttpResp=TSoapResp::GetHttpResp(SoapFuncNm, TAppCS::A_SesId, SesId);
} else
if (SoapFuncNm==TAppCS::F_RegAgent){
HttpResp=OnHttpRq_RegAgent(SoapRq, PeerHostNm);
} else {
bool RedirP=SoapRq->GetFldVal(TAppCS::A_Redir)=="T";
bool AgentP=SoapRq->GetFldVal(TAppCS::A_Agent)=="T";
if ((SockId!=-1)&&(!RedirP)&&(!AgentP)&&(!IsSesId())){
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Session not defined.");
} else
if ((SockId!=-1)&&(!RedirP)&&(!AgentP)&&
(GetSesId()!=SoapRq->GetFldVal(TAppCS::A_SesId))){
TStr SesId1=GetSesId();
TStr SesId2=SoapRq->GetFldVal(TAppCS::A_SesId);
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm,
"Session-ids don't match.");
} else {
HttpResp=OnFuncRq(SoapRq);
}
}
} else {
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::DataEncodingUnknownCodeNm,
"Invalid SOAP HTTP Request.");
}
// send response and/or log
if (SockId!=-1){
if (HttpResp.Empty()){
PutLogMsg("Warning", TStr("Empty Http-Response for ")+SoapRqMsgStr, true);
HttpResp=TSoapResp::GetFaultHttpResp(TSoap::SenderCodeNm, "Empty Response.");
}
// fetch-id
if (!FetchIdStr.Empty()){
HttpResp->AddFldVal(THttp::FetchIdFldNm, FetchIdStr);}
// send response
SendHttpResp(SockId, HttpResp);
// write log
if (TAppSrv::IsLogSoapFuncNm(SoapFuncNm)){
// write log record
Log->PutLogRec(HttpRq, HttpResp, PeerHostNm);
// write soap-response
PSoapResp SoapResp=TSoapResp::New(HttpResp);
if (SoapResp->IsOk()){
PutLogMsg("SoapResponse", SoapResp->GetAsUrlPathStr(), true);
}
}
}
}
void TAppClt::OnHttpRqError(const int& SockId, const TStr& MsgStr){
}
TStr TAppClt::GetPeerHostNm(const int& SockId) const {
if (SockId==-1){
return "internal";
} else {
TStr PeerHostNm=GetCltSock(SockId)->GetPeerIpNum();
if ((PeerHostNm.GetLc()=="localhost")||(PeerHostNm=="127.0.0.1")){
PSockHost LocalSockHost=TSockHost::GetLocalSockHost();
if (LocalSockHost->IsOk()){
return LocalSockHost->GetIpNum();
} else {
return PeerHostNm;
}
} else {
return PeerHostNm;
}
}
}
PHttpResp TAppClt::OnHttpRq_Info() const {
TTm CurTm=TTm::GetCurUniTm();
TChA HtmlChA;
HtmlChA+="<html><head><title>Application Client Status</title></head>";
HtmlChA+="<body>";
HtmlChA+="<h1>Application Client Status</h1>";
HtmlChA+="GMT-Time: ";
HtmlChA+=CurTm.GetWebLogDateStr()+" "+CurTm.GetWebLogTimeStr();
HtmlChA+="<br>";
HtmlChA+="Client Port: ";
HtmlChA+=TInt::GetStr(GetPortN());
HtmlChA+="<br>";
HtmlChA+="Client Up-Time (secs): ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetSecs());
HtmlChA+="<br>";
HtmlChA+="Server-Host-Name: ";
HtmlChA+=GetSrvHostNm();
HtmlChA+="<br>";
HtmlChA+="Server-Host-Name: ";
HtmlChA+=TInt::GetStr(SrvPortN);
HtmlChA+="<br>";
HtmlChA+="Log-File-Name: ";
HtmlChA+=GetLog()->GetLogFNm();
HtmlChA+="<br>";
HtmlChA+="Heart-Beat-TimeOut (msecs): ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetTimeOut());
HtmlChA+="<br>";
HtmlChA+="Heart-Beat-Ticks: ";
HtmlChA+=TInt::GetStr(GetHBeatTimer()->GetTicks());
HtmlChA+="<br>";
HtmlChA+="</body>";
HtmlChA+="</html>";
PHttpResp HttpResp=THttpResp::New(
THttp::OkStatusCd, THttp::TextHtmlFldVal, false, TStrIn::New(HtmlChA));
return HttpResp;
}
PHttpResp TAppClt::OnHttpRq_GetStateInfo(const PSoapRq& SoapRq) const {
// prepare result
PSoapResp SoapResp=TSoapResp::New(SoapRq->GetFuncNm());
// add state-info
SoapResp->AddFldNmVal("StateId", State->GetStateId());