javauis/javalegacyutils/inc/CJavaEventServer.h
changeset 76 4ad59aaee882
parent 69 773449708c84
child 79 2f468c1958d0
equal deleted inserted replaced
69:773449708c84 76:4ad59aaee882
     1 /*
       
     2 * Copyright (c) 1999-2001 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef CJAVAEVENTSERVER_H
       
    20 #define CJAVAEVENTSERVER_H
       
    21 
       
    22 #include <e32std.h>
       
    23 #include <e32cmn.h>
       
    24 
       
    25 #include "mevents.h"
       
    26 
       
    27 enum {EJessExecute,EJessExecuteTrap, EJessShutdown};
       
    28 
       
    29 class RJess : public RSessionBase
       
    30 {
       
    31 public:
       
    32     TInt Connect(RServer2& aServer);
       
    33     inline TInt Execute(TAny* aParam)
       
    34     {
       
    35         return SendReceive(EJessExecute,TIpcArgs(aParam));
       
    36     }
       
    37     inline TInt ExecuteTrap(TAny* aParam)
       
    38     {
       
    39         return SendReceive(EJessExecuteTrap,TIpcArgs(aParam));
       
    40     }
       
    41     inline TInt Shutdown()
       
    42     {
       
    43         return SendReceive(EJessShutdown, TIpcArgs());
       
    44     }
       
    45 };
       
    46 
       
    47 class CJavaEventServer : public CBase
       
    48 {
       
    49 public:
       
    50     ~CJavaEventServer();
       
    51     static CJavaEventServer* NewL(const TDesC& aName,TThreadFunction aServerThread, TAny* aServerArg,
       
    52                                   TUint aStackSize = 0, RAllocator* aHeap = 0);
       
    53 
       
    54     // Notify Java dispatcher thread about new event, called from C++ thread
       
    55     static void NotifyJavaCall(TInt aEvent, TInt aPriority);
       
    56 
       
    57     // Cleanup the event server stuff, called from Java thread through JNI
       
    58     static void Cleanup(RArray<TInt>& aServerHandles);
       
    59 
       
    60     // Helper method to do tracing from the native to the Java side
       
    61     static void Trace(JNIEnv& aJni, TInt aValue);
       
    62 
       
    63     inline RJess& Session()
       
    64     {
       
    65         return iSession;
       
    66     }
       
    67     void PostEvent(CJavaEventBase* aEvent, TInt aPriority);
       
    68     static TInt ServerThread(TAny* aParam);
       
    69 
       
    70     void AddRef();
       
    71     void RemoveRef();
       
    72     void Shutdown();
       
    73 
       
    74 private:
       
    75     CJavaEventServer();
       
    76     void ConstructL(const TDesC& aUserName,TThreadFunction aServerThread, TAny* aServerArg,
       
    77                     TUint aStackSize, RAllocator* aHeap);
       
    78 
       
    79 private:
       
    80     TInt  iRef;
       
    81     RJess iSession;
       
    82     TBool iShutdown;
       
    83     RCriticalSection iMutex;
       
    84 
       
    85 };
       
    86 
       
    87 // CIntQueue helper class implements an integer FIFO queue The implementation is based on
       
    88 // fixed-sized array, because we don't want any garbage to be collected here and we don't
       
    89 // want to grab all the memory due to some misbehaving midlet. Synchronization is provided
       
    90 // in putItem() and removeItem() functions so it's safe to call them from different threads
       
    91 
       
    92 class CIntQueue : public CBase
       
    93 {
       
    94 
       
    95 public:
       
    96     virtual ~CIntQueue();
       
    97 
       
    98     static CIntQueue* NewL();        // two-phased constructor
       
    99     static CIntQueue* NewLC();       // two-phased constructor
       
   100 
       
   101     void  Enqueue(TInt aValue);      // add integer to the end of the queue
       
   102     TInt  Dequeue();                 // return integer from the beginning of the queue
       
   103     TBool IsEmpty();                 // checks if there are any items in queue
       
   104 
       
   105 private:
       
   106     void PutItem(TInt aItem);        // put new item to the buffer
       
   107     TInt RemoveItem();               // take item from the buffer
       
   108 
       
   109     CIntQueue();                     // default C++ constructor
       
   110     void ConstructL();               // by default Symbian 2nd phase constructor is private
       
   111 
       
   112 private:
       
   113     RArray<TInt>      iQueue;
       
   114     RMutex            iMutex;        // protect a critical regions of code
       
   115     RSemaphore        iFull;         // counts full buffer slots
       
   116     TInt              iCompressCounter; // counts event dequeuing
       
   117 };
       
   118 
       
   119 // CEventLock implements queues for manipulating of event and notification handlers.
       
   120 
       
   121 class CEventQueue : public CBase
       
   122 {
       
   123 
       
   124 public:
       
   125     virtual ~CEventQueue();
       
   126 
       
   127     void NewEvent(TInt aEvent);
       
   128     TInt GetNext();
       
   129 
       
   130     static void NotifyServer(TInt aEvent, TInt aThreadNumber);
       
   131     static void OnCloseThreads();
       
   132 
       
   133     static void InitL();
       
   134     static void Cleanup();
       
   135 
       
   136     static CEventQueue* GetQueue(TInt aThread);
       
   137     static const TInt KDispatchThreads = 2;      // two dispatching threads: events and notifications
       
   138 
       
   139 private:
       
   140     CEventQueue();
       
   141     void ConstructL();
       
   142 
       
   143 private:
       
   144     CIntQueue* iEventQueue;                      // queue for storing event handles
       
   145 };
       
   146 
       
   147 // clean-up event server objects on vm exit
       
   148 void CleanupEventServer();
       
   149 // init event server objects on vm start
       
   150 void InitEventServer();
       
   151 
       
   152 #endif // CJAVAEVENTSERVER_H
       
   153