PageRenderTime 39ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/fbus/demo/fbus_service_echo.c

http://ftk.googlecode.com/
C | 149 lines | 100 code | 19 blank | 30 comment | 6 complexity | b0068649fc65801a0d435bc953a3e675 MD5 | raw file
  1/*
  2 * File:    fbus_service_echo.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   echo service.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "fbus_echo_common.h"
 34#include "fbus_service.h"
 35
 36static const char* fbus_service_echo_get_name(FBusService* thiz)
 37{
 38	return FBUS_SERVICE_ECHO;
 39}
 40
 41static Ret fbus_service_echo_on_client_connect(FBusService* thiz, int client_id)
 42{
 43	ftk_logd("%s: client_id=%d\n", __func__, client_id);
 44	
 45	return RET_OK;
 46}
 47
 48static Ret fbus_service_echo_on_client_disconnect(FBusService* thiz, int client_id)
 49{
 50	ftk_logd("%s: client_id=%d\n", __func__, client_id);
 51	
 52	return RET_OK;
 53}
 54
 55static Ret fbus_service_echo_handle_request(FBusService* thiz, int client_id, FBusParcel* req_resp)
 56{
 57	int req_code = fbus_parcel_get_int(req_resp);
 58
 59	switch(req_code)
 60	{
 61		case FBUS_ECHO_CHAR:
 62		{
 63			char data = fbus_parcel_get_char(req_resp);
 64			fbus_parcel_reset(req_resp);
 65			fbus_parcel_write_int(req_resp, RET_OK);
 66			fbus_parcel_write_char(req_resp, data);
 67			break;
 68		}
 69		case FBUS_ECHO_SHORT:
 70		{
 71			short data = fbus_parcel_get_short(req_resp);
 72			fbus_parcel_reset(req_resp);
 73			fbus_parcel_write_int(req_resp, RET_OK);
 74			fbus_parcel_write_short(req_resp, data);
 75			break;
 76		}
 77		case FBUS_ECHO_INT:
 78		{
 79			int data = fbus_parcel_get_int(req_resp);
 80			fbus_parcel_reset(req_resp);
 81			fbus_parcel_write_int(req_resp, RET_OK);
 82			fbus_parcel_write_int(req_resp, data);
 83			break;
 84		}
 85		case FBUS_ECHO_STRING:
 86		{
 87			const char* data = fbus_parcel_get_string(req_resp);
 88			fbus_parcel_reset(req_resp);
 89			fbus_parcel_write_int(req_resp, RET_OK);
 90			fbus_parcel_write_string(req_resp, data);
 91
 92			break;
 93		}
 94		case FBUS_ECHO_BINARY:
 95		{
 96			int len = fbus_parcel_get_int(req_resp);
 97			const char* data = fbus_parcel_get_data(req_resp, len);
 98			fbus_parcel_reset(req_resp);
 99			fbus_parcel_write_int(req_resp, RET_OK);
100			fbus_parcel_write_data(req_resp, data, len);
101			break;
102		}
103	}
104	//ftk_logd("%s: client_id=%d\n", __func__, client_id);
105
106	return RET_OK;	
107}
108
109static void fbus_service_echo_destroy(FBusService* thiz)
110{
111	if(thiz != NULL)
112	{
113		FTK_FREE(thiz);
114	}
115}
116
117static FBusService* fbus_service_echo_create(void)
118{
119	FBusService* thiz = FTK_ZALLOC(sizeof(FBusService));
120
121	if(thiz != NULL)
122	{
123		thiz->get_name = fbus_service_echo_get_name;
124		thiz->on_client_connect = fbus_service_echo_on_client_connect;
125		thiz->on_client_disconnect = fbus_service_echo_on_client_disconnect;
126		thiz->handle_request = fbus_service_echo_handle_request;
127		thiz->destroy = fbus_service_echo_destroy;
128
129		fbus_service_register(thiz);
130	}
131
132	return thiz;
133}
134
135int main(int argc, char* argv[])
136{
137	fbus_service_init(argc, argv);
138	
139	if(fbus_service_echo_create() != NULL)
140	{
141		fbus_service_run();
142	}
143	else
144	{
145		ftk_loge("Create service echo failed.\n");
146	}
147
148	return 0;
149}