Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[platform/core/uifw/dali-adaptor.git] / adaptors / common / server-connection.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "server-connection.h"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore.h>
23
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27
28 namespace
29 {
30 // Copied from ecore_evas_extn_engine.h
31 // procotol version - change this as needed
32 const int MAJOR( 0x2011 );
33 }
34
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42 #if defined(DEBUG_ENABLED)
43 extern Debug::Filter* gIndicatorLogFilter;
44 #endif
45
46 ServerConnection::~ServerConnection()
47 {
48   CloseConnection();
49
50   if( mService.name != NULL )
51   {
52     eina_stringshare_del(mService.name);
53   }
54
55   for( Handlers::iterator iter = mIpcHandlers.begin(); iter != mIpcHandlers.end(); ++iter )
56   {
57     ecore_event_handler_del(*iter);
58   }
59   mIpcHandlers.clear();
60 }
61
62 bool ServerConnection::IsConnected()
63 {
64   return mConnected;
65 }
66
67 void ServerConnection::OnDisconnect()
68 {
69   mConnected = false;
70   mIpcServer = NULL;
71   ecore_ipc_shutdown();
72   if( mObserver )
73   {
74     mObserver->ConnectionClosed();
75   }
76 }
77
78 bool ServerConnection::SendEvent( int event, const void *data, int size )
79 {
80   return SendEvent(event, 0, 0, data, size);
81 }
82
83 bool ServerConnection::SendEvent( int event, int ref, int ref_to, const void *data, int size )
84 {
85   if( mIpcServer != NULL  && ecore_ipc_server_send(mIpcServer, MAJOR, event, ref, ref_to, 0, data, size) )
86   {
87     return true;
88   }
89   else
90   {
91     return false;
92   }
93 }
94
95 Eina_Bool ServerConnection::IpcServerAdd( void *data, int /*type*/, void *event )
96 {
97   DALI_LOG_INFO(gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerAdd\n" );
98
99   return ECORE_CALLBACK_PASS_ON;
100 }
101
102 Eina_Bool ServerConnection::IpcServerDel( void *data, int /*type*/, void *event )
103 {
104   DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerDel\n" );
105
106   Ecore_Ipc_Event_Server_Del *e = static_cast<Ecore_Ipc_Event_Server_Del *>( event );
107   ServerConnection* connection = static_cast<ServerConnection*>( data );
108
109   if( connection != NULL )
110   {
111     if( connection->mIpcServer == e->server)
112     {
113       // No longer have a server connection
114       connection->OnDisconnect();
115     }
116   }
117
118   return ECORE_CALLBACK_PASS_ON;
119 }
120
121 Eina_Bool ServerConnection::IpcServerData( void *data, int /*type*/, void *event )
122 {
123   DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerData\n" );
124
125   Ecore_Ipc_Event_Server_Data *e = static_cast<Ecore_Ipc_Event_Server_Data *>( event );
126   ServerConnection* connection = static_cast<ServerConnection*>( data );
127
128   if( connection != NULL )
129   {
130     if( connection != ecore_ipc_server_data_get( e->server ) )
131     {
132       return ECORE_CALLBACK_PASS_ON;
133     }
134
135     if( e->major != MAJOR )
136     {
137       return ECORE_CALLBACK_PASS_ON;
138     }
139
140     if( connection->mObserver )
141     {
142       connection->mObserver->DataReceived( event );
143     }
144   }
145   return ECORE_CALLBACK_PASS_ON;
146 }
147
148 void ServerConnection::CloseConnection()
149 {
150   if( mConnected )
151   {
152     DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: CloseConnection\n" );
153
154     if( mIpcServer )
155     {
156       ecore_ipc_server_del( mIpcServer );
157       mIpcServer = NULL;
158     }
159
160     ecore_ipc_shutdown();
161     mConnected = false;
162   }
163 }
164
165 } // Adaptor
166 } // Internal
167 } // Dali