javacommons/comms/ipclib/clientserver/src.s60/ipcserver.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: IpcServer is the server side implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <memory>
       
    20 #include <sstream>
       
    21 #include <stdlib.h>
       
    22 
       
    23 #include "logger.h"
       
    24 #include "javasymbianoslayer.h"
       
    25 
       
    26 #include "ipcserver.h"
       
    27 #include "commsserver.h"
       
    28 #include "commssession.h"
       
    29 #include "common.h"
       
    30 
       
    31 namespace java
       
    32 {
       
    33 namespace comms
       
    34 {
       
    35 using java::util::ScopedLock;
       
    36 
       
    37 IpcServer::IpcServer(IpcListener* aListener) : CActive(EPriorityStandard),
       
    38         mRunning(false), mAddress(0), mListener(aListener)
       
    39 {
       
    40 }
       
    41 
       
    42 IpcServer::~IpcServer()
       
    43 {
       
    44 
       
    45 }
       
    46 
       
    47 int IpcServer::start(int aAddr)
       
    48 {
       
    49     ScopedLock lock(mMutex);
       
    50 
       
    51     if (mRunning) return 0;
       
    52 
       
    53     mAddress = aAddr;
       
    54 
       
    55     // create unique thread name
       
    56     std::stringstream name;
       
    57     name << "JavaCommsServer-" << mAddress << "-" << this << "-" << rand();
       
    58     std::auto_ptr<HBufC> threadName(stringToDes(name.str().c_str()));
       
    59 
       
    60     TInt rc = mThread.Create(
       
    61                   threadName->Des(),
       
    62                   reinterpret_cast<TThreadFunction>(messageLoop),
       
    63                   KCommsStackSize,
       
    64                   NULL,
       
    65                   this);
       
    66 
       
    67     if (rc == KErrNone)
       
    68     {
       
    69         // block until thread has been started ok
       
    70         TRequestStatus rendezvousStatus;
       
    71         mThread.Rendezvous(rendezvousStatus);
       
    72         mThread.Resume();
       
    73         User::WaitForRequest(rendezvousStatus);
       
    74         rc = rendezvousStatus.Int();
       
    75         if (rc == KErrNone)
       
    76         {
       
    77             mRunning = true;
       
    78         }
       
    79     }
       
    80 
       
    81     if (rc != KErrNone)
       
    82     {
       
    83         mThread.Close();
       
    84         ELOG3(EJavaComms, "%s failed on java-comms-%d, err = %d", __PRETTY_FUNCTION__, aAddr, rc);
       
    85     }
       
    86     return rc;
       
    87 }
       
    88 
       
    89 void IpcServer::stop()
       
    90 {
       
    91 //    JELOG2(EJavaComms);
       
    92     ScopedLock lock(mMutex);
       
    93 
       
    94     if (!mRunning) return;
       
    95 
       
    96     TRequestStatus status;
       
    97     mThread.Logon(status);
       
    98 
       
    99     TRequestStatus* istatus = &iStatus;
       
   100     mThread.RequestComplete(istatus, KErrNone);
       
   101 
       
   102     User::WaitForRequest(status);
       
   103     mThread.Close();
       
   104 
       
   105     LOG1(EJavaComms, EInfo, "IpcServer stopped on java-comms-%d", mAddress);
       
   106 }
       
   107 
       
   108 int IpcServer::send(ipcMessage_t* aMsg)
       
   109 {
       
   110     ScopedLock lock(mMutex);
       
   111 
       
   112     int rc = KErrNotFound;
       
   113     if (mRunning)
       
   114     {
       
   115         mServer->iSessionIter.SetToFirst();
       
   116         CCommsSession* s = 0;
       
   117         while ((s = static_cast<CCommsSession*>(mServer->iSessionIter++)) !=0)
       
   118         {
       
   119             if (aMsg->ipcHeader.receiver == s->getId())
       
   120             {
       
   121                 rc = s->send(aMsg);
       
   122             }
       
   123         }
       
   124     }
       
   125 
       
   126     if (rc)
       
   127     {
       
   128         ELOG2(EJavaComms, "%s failed, err = %d", __PRETTY_FUNCTION__, rc);
       
   129     }
       
   130     return rc;
       
   131 }
       
   132 
       
   133 void IpcServer::RunL()
       
   134 {
       
   135     CActiveScheduler::Stop();
       
   136 }
       
   137 
       
   138 void IpcServer::DoCancel()
       
   139 {
       
   140     ELOG1(EJavaComms, "%s", __PRETTY_FUNCTION__);
       
   141 }
       
   142 
       
   143 TInt IpcServer::RunError(TInt aError)
       
   144 {
       
   145     ELOG2(EJavaComms, "%s, err = %d", __PRETTY_FUNCTION__, aError);
       
   146     return KErrNone;
       
   147 }
       
   148 
       
   149 void IpcServer::messageLoop(TAny* aArgs)
       
   150 {
       
   151     std::auto_ptr<CTrapCleanup> cleanup(CTrapCleanup::New());
       
   152     TInt r = KErrNoMemory;
       
   153     if (cleanup.get())
       
   154     {
       
   155         IpcServer* me = reinterpret_cast<IpcServer*>(aArgs);
       
   156         TRAP(r,me->doMainL());
       
   157         me->mListener->onExit();
       
   158         me->mRunning = false;
       
   159     }
       
   160     // stop blocks until this notification
       
   161     RThread::Rendezvous(r);
       
   162 }
       
   163 
       
   164 void IpcServer::doMainL()
       
   165 {
       
   166     CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
   167     CleanupStack::PushL(s);
       
   168     CActiveScheduler::Install(s);
       
   169 
       
   170     mServer = new(ELeave)CCommsServer(*mListener);
       
   171     CleanupStack::PushL(mServer);
       
   172 
       
   173     std::stringstream address;
       
   174     address << "java-comms-" << mAddress;
       
   175     std::auto_ptr<HBufC> serverName(stringToDes(address.str().c_str()));
       
   176     mServer->StartL(serverName->Des());
       
   177     LOG1(EJavaComms, EInfo, "IpcServer started on %s", address.str().c_str());
       
   178 
       
   179     CActiveScheduler::Add(this);
       
   180     iStatus = KRequestPending;
       
   181     SetActive();
       
   182 
       
   183     // start blocks until this notification
       
   184     RThread::Rendezvous(KErrNone);
       
   185     mListener->onStart();
       
   186 
       
   187     CActiveScheduler::Start();
       
   188     CleanupStack::PopAndDestroy(mServer);
       
   189     CleanupStack::PopAndDestroy(s);
       
   190 }
       
   191 
       
   192 
       
   193 } // namespace comms
       
   194 } // namespace java