javacommons/fileutils/src/driveobserverserver.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:  DriveObserverServer - platform independent
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "logger.h"
       
    21 #include "comms.h"
       
    22 #include "commsmessage.h"
       
    23 #include "commsendpoint.h"
       
    24 
       
    25 #include "driveobserverserver.h"
       
    26 #include "driveobservermessages.h"
       
    27 #include "drivechangedeventgenerator.h"
       
    28 
       
    29 using java::comms::CommsEndpoint;
       
    30 using java::comms::CommsMessage;
       
    31 
       
    32 namespace java
       
    33 {
       
    34 namespace fileutils
       
    35 {
       
    36 
       
    37 DriveObserverServer::DriveObserverServer()
       
    38         :iComms(0), iEventGenerator(0)
       
    39 {
       
    40     JELOG2(EJavaFile);
       
    41 }
       
    42 
       
    43 DriveObserverServer::~DriveObserverServer()
       
    44 {
       
    45     JELOG2(EJavaFile);
       
    46 
       
    47     // delete event generator
       
    48     if (iEventGenerator)
       
    49     {
       
    50         iEventGenerator->stopEvents();
       
    51         delete iEventGenerator;
       
    52     }
       
    53 
       
    54     // Clear listeners and subscribers collections
       
    55     iListeners.clear();
       
    56     iSubscribers.clear();
       
    57 }
       
    58 
       
    59 void DriveObserverServer::startServer(CommsEndpoint* aComms)
       
    60 {
       
    61     JELOG2(EJavaFile);
       
    62 
       
    63     iComms = aComms;
       
    64 
       
    65     // Register moduleId
       
    66     if (iComms)
       
    67     {
       
    68         iComms->registerListener(java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C, this);
       
    69     }
       
    70 }
       
    71 
       
    72 void DriveObserverServer::stopServer()
       
    73 {
       
    74     JELOG2(EJavaFile);
       
    75 
       
    76     if (iEventGenerator)
       
    77     {
       
    78         iEventGenerator->stopEvents();
       
    79     }
       
    80 
       
    81     if (iComms)
       
    82     {
       
    83         // Unregister moduleId
       
    84         iComms->unregisterListener(java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C, this);
       
    85     }
       
    86 }
       
    87 
       
    88 
       
    89 void DriveObserverServer::registerListener(DriveListenerInterface* aListener)
       
    90 {
       
    91     JELOG2(EJavaFile);
       
    92 
       
    93     iListeners.push_back(aListener);
       
    94 
       
    95     enableEvents();
       
    96 }
       
    97 
       
    98 void DriveObserverServer::unregisterListener(DriveListenerInterface* aListener)
       
    99 {
       
   100     JELOG2(EJavaFile);
       
   101 
       
   102     for (listenersIter iter = iListeners.begin();
       
   103             iter != iListeners.end() ; /* Empty*/)
       
   104     {
       
   105         if (*iter == aListener)
       
   106         {
       
   107             iter = iListeners.erase(iter);
       
   108         }
       
   109         else
       
   110         {
       
   111             ++iter;
       
   112         }
       
   113     }
       
   114     disableEventsIfNotNeeded();
       
   115 }
       
   116 
       
   117 void DriveObserverServer::driveChanged(const int& aOperation, const driveInfo& aDriveInfo)
       
   118 {
       
   119     JELOG2(EJavaFile);
       
   120 
       
   121     for (listenersIter iter = iListeners.begin() ;
       
   122             iter != iListeners.end() ; ++iter)
       
   123     {
       
   124         (*iter)->driveChanged(aOperation, aDriveInfo);
       
   125     }
       
   126 
       
   127     if (iSubscribers.size() > 0)
       
   128     {
       
   129         CommsMessage msg;
       
   130 
       
   131         for (subscribersIter iter = iSubscribers.begin() ;
       
   132                 iter != iSubscribers.end() ; ++iter)
       
   133         {
       
   134             setDriveChangedParams(msg, (*iter)->iSubscriberAddress,
       
   135                                   (*iter)->iModuleId, aOperation, aDriveInfo);
       
   136             int rc = 0;
       
   137             LOG2(EJavaFile, EInfo, "Sending driveChanged message to %d/%d",
       
   138                  (*iter)->iSubscriberAddress, (*iter)->iModuleId);
       
   139             if (0 != (rc = iComms->send(msg)))
       
   140             {
       
   141                 ELOG1(EJavaFile, "Event message sent failed %d", rc);
       
   142             }
       
   143             msg.reset();
       
   144         }
       
   145     }
       
   146 }
       
   147 
       
   148 void DriveObserverServer::processMessage(CommsMessage& aMessage)
       
   149 {
       
   150     JELOG2(EJavaFile);
       
   151 
       
   152     switch (aMessage.getMessageId())
       
   153     {
       
   154     case DRIVEOBSERVER_MSG_SUBSCRIBE_EVENTS:
       
   155         registerSubscriber(aMessage);
       
   156         break;
       
   157 
       
   158     case DRIVEOBSERVER_MSG_UNSUBSCRIBE_EVENTS:
       
   159         unregisterSubscriber(aMessage);
       
   160         break;
       
   161 
       
   162     case DRIVEOBSERVER_MSG_GETDRIVES_REQUEST:
       
   163         getDrives(aMessage);
       
   164         break;
       
   165 
       
   166     default:
       
   167         ELOG1(EJavaFile, "Unknown message received %d", aMessage.getMessageId());
       
   168     }
       
   169 }
       
   170 
       
   171 void DriveObserverServer::registerSubscriber(CommsMessage& aMessage)
       
   172 {
       
   173     JELOG2(EJavaFile);
       
   174 
       
   175     int subsModuleId = 0;
       
   176     getSubscribeParams(aMessage, subsModuleId);
       
   177     iSubscribers.push_back(new subscriberData(subsModuleId,
       
   178                            aMessage.getSender()));
       
   179     enableEvents();
       
   180 }
       
   181 
       
   182 void DriveObserverServer::unregisterSubscriber(CommsMessage& aMessage)
       
   183 {
       
   184     JELOG2(EJavaFile);
       
   185 
       
   186     for (subscribersIter iter = iSubscribers.begin() ;
       
   187             iter != iSubscribers.end() ; /* empty*/)
       
   188     {
       
   189         if ((*iter)->iSubscriberAddress == aMessage.getSender() &&
       
   190                 (*iter)->iModuleId          == aMessage.getModuleId())
       
   191         {
       
   192             iter = iSubscribers.erase(iter);
       
   193         }
       
   194         else
       
   195         {
       
   196             ++iter;
       
   197         }
       
   198     }
       
   199     disableEventsIfNotNeeded();
       
   200 }
       
   201 
       
   202 void DriveObserverServer::getDrives(CommsMessage& aMessage)
       
   203 {
       
   204     JELOG2(EJavaFile);
       
   205 
       
   206     int driveTypes = 0;
       
   207     getGetDrivesParams(aMessage, driveTypes);
       
   208 
       
   209     driveInfos drives;
       
   210     CommsMessage replyMsg;
       
   211     replyMsg.replyTo(aMessage);
       
   212 
       
   213     switch (driveTypes)
       
   214     {
       
   215     case DRIVEOBSERER_GET_DRIVES_ALL:
       
   216         DriveUtilities::getAllDrives(drives);
       
   217         break;
       
   218     case DRIVEOBSERER_GET_DRIVES_ACCESIBLE:
       
   219         DriveUtilities::getAccesibleDrives(drives);
       
   220         break;
       
   221     }
       
   222 
       
   223     setGetDrivesResultParams(replyMsg, drives);
       
   224     iComms->send(replyMsg);
       
   225 }
       
   226 
       
   227 void DriveObserverServer::enableEvents()
       
   228 {
       
   229     JELOG2(EJavaFile);
       
   230 
       
   231     if (!iEventGenerator)
       
   232     {
       
   233         // Create platform specific event generator
       
   234         iEventGenerator = new DriveChangedEventGenerator(this);
       
   235         iEventGenerator->startEvents();
       
   236     }
       
   237 }
       
   238 
       
   239 void DriveObserverServer::disableEventsIfNotNeeded()
       
   240 {
       
   241     JELOG2(EJavaFile);
       
   242 
       
   243     if (0 == iSubscribers.size() &&
       
   244             0 == iListeners.size() &&
       
   245             iEventGenerator)
       
   246     {
       
   247         iEventGenerator->stopEvents();
       
   248         delete iEventGenerator;
       
   249         iEventGenerator = 0;
       
   250     }
       
   251 }
       
   252 
       
   253 } // end of namespace fileutils
       
   254 } // end of namespace java