5f7a841e6b9f1f9c971f3f67a8bd72b8cc1b2590
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / common / ecore-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 "ecore-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   const char*                 serviceName,
48   int                         serviceNumber,
49   bool                        isSystem,
50   ServerConnection::Observer* observer)
51
52 : mConnected(false),
53   mObserver(observer)
54 {
55   Ecore_Ipc_Type ipctype = ECORE_IPC_LOCAL_USER;
56
57   ecore_ipc_init();
58   mService.name = eina_stringshare_add(serviceName);
59   mService.num = serviceNumber;
60   mService.isSystem = isSystem;
61
62   if( mService.isSystem )
63   {
64     ipctype = ECORE_IPC_LOCAL_SYSTEM;
65   }
66
67   DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: Connecting to %s %d\n", mService.name, mService.num );
68
69   mIpcServer = ecore_ipc_server_connect( ipctype, (char *)mService.name, mService.num, this );
70
71   if( !mIpcServer )
72   {
73     DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "mIpcServer is null\n" );
74     ecore_ipc_shutdown();
75   }
76   else
77   {
78     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_ADD,
79                                                      &ServerConnection::IpcServerAdd,
80                                                      this ) );
81
82     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DEL,
83                                                      &ServerConnection::IpcServerDel,
84                                                      this ) );
85
86     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DATA,
87                                                      &ServerConnection::IpcServerData,
88                                                      this));
89
90     mConnected = true;
91   }
92 }
93
94 ServerConnection::~ServerConnection()
95 {
96   CloseConnection();
97
98   if( mService.name != NULL )
99   {
100     eina_stringshare_del(mService.name);
101   }
102
103   for( Handlers::iterator iter = mIpcHandlers.begin(); iter != mIpcHandlers.end(); ++iter )
104   {
105     ecore_event_handler_del(*iter);
106   }
107   mIpcHandlers.clear();
108 }
109
110 bool ServerConnection::IsConnected()
111 {
112   return mConnected;
113 }
114
115 void ServerConnection::OnDisconnect()
116 {
117   mConnected = false;
118   mIpcServer = NULL;
119   ecore_ipc_shutdown();
120   if( mObserver )
121   {
122     mObserver->ConnectionClosed();
123   }
124 }
125
126 bool ServerConnection::SendEvent( int event, const void *data, int size )
127 {
128   return SendEvent(event, 0, 0, data, size);
129 }
130
131 bool ServerConnection::SendEvent( int event, int ref, int ref_to, const void *data, int size )
132 {
133   if( mIpcServer != NULL  && ecore_ipc_server_send(mIpcServer, MAJOR, event, ref, ref_to, 0, data, size) )
134   {
135     return true;
136   }
137   else
138   {
139     return false;
140   }
141 }
142
143 Eina_Bool ServerConnection::IpcServerAdd( void *data, int /*type*/, void *event )
144 {
145   DALI_LOG_INFO(gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerAdd\n" );
146
147   return ECORE_CALLBACK_PASS_ON;
148 }
149
150 Eina_Bool ServerConnection::IpcServerDel( void *data, int /*type*/, void *event )
151 {
152   DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerDel\n" );
153
154   Ecore_Ipc_Event_Server_Del *e = static_cast<Ecore_Ipc_Event_Server_Del *>( event );
155   ServerConnection* connection = static_cast<ServerConnection*>( data );
156
157   if( connection != NULL )
158   {
159     if( connection->mIpcServer == e->server)
160     {
161       // No longer have a server connection
162       connection->OnDisconnect();
163     }
164   }
165
166   return ECORE_CALLBACK_PASS_ON;
167 }
168
169 Eina_Bool ServerConnection::IpcServerData( void *data, int /*type*/, void *event )
170 {
171   DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerData\n" );
172
173   Ecore_Ipc_Event_Server_Data *e = static_cast<Ecore_Ipc_Event_Server_Data *>( event );
174   ServerConnection* connection = static_cast<ServerConnection*>( data );
175
176   if( connection != NULL )
177   {
178     if( connection != ecore_ipc_server_data_get( e->server ) )
179     {
180       return ECORE_CALLBACK_PASS_ON;
181     }
182
183     if( e->major != MAJOR )
184     {
185       return ECORE_CALLBACK_PASS_ON;
186     }
187
188     if( connection->mObserver )
189     {
190       connection->mObserver->DataReceived( event );
191     }
192   }
193   return ECORE_CALLBACK_PASS_ON;
194 }
195
196 void ServerConnection::CloseConnection()
197 {
198   if( mConnected )
199   {
200     DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: CloseConnection\n" );
201
202     if( mIpcServer )
203     {
204       ecore_ipc_server_del( mIpcServer );
205       mIpcServer = NULL;
206     }
207
208     ecore_ipc_shutdown();
209     mConnected = false;
210   }
211 }
212
213 } // Adaptor
214 } // Internal
215 } // Dali