Author: derek Date: Thu Aug 10 00:34:04 2006 New Revision: 62 Modified: sofaswitch/trunk/server/main.cxx sofaswitch/trunk/server/main.h Log: Split up a long long method into two "longish" methods. Makes it much more readable as to what is going on. Modified: sofaswitch/trunk/server/main.cxx ============================================================================== --- sofaswitch/trunk/server/main.cxx (original) +++ sofaswitch/trunk/server/main.cxx Thu Aug 10 00:34:04 2006 @@ -703,12 +703,51 @@ cerr << msg; } + if (ProcessedAsServerCommand(txt)) + return; + PTRACE(1, "READ \"" << txt << "\" of size " << txt.GetSize()); + PINDEX l = txt.Find(':'); + if (l == P_MAX_INDEX) { + PTRACE(3, "Could not find a colon in \"" << txt << "\", of size " << txt.GetLength()); + return; + } + PString dest = txt.Left(l).Trim(); + PString msg = txt.Mid(l + 1).Trim(); + + PTRACE(3, "Read in " << dest << "--:--" << msg); + // PTRACE(0, ::setw(32) << sourceId << " " <<txt ); + PBYTEArray outMessage; + outMessage.SetSize(sourceId.GetLength() + 1 + msg.GetLength() + 1); + memcpy(outMessage.GetPointer(), sourceId.GetPointer(), sourceId.GetLength()); + outMessage[sourceId.GetLength()] = ':'; + memcpy(outMessage.GetPointer() + sourceId.GetLength() + 1, msg.GetPointer(), msg.GetLength()); + outMessage[outMessage.GetSize() - 1] = 0; + // cerr << " Send " << PTime().AsString("hh:mm.ss.uuu") << " " + // << sourceId << ":" << msg << endl << endl; + PSafePtr <TcpConnection> conn = manager.GetConnectionWithLock(dest, PSafeReadOnly); + if (conn != NULL) { + PTRACE(3, "Found connection for id of " << dest); + conn->SendThisMessage(outMessage); + } else { + PTRACE(1, "Fail to send the message " + << PString((const char *)outMessage.GetPointer(), outMessage.GetSize()) + << " to " << dest); + PTRACE(1, " as could not get a lock"); + PTRACE(1, sourceId << " is sorry"); + IncCountDropped(); + } + + return; +} + +BOOL TcpConnection::ProcessedAsServerCommand(const PString & txt) +{ if (txt *= PString("quitnow")) { PTRACE(3, "QUIT now as have read in the command \"" << txt << "\""); messagesSocket.Close(); manager.QuitNow(); - return; + return TRUE; } if (txt.Left(6) *= PString("WhoAmI")){ @@ -717,13 +756,13 @@ memcpy(answer.GetPointer(), sourceId.GetPointer(), sourceId.GetLength()); answer[sourceId.GetLength()] = 0; SendThisMessage(answer); - return; + return TRUE; } if (txt.Left(19) *= PString("AdviseOnTermination")) { PStringArray msg = txt.Tokenise(" "); adviseOnTermination = msg[1]; - return; + return TRUE; } if (txt.Left(6) *= PString("status")) { @@ -741,7 +780,7 @@ memcpy(answer.GetPointer(), msg.GetPointer(), msg.GetLength()); answer[msg.GetLength()] = 0; SendThisMessage(answer); - return; + return TRUE; } #ifdef PMEMORY_CHECK @@ -756,7 +795,7 @@ PFile::Create + PFile::Truncate)) { PTRACE(3, "Failed to open the specified file " << fileName << " to dump memory to"); - return; + return TRUE; } PMemoryHeap::DumpStatistics(); @@ -765,12 +804,10 @@ PMemoryHeap::DumpObjectsSince(0, result); outputFile.WriteLine(result); outputFile.Close(); - return; + return TRUE; } #endif - - PTRACE(1, "READ \"" << txt << "\" of size " << txt.GetSize()); if (txt == PString("list")) { PStringStream msg; PStringArray listNodes; @@ -783,40 +820,10 @@ memcpy(answer.GetPointer(), msg.GetPointer(), msg.GetLength()); answer[msg.GetLength()] = 0; SendThisMessage(answer); + return TRUE; } - PINDEX l = txt.Find(':'); - if (l == P_MAX_INDEX) { - PTRACE(3, "Could not find a colon in \"" << txt << "\", of size " << txt.GetLength()); - return; - } - PString dest = txt.Left(l).Trim(); - PString msg = txt.Mid(l + 1).Trim(); - - PTRACE(3, "Read in " << dest << "--:--" << msg); - // PTRACE(0, ::setw(32) << sourceId << " " <<txt ); - PBYTEArray outMessage; - outMessage.SetSize(sourceId.GetLength() + 1 + msg.GetLength() + 1); - memcpy(outMessage.GetPointer(), sourceId.GetPointer(), sourceId.GetLength()); - outMessage[sourceId.GetLength()] = ':'; - memcpy(outMessage.GetPointer() + sourceId.GetLength() + 1, msg.GetPointer(), msg.GetLength()); - outMessage[outMessage.GetSize() - 1] = 0; - // cerr << " Send " << PTime().AsString("hh:mm.ss.uuu") << " " - // << sourceId << ":" << msg << endl << endl; - PSafePtr <TcpConnection> conn = manager.GetConnectionWithLock(dest, PSafeReadOnly); - if (conn != NULL) { - PTRACE(3, "Found connection for id of " << dest); - conn->SendThisMessage(outMessage); - } else { - PTRACE(1, "Fail to send the message " - << PString((const char *)outMessage.GetPointer(), outMessage.GetSize()) - << " to " << dest); - PTRACE(1, " as could not get a lock"); - PTRACE(1, sourceId << " is sorry"); - IncCountDropped(); - } - - return; + return FALSE; } void TcpConnection::SendThisMessage(PBYTEArray & message) Modified: sofaswitch/trunk/server/main.h ============================================================================== --- sofaswitch/trunk/server/main.h (original) +++ sofaswitch/trunk/server/main.h Thu Aug 10 00:34:04 2006 @@ -34,8 +34,6 @@ #include <ptlib/sockets.h> #include <ptlib/safecoll.h> - - class TcpConnectionList; class TcpConnection; class Manager; @@ -108,6 +106,16 @@ /**Now, we have exactly one message, not multiple messages. Process it on */ void ProcessOneTextMessage(const PString & txt); + + /**There are several messages which are not to be passed on to some other + node. Instead, these messages are commands to this server. Commands to + this server are processed here. + + return TRUE If the supplied text is a command to this server, + + return FALSE if the supplied text is to be passsed on another node. + (typical case) */ + BOOL ProcessedAsServerCommand(const PString & text); /**Send the supplied message to the node on this socket */ void SendThisMessage(PBYTEArray & msg);