Author: derek Date: Tue Aug 8 18:08:02 2006 New Revision: 59 Modified: sofaswitch/trunk/server/main.cxx sofaswitch/trunk/server/main.h sofaswitch/trunk/sofa/src/messages.cxx Log: Add ability for server to generate a unique ID. This is useful for when there are multiple controllers, and we don't know the id of the other controllers. Add command to query server, and ask the server what our id is. This command is used when we ask the server to assign us an ID. Add facility so that when node X dies on the server, the server will advise node Y. This is useful to handle the case when node X crashes. Modified: sofaswitch/trunk/server/main.cxx ============================================================================== --- sofaswitch/trunk/server/main.cxx (original) +++ sofaswitch/trunk/server/main.cxx Tue Aug 8 18:08:02 2006 @@ -254,6 +254,14 @@ void Manager::AppendRunning(PSafePtr<TcpConnection> conn, const PString & id) { PTRACE(3, "Manager\tAppendRunning " << *conn); + + if (id == PString("?")) { + conn->GenerateSourceId(); + PTRACE(3, "ADD " << conn->GetSourceId() << " which is a random Id"); + connectionsActive.SetAt(conn->GetSourceId(), conn); + return; + } + if (connectionsActive.Contains(id)) { cerr << " ERROR - CANNOT add this id " << id << " it already exits" << endl; PTRACE(3, "Cannot cope with duplicate instances of the id " << id); @@ -347,6 +355,24 @@ TcpConnection::~TcpConnection() { PTRACE(3, "Destroy this connection " << *this << " now"); + + if (!adviseOnTermination.IsEmpty()) { + PStringStream msg; + cerr << "send terminating message to "<< adviseOnTermination << " from " + << sourceId << endl; + msg << sourceId << ":Terminating"; + PSafePtr <TcpConnection> conn = manager.GetConnectionWithLock(adviseOnTermination, + PSafeReadOnly); + if (conn != NULL) { + PBYTEArray binary; + binary.SetSize(msg.GetSize()); + memcpy(binary.GetPointer(), msg.GetPointer(), msg.GetLength()); + binary[msg.GetSize()] = 0; + conn->SendThisMessage(binary); + } + + } + PWaitAndSignal m(incomingLock); if (incomingMessages != NULL) { @@ -361,7 +387,14 @@ DecCountTcpConnections(); } +void TcpConnection::GenerateSourceId() +{ + PINDEX thisId = manager.GetNewUniqueId(); + PString newId = psprintf("c%d", thisId); + AssignSourceId(newId); +} + void TcpConnection::StartRunning() { /* If the messagesSocket is bad/closed, the incomingMessages thread @@ -668,6 +701,21 @@ messagesSocket.Close(); manager.QuitNow(); return; + } + + if (txt.Left(6) *= PString("WhoAmI")){ + PBYTEArray answer; + answer.SetSize(sourceId.GetLength() + 1); + memcpy(answer.GetPointer(), sourceId.GetPointer(), sourceId.GetLength()); + answer[sourceId.GetLength()] = 0; + SendThisMessage(answer); + return; + } + + if (txt.Left(19) *= PString("AdviseOnTermination")) { + PStringArray msg = txt.Tokenise(" "); + adviseOnTermination = msg[1]; + return; } if (txt.Left(6) *= PString("status")) { Modified: sofaswitch/trunk/server/main.h ============================================================================== --- sofaswitch/trunk/server/main.h (original) +++ sofaswitch/trunk/server/main.h Tue Aug 8 18:08:02 2006 @@ -68,6 +68,12 @@ accept it */ void AssignSourceId(PString & newId) { sourceId = newId; } + /**The remote node has no idea what to use for a source ID. Consequently, we have to +generate a source ID here, and advise the remote node what the source +ID is. This method will alos assign the source id to this class. This method +is called wwhen this class is created. */ + void GenerateSourceId(); + /**Report the source id as used by this connection */ PString GetSourceId() { return sourceId; } @@ -193,6 +199,11 @@ on a random number. It is used to uniquely identify this class, wherever it is */ PString globalUniqueId; + + /** Set the node to advise on death of this connection. For example, the + voip node for one circuit will use this, as a means to advise the + controller that it has died. */ + PString adviseOnTermination; }; //////////////////////////////////////////////////////////////////////////////// @@ -324,6 +335,9 @@ used on those TcpConnection instances that do not start up correctly */ PString GetNextBogusId(); + + /**Get the index of the next unique ID that we are generating */ + PINDEX GetNewUniqueId() { return ++uniqueIdGenerator; } protected: /**The thread safe list of connections that we manage */ @@ -352,6 +366,9 @@ /**The count of all threads that have beeen spawned to process messages*/ PAtomicInteger connectionCount; + + /**The count of all the number of unique ID's we have created */ + PAtomicInteger uniqueIdGenerator; /**A class that manages the list of available TcpConnection threads */ TcpConnectionList availableTcpConnections; Modified: sofaswitch/trunk/sofa/src/messages.cxx ============================================================================== --- sofaswitch/trunk/sofa/src/messages.cxx (original) +++ sofaswitch/trunk/sofa/src/messages.cxx Tue Aug 8 18:08:02 2006 @@ -449,7 +449,6 @@ PTRACE(3, "Failed to send message " << messagesSocket.GetErrorText()); } writeMessageLock.Signal(); - } void MessageFrame::ProcessMessage(PString message, PString sender) @@ -896,6 +895,17 @@ { remoteController = rc; PTRACE(3, "SET Remote controller to " << remoteController); + + PStringStream message; + message << "AdviseOnTermination " << rc; + PBYTEArray binary; + binary.SetSize(message.GetSize()); + memcpy(binary.GetPointer(), message.(), message.GetLength()); + binary[message.GetLength()] = 0; + + writeMessageLock.Wait(); + messagesSocket.Write(binary.GetPointer(), binary.GetSize()); + writeMessageLock.Signal(); }