[IO] Added Event class support to FileEventManager and SerialPort (from Hari)
authorHariprasad Taduru <hariprasad.t@samsung.com>
Thu, 4 Apr 2013 07:19:46 +0000 (12:49 +0530)
committerSunwook Bae <sunwook45.bae@samsung.com>
Fri, 5 Apr 2013 08:25:18 +0000 (17:25 +0900)
Change-Id: I9ec20e72c512ba93f8b9a78f823f29dd9ac47207
Signed-off-by: Hariprasad Taduru <hariprasad.t@samsung.com>
Signed-off-by: Sunwook Bae <sunwook45.bae@samsung.com>
src/io/FIo_FileEventManagerImpl.cpp
src/io/FIo_SerialPortImpl.cpp
src/io/FIo_SerialPortImpl.h
src/io/inc/FIo_FileEventManagerImpl.h

index a2ed063..9ce1b20 100644 (file)
@@ -30,6 +30,7 @@
 #include <FBaseString.h>
 #include <FBaseSysLog.h>
 #include <FBaseColAllElementsDeleter.h>
 #include <FBaseString.h>
 #include <FBaseSysLog.h>
 #include <FBaseColAllElementsDeleter.h>
+#include <FBaseRtIEventArg.h>
 #include <FIoFile.h>
 #include <FIoIFileEventListener.h>
 #include <FIoFileEventManager.h>
 #include <FIoFile.h>
 #include <FIoIFileEventListener.h>
 #include <FIoFileEventManager.h>
 
 using namespace std;
 using namespace Tizen::Base;
 
 using namespace std;
 using namespace Tizen::Base;
+using namespace Tizen::Base::Runtime;
 using namespace Tizen::Base::Collection;
 
 namespace Tizen { namespace Io
 {
 
 using namespace Tizen::Base::Collection;
 
 namespace Tizen { namespace Io
 {
 
+class _FileEventArg
+        : public IEventArg
+{
+public:
+        _FileEventArg(unsigned long event, String path, unsigned int eventId)
+                : __event(event)
+                , __path(path)
+               , __eventId(eventId)
+        {
+        }
+       unsigned long __event;
+       String __path;
+       unsigned int __eventId;
+};
+
+class _FileEvent
+        : public Event
+{
+protected:
+        virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
+        {
+                IFileEventListener* pListener = dynamic_cast<IFileEventListener*> (&listener);
+                if (pListener != null)
+                {
+                        const _FileEventArg* pArg = dynamic_cast<const _FileEventArg*>(&arg);
+                        if (pArg != null)
+                        {
+                               pListener->OnFileEventOccured(pArg->__event, pArg->__path, pArg->__eventId);
+                        }
+                }
+        }
+};
+
 _FileEventManagerImpl::_FileEventManagerImpl(void)
        : __pMonitorFileList(null)
 _FileEventManagerImpl::_FileEventManagerImpl(void)
        : __pMonitorFileList(null)
-       , __pFileEventListener(null)
+       , __pEvent(null)
        , __inotifyFd(-1)
        , __pGSource(null)
        , __pGIOChannel(null)
        , __inotifyFd(-1)
        , __pGSource(null)
        , __pGIOChannel(null)
@@ -79,6 +114,8 @@ _FileEventManagerImpl::~_FileEventManagerImpl(void)
                __pMonitorFileList->RemoveAll(true);
                delete __pMonitorFileList;
        }
                __pMonitorFileList->RemoveAll(true);
                delete __pMonitorFileList;
        }
+
+       delete __pEvent;
 }
 
 gboolean
 }
 
 gboolean
@@ -124,11 +161,13 @@ _FileEventManagerImpl::SendEvent(void)
                        path = GetRegisteredPath(pEvent->wd);
                }
 
                        path = GetRegisteredPath(pEvent->wd);
                }
 
-               if (__pFileEventListener != null)
-               {
-                       __pFileEventListener->OnFileEventOccured(event, path, eventId);
+               _FileEventArg* pEventArg= new (std::nothrow) _FileEventArg(event, path, eventId);
+                if(pEventArg != null)
+                {
+                       SysLog(NID_IO, "_FileEventManagerImpl::SendEvent Event Fire");
+                       __pEvent->Fire(*pEventArg);
                        r = E_SUCCESS;
                        r = E_SUCCESS;
-               }
+                }
 
                iter += sizeof(struct inotify_event) + pEvent->len;
        }
 
                iter += sizeof(struct inotify_event) + pEvent->len;
        }
