2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ecore-server-connection.h"
24 #include <dali/integration-api/debug.h>
30 // Copied from ecore_evas_extn_engine.h
31 // procotol version - change this as needed
32 const int MAJOR( 0x2011 );
42 #if defined(DEBUG_ENABLED)
43 extern Debug::Filter* gIndicatorLogFilter;
46 ServerConnection::ServerConnection(
47 const char* serviceName,
50 ServerConnection::Observer* observer)
55 Ecore_Ipc_Type ipctype = ECORE_IPC_LOCAL_USER;
58 mService.name = eina_stringshare_add(serviceName);
59 mService.num = serviceNumber;
60 mService.isSystem = isSystem;
62 if( mService.isSystem )
64 ipctype = ECORE_IPC_LOCAL_SYSTEM;
67 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: Connecting to %s %d\n", mService.name, mService.num );
69 mIpcServer = ecore_ipc_server_connect( ipctype, (char *)mService.name, mService.num, this );
73 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "mIpcServer is null\n" );
78 mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_ADD,
79 &ServerConnection::IpcServerAdd,
82 mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DEL,
83 &ServerConnection::IpcServerDel,
86 mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DATA,
87 &ServerConnection::IpcServerData,
94 ServerConnection::~ServerConnection()
98 if( mService.name != NULL )
100 eina_stringshare_del(mService.name);
103 for( Handlers::iterator iter = mIpcHandlers.begin(); iter != mIpcHandlers.end(); ++iter )
105 ecore_event_handler_del(*iter);
107 mIpcHandlers.clear();
110 bool ServerConnection::IsConnected()
115 void ServerConnection::OnDisconnect()
119 ecore_ipc_shutdown();
122 mObserver->ConnectionClosed();
126 bool ServerConnection::SendEvent( int event, const void *data, int size )
128 return SendEvent(event, 0, 0, data, size);
131 bool ServerConnection::SendEvent( int event, int ref, int ref_to, const void *data, int size )
133 if( mIpcServer != NULL && ecore_ipc_server_send(mIpcServer, MAJOR, event, ref, ref_to, 0, data, size) )
143 Eina_Bool ServerConnection::IpcServerAdd( void *data, int /*type*/, void *event )
145 DALI_LOG_INFO(gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerAdd\n" );
147 return ECORE_CALLBACK_PASS_ON;
150 Eina_Bool ServerConnection::IpcServerDel( void *data, int /*type*/, void *event )
152 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerDel\n" );
154 Ecore_Ipc_Event_Server_Del *e = static_cast<Ecore_Ipc_Event_Server_Del *>( event );
155 ServerConnection* connection = static_cast<ServerConnection*>( data );
157 if( connection != NULL )
159 if( connection->mIpcServer == e->server)
161 // No longer have a server connection
162 connection->OnDisconnect();
166 return ECORE_CALLBACK_PASS_ON;
169 Eina_Bool ServerConnection::IpcServerData( void *data, int /*type*/, void *event )
171 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: IpcServerData\n" );
173 Ecore_Ipc_Event_Server_Data *e = static_cast<Ecore_Ipc_Event_Server_Data *>( event );
174 ServerConnection* connection = static_cast<ServerConnection*>( data );
176 if( connection != NULL )
178 if( connection != ecore_ipc_server_data_get( e->server ) )
180 return ECORE_CALLBACK_PASS_ON;
183 if( e->major != MAJOR )
185 return ECORE_CALLBACK_PASS_ON;
188 if( connection->mObserver )
190 connection->mObserver->DataReceived( event );
193 return ECORE_CALLBACK_PASS_ON;
196 void ServerConnection::CloseConnection()
200 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "ServerConnection: CloseConnection\n" );
204 ecore_ipc_server_del( mIpcServer );
208 ecore_ipc_shutdown();