Exemplo n.º 1
0
UtlBoolean SipProtocolServerBase::send(SipMessage* message,
                            const char* hostAddress,
                            int hostPort)
{
    UtlBoolean sendOk = FALSE;


    UtlString localIp(message->getLocalIp());
    
    if (localIp.length() < 1)
    {
        localIp = mDefaultIp;
    }

    SipClient* client = createClient(hostAddress, hostPort, localIp);
    if(client)
    {
        int isBusy = client->isInUseForWrite();
        UtlString clientNames;

        client->getClientNames(clientNames);
        OsSysLog::add(FAC_SIP, PRI_DEBUG, "Sip%sServerBase::send %p isInUseForWrite %d, client info\n %s",
                mProtocolString.data(), client, isBusy, clientNames.data());

        sendOk = client->sendTo(*message, hostAddress, hostPort);
        if(!sendOk)
        {
            OsTask* pCallingTask = OsTask::getCurrentTask();
            OsTaskId_t callingTaskId = -1;
            OsTaskId_t clientTaskId = -1;

            if ( pCallingTask )
            {
               pCallingTask->id(callingTaskId);
            }
            client->id(clientTaskId);

            if (clientTaskId != callingTaskId)
            {
               // Do not need to clientLock.acquireWrite();
               // as deleteClient uses the locking list lock
               // which is all that is needed as the client is
               // already marked as busy when we called
               // createClient above.
               deleteClient(client);
               client = NULL;
            }
        }
    }

        if(client)
        {
            releaseClient(client);
        }

    return(sendOk);
}
Exemplo n.º 2
0
void SipProtocolServerBase::removeOldClients(long oldTime)
{
    mClientLock.acquireWrite();
    // Find the old clients in the list  and shut them down
    int iteratorHandle = mClientList.getIteratorHandle();
    SipClient* client;
    int numClients = mClientList.getCount();
    int numDelete = 0;
    int numBusy = 0;
    SipClient** deleteClientArray = NULL;


    UtlString clientNames;
    while ((client = (SipClient*)mClientList.next(iteratorHandle)))
    {
        if(client->isInUseForWrite()) numBusy++;

        // Remove any client with a bad socket
        // With TCP clients let them stay around if they are still
        // good as the may stay open for the session
        // The clients opened from this side for sending requests
        // get closed by the server (i.e. other side).  The clients
        // opened as servers for requests from the remote side are
        // explicitly closed on this side when the final response is
        // sent.
        if(   ! client->isInUseForWrite() // can't remove it if writing to it...
           && (   ! client->isOk() // socket is bad
               || client->getLastTouchedTime() < oldTime // idle for long enough
               )
           )
        {
            client->getClientNames(clientNames);
#ifdef TEST_PRINT
            osPrintf("Removing %s client names:\n%s\r\n",
                mProtocolString.data(), clientNames.data());
#endif
            OsSysLog::add(FAC_SIP, PRI_DEBUG, "Sip%sServer::Removing old client %p:\n%s\r",
                          mProtocolString.data(), client, clientNames.data());

            mClientList.remove(iteratorHandle);
            // Delete the clients after releasing the lock
            if(!deleteClientArray) deleteClientArray =
                new SipClient*[numClients];

            deleteClientArray[numDelete] = client;
            numDelete++;

            client = NULL;
        }
        else
        {
#           ifdef TEST_PRINT
            UtlString names;
            client->getClientNames(names);
            OsSysLog::add(FAC_SIP, PRI_DEBUG, "Sip%sServer::removeOldClients leaving client:\n%s",
                mProtocolString.data(), names.data());
#           endif
        }
    }
    mClientList.releaseIteratorHandle(iteratorHandle);
    mClientLock.releaseWrite();

    if ( numDelete || numBusy ) // get rid of lots of 'doing nothing when nothing to do' messages in the log
    {
        OsSysLog::add(FAC_SIP, PRI_DEBUG,
                      "Sip%sServer::removeOldClients deleting %d of %d SipClients (%d busy)",
                      mProtocolString.data(), numDelete, numClients, numBusy);
    }
    // These have been removed from the list so delete them
    // after releasing the locks
    for(int clientIndex = 0; clientIndex < numDelete; clientIndex++)
    {
        delete deleteClientArray[clientIndex];
    }

    if(deleteClientArray)
    {
        delete[] deleteClientArray;
        deleteClientArray = NULL;
    }
}
Exemplo n.º 3
0
SipClient* SipProtocolServerBase::createClient(const char* hostAddress,
                                               int hostPort,
                                               const char* localIp)
{
    UtlString remoteHostAddr;
    UtlBoolean clientStarted = FALSE;

    mClientLock.acquireWrite();

    SipClient* client = getClient(hostAddress, hostPort, localIp);

    if(! client)
    {
#       if TEST_CLIENT_CREATION
        OsSysLog::add(FAC_SIP, PRI_DEBUG, "SipProtocolServerBase::createClient( %s, %d )",
                      hostAddress, hostPort);
#       endif

        if(!portIsValid(hostPort))
        {
            hostPort = mDefaultPort;
#           if TEST_CLIENT_CREATION
            OsSysLog::add(FAC_SIP, PRI_DEBUG,
                          "SipProtocolServerBase::createClient port defaulting to %d",
                          hostPort);
#           endif
        }

        OsTime time;
        OsDateTime::getCurTimeSinceBoot(time);
        long beforeSecs = time.seconds();

        OsSocket* clientSocket = buildClientSocket(hostPort, hostAddress, localIp);

        OsDateTime::getCurTimeSinceBoot(time);
        long afterSecs = time.seconds();
        if(afterSecs - beforeSecs > 1)
        {
            OsSysLog::add(FAC_SIP, PRI_WARNING, "SIP %s socket create for %s:%d took %d seconds",
                mProtocolString.data(), hostAddress, hostPort,
                (int)(afterSecs - beforeSecs));
        }

        UtlBoolean isOk = clientSocket->isOk();
        int writeWait = 3000; // mSec
        UtlBoolean isReadyToWrite = clientSocket->isReadyToWrite(writeWait);

        if(!isReadyToWrite)
        {
            OsSysLog::add(FAC_SIP, PRI_WARNING,
                          "SIP %s socket %s:%d not ready for writing after %d seconds",
                          mProtocolString.data(), hostAddress, hostPort, (int) (writeWait/1000));
        }

        if(isOk &&
           isReadyToWrite)
        {
#ifdef TEST
            osPrintf("Socket OK, creating client\n");
#endif
            client = new SipClient(clientSocket) ;
            if (client && mSipUserAgent->getUseRport() &&
                    clientSocket->getIpProtocol() == OsSocket::UDP)
            {
                client->setSharedSocket(TRUE) ;
            }

#ifdef TEST
            osPrintf("Created client\n");
#endif
            if(mSipUserAgent)
            {
                client->setUserAgent(mSipUserAgent);
            }

            if (clientSocket->getIpProtocol() != OsSocket::UDP)
            {
                //osPrintf("starting client\n");
                clientStarted = client->start();
                if(!clientStarted)
                {
                    osPrintf("SIP %s client failed to start\n",
                        mProtocolString.data());
                }
            }

            OsSysLog::add(FAC_SIP, PRI_DEBUG, "Sip%sServer::createClient client: %p %s -> %s:%d",
                mProtocolString.data(), client, localIp, hostAddress, hostPort);

            mClientList.push(client);
        }

        // The socket failed to be connected
        else
        {
            if(clientSocket)
            {
                if (!mSipUserAgent->getUseRport() ||
                        (clientSocket->getIpProtocol() == OsSocket::TCP))
                {
                    delete clientSocket;
                }
                clientSocket = NULL;
            }
            OsSysLog::add(FAC_SIP, PRI_WARNING,
                          "Sip%sServer::createClient client %p Failed to create socket %s -> %s:%d",
                          mProtocolString.data(), this, localIp, hostAddress, hostPort);
        }
    }

    int isBusy = FALSE;
    if(client)
    {
        isBusy = client->isInUseForWrite();

        if(!isBusy)
            client->markInUseForWrite();
    }

    mClientLock.releaseWrite();

    if(client && isBusy)
    {
        if(!waitForClientToWrite(client)) client = NULL;
    }

    return(client);
}