@@ -141,6 +180,7 @@ _FileEventManagerImpl::Construct(IFileEventListener& listener)
 {
        GMainContext* pGContext = null;
        result r = E_SUCCESS;
 {
        GMainContext* pGContext = null;
        result r = E_SUCCESS;
+       _FileEvent* pEvent = null;
        __inotifyFd = inotify_init();
 
        if (__inotifyFd == -1 && errno == EMFILE)
        __inotifyFd = inotify_init();
 
        if (__inotifyFd == -1 && errno == EMFILE)
@@ -164,21 +204,19 @@ _FileEventManagerImpl::Construct(IFileEventListener& listener)
                SysTryCatch(NID_IO, pGContext != null, r = E_IO, E_IO, "[E_IO] Failed to get glib context.");
        }
 
                SysTryCatch(NID_IO, pGContext != null, r = E_IO, E_IO, "[E_IO] Failed to get glib context.");
        }
 
-       SysLog(NID_IO, "GContext:%x", pGContext);
+       __pEvent = new (std::nothrow) _FileEvent();
+        SysTryReturnResult(NID_IO, __pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
 
 
-       __pFileEventListener = &listener;
+       __pEvent->AddListener(listener);
 
        __pGIOChannel = g_io_channel_unix_new(__inotifyFd); //fd wrapping
        SysTryCatch(NID_IO, __pGIOChannel != null, r = E_IO, E_IO, "[E_IO] Failed to create glib channel.");
 
        __pGIOChannel = g_io_channel_unix_new(__inotifyFd); //fd wrapping
        SysTryCatch(NID_IO, __pGIOChannel != null, r = E_IO, E_IO, "[E_IO] Failed to create glib channel.");
-       SysLog(NID_IO, "__pGIOChannel:%x", __pGIOChannel);
 
        __pGSource = g_io_create_watch(__pGIOChannel, (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
        SysTryCatch(NID_IO, __pGSource != null, r = E_IO, E_IO, "[E_IO] Failed to create glib watch.");
 
        __pGSource = g_io_create_watch(__pGIOChannel, (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
        SysTryCatch(NID_IO, __pGSource != null, r = E_IO, E_IO, "[E_IO] Failed to create glib watch.");
-       SysLog(NID_IO, "__pGSource:%x", __pGSource);
 
        g_source_set_callback(__pGSource, (GSourceFunc)OnFileEventOccured, this, NULL);
        g_source_attach(__pGSource, pGContext);
 
        g_source_set_callback(__pGSource, (GSourceFunc)OnFileEventOccured, this, NULL);
        g_source_attach(__pGSource, pGContext);
-       SysLog(NID_IO, "Attached fd on g_source");
 
        __pMonitorFileList = pMonitorFileList.release();
 
 
        __pMonitorFileList = pMonitorFileList.release();
 
index 5fb4d57..69a5c02 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <FBaseErrors.h>
 #include <FBaseSysLog.h>
 
 #include <FBaseErrors.h>
 #include <FBaseSysLog.h>
+#include <FBaseRtIEventArg.h>
 
 #include <FIo_AppServiceIpcMessages.h>
 #include <FIo_IpcClient.h>
 
 #include <FIo_AppServiceIpcMessages.h>
 #include <FIo_IpcClient.h>
 using namespace std;
 using namespace Tizen::App;
 using namespace Tizen::Base;
 using namespace std;
 using namespace Tizen::App;
 using namespace Tizen::Base;
+using namespace Tizen::Base::Runtime;
 using namespace Tizen::Base::Collection;
 
 namespace Tizen { namespace Io
 {
 
 using namespace Tizen::Base::Collection;
 
 namespace Tizen { namespace Io
 {
 
-const String COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
-const String ACCESSORY_MANAGER_SERVICE_ID = L"osp.accessorymanager.service";
+const wchar_t COMMUNICATION_DISPATCHER_IPC_ID[] = L"osp.app.ipcserver.communicationdispatcher";
+const wchar_t ACCESSORY_MANAGER_SERVICE_ID[] = L"osp.accessorymanager.service";
+const wchar_t SERIAL_COMMAND_OPEN[] = L"Open";
+const wchar_t SERIAL_COMMAND_CLOSE[] = L"Close";
+const wchar_t SERIAL_COMMAND_WRITE[] = L"Write";
+const wchar_t SERIAL_COMMAND_DATA[] = L"Data";
+const wchar_t SERIAL_COMMAND_ERROR[] = L"Error";
 
 
-const String SERIAL_COMMAND_OPEN = L"Open";
-const String SERIAL_COMMAND_CLOSE = L"Close";
-const String SERIAL_COMMAND_WRITE = L"Write";
-const String SERIAL_COMMAND_DATA = L"Data";
-const String SERIAL_COMMAND_ERROR = L"Error";
-
-const int SERIAL_BUFFER_SIZE = 536870912; //512KB
+const int SERIAL_BUFFER_SIZE = 512 * 1024; //512KB
 const int SERIAL_DATA_HEAD = 1;
 const int SERIAL_DATA_BODY = 2;
 
 const int SERIAL_DATA_HEAD = 1;
 const int SERIAL_DATA_BODY = 2;
 
+class _SerialPortEventArg
+        : public IEventArg
+{
+public:
+        _SerialPortEventArg(ByteBuffer* pBuffer)
+                : __pBuffer(pBuffer)
+        {
+        }
+       ByteBuffer* __pBuffer;
+};
+
+class _SerialPortEvent
+        : public Event
+{
+protected:
+        virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
+        {
+                ISerialPortEventListener* pListener = dynamic_cast<ISerialPortEventListener*> (&listener);
+                if (pListener != null)
+                {
+                        const _SerialPortEventArg* pArg = dynamic_cast<const _SerialPortEventArg*>(&arg);
+                        if (pArg != null)
+                        {
+                               if (pArg->__pBuffer == null)
+                               {
+                                       pListener->OnSerialPortErrorOccured(E_SYSTEM);
+                               }
+                               else
+                               {
+                                       pListener->OnSerialPortDataReceivedN(*(pArg->__pBuffer));
+                               }
+                        }
+                }
+        }
+};
+
 _SerialPortImpl::_SerialPortImpl(ISerialPortEventListener& listener)
        : __pIpcClient(null)
 _SerialPortImpl::_SerialPortImpl(ISerialPortEventListener& listener)
        : __pIpcClient(null)
-       , __pEventListener(null)
+       , __pEvent(null)
 {
        result r = E_SUCCESS;
        ArrayList request;
 {
        result r = E_SUCCESS;
        ArrayList request;
@@ -62,13 +99,17 @@ _SerialPortImpl::_SerialPortImpl(ISerialPortEventListener& listener)
        SysTryReturnVoidResult(NID_IO, __pIpcClient != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
        r = __pIpcClient->Construct(COMMUNICATION_DISPATCHER_IPC_ID, this);
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturnVoidResult(NID_IO, __pIpcClient != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
        r = __pIpcClient->Construct(COMMUNICATION_DISPATCHER_IPC_ID, this);
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
-       __pEventListener = &listener;
+
+       __pEvent = new (std::nothrow) _SerialPortEvent();
+       SysTryReturnVoidResult(NID_IO, __pEvent != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
+       __pEvent->AddListener(listener);
 
        r = request.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
 
        r = request.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(ACCESSORY_MANAGER_SERVICE_ID);
+       r = request.Add(String(ACCESSORY_MANAGER_SERVICE_ID));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(SERIAL_COMMAND_OPEN);
+       r = request.Add(String(SERIAL_COMMAND_OPEN));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        r = response.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        r = response.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
@@ -92,9 +133,9 @@ _SerialPortImpl::~_SerialPortImpl(void)
 
        r = request.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
 
        r = request.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(ACCESSORY_MANAGER_SERVICE_ID);
+       r = request.Add(String(ACCESSORY_MANAGER_SERVICE_ID));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(SERIAL_COMMAND_CLOSE);
+       r = request.Add(String(SERIAL_COMMAND_CLOSE));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        r = response.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
        r = response.Construct();
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
@@ -105,6 +146,8 @@ _SerialPortImpl::~_SerialPortImpl(void)
        r = __pIpcClient->SendRequest(pMsg.get());
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
 
        r = __pIpcClient->SendRequest(pMsg.get());
        SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
 
+       delete __pEvent;
+
        SetLastResult(E_SUCCESS);
 }
 
        SetLastResult(E_SUCCESS);
 }
 
@@ -124,9 +167,9 @@ _SerialPortImpl::Write(const Tizen::Base::ByteBuffer &byteBuffer)
 
        r = request.Construct();
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
 
        r = request.Construct();
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(ACCESSORY_MANAGER_SERVICE_ID);
+       r = request.Add(String(ACCESSORY_MANAGER_SERVICE_ID));
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
-       r = request.Add(SERIAL_COMMAND_WRITE);
+       r = request.Add(String(SERIAL_COMMAND_WRITE));
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
        r = request.Add(*pByteBuffer);
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
        r = request.Add(*pByteBuffer);
        SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
@@ -166,18 +209,25 @@ _SerialPortImpl::OnDataReceived(const ArrayList& data)
 
        if (pCommand->Equals(SERIAL_COMMAND_DATA, true))
        {
 
        if (pCommand->Equals(SERIAL_COMMAND_DATA, true))
        {
-               if (__pEventListener != null)
+               pBuffer = (ByteBuffer*)data.GetAt(SERIAL_DATA_BODY);
+               if (pBuffer != null)
                {
                {
-                       pBuffer = (ByteBuffer*)data.GetAt(SERIAL_DATA_BODY);
-                       __pEventListener->OnSerialPortDataReceivedN(*pBuffer);
+                       _SerialPortEventArg* pEventArg= new (std::nothrow) _SerialPortEventArg(pBuffer);
+                       if(pEventArg != null)
+                       {
+                               SysLog(NID_IO, "_SerialPortImpl::OnDataReceived Event Fire");
+                               __pEvent->Fire(*pEventArg);
+                       }
                }
        }
        else if (pCommand->Equals(SERIAL_COMMAND_ERROR, true))
        {
                }
        }
        else if (pCommand->Equals(SERIAL_COMMAND_ERROR, true))
        {
-               if (__pEventListener != null)
-               {
-                       __pEventListener->OnSerialPortErrorOccured(E_SYSTEM);
-               }
+               _SerialPortEventArg* pEventArg= new (std::nothrow) _SerialPortEventArg(pBuffer);
+                 if(pEventArg != null)
+                 {
+                       SysLog(NID_IO, "_SerialPortImpl::OnDataReceived Event Fire");
+                       __pEvent->Fire(*pEventArg);
+                 }
        }
 }
 }} // Tizen::Io
        }
 }
 }} // Tizen::Io
index 5badd89..d3e690e 100644 (file)
@@ -25,6 +25,7 @@
 #ifndef _FIO_INTERNAL_SERIALPORT_IMPL_H_
 #define _FIO_INTERNAL_SERIALPORT_IMPL_H_
 
 #ifndef _FIO_INTERNAL_SERIALPORT_IMPL_H_
 #define _FIO_INTERNAL_SERIALPORT_IMPL_H_
 
+#include <FBaseRtEvent.h>
 #include <FIo.h>
 #include <FIoISerialPortEventListener.h>
 #include <FIo_IIpcClientEventListener.h>
 #include <FIo.h>
 #include <FIoISerialPortEventListener.h>
 #include <FIo_IIpcClientEventListener.h>
@@ -51,7 +52,7 @@ public:
 
 private:
        _IpcClient*                     __pIpcClient;
 
 private:
        _IpcClient*                     __pIpcClient;
-       ISerialPortEventListener*       __pEventListener;
+       Tizen::Base::Runtime::Event*    __pEvent;
 
        _SerialPortImpl(_SerialPortImpl& serialPortImpl);
 
 
        _SerialPortImpl(_SerialPortImpl& serialPortImpl);
 
index 1e87910..241a849 100644 (file)
@@ -30,6 +30,7 @@
 #include <FBaseObject.h>
 #include <FBaseColHashMap.h>
 #include <FOspConfig.h>
 #include <FBaseObject.h>
 #include <FBaseColHashMap.h>
 #include <FOspConfig.h>
+#include <FBaseRtEvent.h>
 
 namespace Tizen { namespace Base
 {
 
 namespace Tizen { namespace Base
 {
@@ -73,7 +74,7 @@ private:
        static gboolean OnFileEventOccured(GIOChannel* source, GIOCondition condition, gpointer data);
 
        Tizen::Base::Collection::HashMap* __pMonitorFileList;
        static gboolean OnFileEventOccured(GIOChannel* source, GIOCondition condition, gpointer data);
 
        Tizen::Base::Collection::HashMap* __pMonitorFileList;
-       IFileEventListener* __pFileEventListener;
+       Tizen::Base::Runtime::Event* __pEvent;
        int __inotifyFd;
        GSource* __pGSource;
        GIOChannel* __pGIOChannel;
        int __inotifyFd;
        GSource* __pGSource;
        GIOChannel* __pGIOChannel;