00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 #ifndef _MAIN_H
00147 #define _MAIN_H
00148
00149 #pragma interface
00150
00151 #include <ptlib.h>
00152 #include <ptlib/sockets.h>
00153 #include <ptlib/safecoll.h>
00154
00155
00156
00157 class TcpConnectionList;
00158 class TcpConnection;
00159 class Manager;
00160
00162
00164 class TcpConnection : public PSafeObject
00165 {
00166 PCLASSINFO(TcpConnection, PSafeObject);
00167 public:
00169 TcpConnection(Manager & _mgr);
00170
00172 ~TcpConnection();
00173
00175 void StartRunning();
00176
00178 BOOL IsRunning();
00179
00181 void PrintOn(ostream & strm) const;
00182
00184 void Terminate();
00185
00189 void AssignSourceId(PString & newId) { sourceId = newId; }
00190
00192 PString GetSourceId() { return sourceId; }
00193
00195 void Clear();
00196
00198 void StartWorkNow(PTCPSocket &listener);
00199
00202 void StartWorkNowNotPossible();
00203
00204 protected:
00205
00207 PString GetNow();
00208
00213 void DoOneMessage();
00214
00218 void BreakUpMessageBlock(PBYTEArray & src, PINDEX srcLen, PStringArray & result);
00219
00222 void ProcessOneTextMessage(const PString & txt);
00223
00225 void SendThisMessage(PBYTEArray & msg);
00226
00228 Manager & manager;
00229
00230 #ifdef DOC_PLUS_PLUS
00231
00234 virtual void OnReleaseThreadMain(PThread &, INT);
00235 #else
00236 PDECLARE_NOTIFIER(PThread, TcpConnection, OnReleaseThreadMain);
00237 #endif
00238
00245 virtual void OnReleased();
00246
00248 PMutex writeMessageLock;
00249
00251 PMutex incomingLock;
00252
00255 PThread *incomingMessages;
00256
00257 #ifdef DOC_PLUS_PLUS
00258
00259 virtual void IncomingMessagesMain(PThread &, INT);
00260 #else
00261 PDECLARE_NOTIFIER(PThread, TcpConnection, IncomingMessagesMain);
00262 #endif
00263
00265 PSyncPoint socketInitialised;
00266
00269 PTCPSocket messagesSocket;
00270
00272 PString sourceId;
00273
00275 enum {
00276 readArraySize = 10
00277 };
00278
00280 PINDEX readArray[readArraySize];
00281
00283 PINDEX readArrayIndex;
00284
00286 PMutex clearMutex;
00287
00289 BOOL isClearing;
00290
00292 BOOL reportReadMessages;
00293
00298 BOOL reportSentMessages;
00299
00303 PBYTEArray message;
00304
00307 PString thisThreadName;
00308
00310 PString clearName;
00311
00315 PString globalUniqueId;
00316 };
00317
00319
00320 PDECLARE_LIST (TcpConnectionList, TcpConnection *)
00321 #ifdef DOC_PLUS_PLUS
00329 class TcpConnectionList : public TcpConnection *
00330 {
00331 #endif
00332
00334 ~TcpConnectionList();
00335
00337 void CloseDown();
00338
00340 void Initialise();
00341
00342 #ifdef DOC_PLUS_PLUS
00343
00345 void FillListWithTcpConnections(PThread &, INT param);
00346 #else
00347 PDECLARE_NOTIFIER(PThread, TcpConnectionList, FillListWithTcpConnections);
00348 #endif
00349
00352 TcpConnection * GetNextTcpConnection();
00353
00355 PINDEX GetListSize() { PWaitAndSignal m(accessMutex); return PAbstractList::GetSize(); }
00356
00357 protected:
00359 TcpConnection *MakeOneTcpConnection();
00360
00362 PSyncPoint checkListContents;
00363
00365 PMutex accessMutex;
00366
00368 PThread *createNewTcpConnections;
00369
00371 PMutex mutexNewTcpConnections;
00372
00374 BOOL endNow;
00375 };
00376
00378
00381 class Manager : public PObject
00382 {
00383 PCLASSINFO(Manager, PObject);
00384 public:
00386 Manager();
00387
00389 ~Manager();
00390
00394 PSafePtr<TcpConnection> GetConnectionWithLock(
00395 const PString & token
00396 ,PSafetyMode mode = PSafeReadWrite
00397 ) { return connectionsActive.FindWithLock(token, mode); }
00398
00400 void RemoveNode(const PString & token);
00401
00404 void WaitForIncoming();
00405
00408 PString ProcessMessagesName();
00409
00412 void AppendRunning(PSafePtr<TcpConnection> conn, const PString & id);
00413
00418 void AppendDead(PSafePtr<TcpConnection> conn);
00419
00421 void QuitNow();
00422
00424 void StopIncomingConnections();
00425
00428 void GetListConnectedNodes(PStringArray & nodes);
00429
00431 BOOL KeepRunning() { return keepRunning; }
00432
00435 void OnReleased(TcpConnection & connection);
00436
00438 void WaitForEmpty();
00439
00441 void GetStatus(PStringStream &status);
00442
00446 PString GetNextBogusId();
00447 protected:
00448
00450 class ConnectionDict : public PSafeDictionary<PString, TcpConnection>
00451 {
00456 virtual void DeleteObject(PObject * object) const;
00457 } connectionsActive;
00458
00459 #ifdef DOC_PLUS_PLUS
00460
00462 virtual void DoQuitNow(PThread &, INT);
00463 #else
00464 PDECLARE_NOTIFIER(PThread, Manager, DoQuitNow);
00465 #endif
00466
00468 PTCPSocket listener;
00469
00471 PAtomicInteger bogusCount;
00472
00474 PAtomicInteger connectionCount;
00475
00477 TcpConnectionList availableTcpConnections;
00478
00480 BOOL keepRunning;
00481
00483 BOOL isQuitting;
00484
00486 PMutex quitNowMutex;
00487 };
00488
00490
00493 class ServerProcess : public PProcess
00494 {
00495 PCLASSINFO(ServerProcess, PProcess);
00496
00497 public:
00499 ServerProcess();
00500
00502 ~ServerProcess();
00503
00505 void Main();
00506
00509 static ServerProcess & Current()
00510 { return (ServerProcess &)PProcess::Current(); }
00511
00513 BOOL ReportReadMessages() { return reportReadMessages; }
00514
00516 BOOL ReportSentMessages() { return reportSentMessages; }
00517
00519 Manager & GetManager() { return localManager; }
00520
00521 protected:
00522
00524 Manager localManager;
00525
00527 BOOL reportReadMessages;
00528
00533 BOOL reportSentMessages;
00534 };
00536
00537 #endif // _MAIN_H