PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/fbus/fbus_source_service.c

http://ftk.googlecode.com/
C | 125 lines | 76 code | 20 blank | 29 comment | 12 complexity | 3d4579c0082e5f47a5c614a5361afe92 MD5 | raw file
  1/*
  2 * File:   fbus_source_service.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   a source to handle service request.
  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 "fbus_typedef.h"
 33#include "fbus_source_service.h"
 34
 35typedef struct _PrivInfo
 36{
 37	FBusStream*  stream;
 38	FBusParcel*  parcel;
 39	FBusService* service;
 40}PrivInfo;
 41
 42static int fbus_source_service_get_fd(FtkSource* thiz)
 43{
 44	DECL_PRIV(thiz, priv);
 45	
 46	return fbus_stream_get_fd(priv->stream);
 47}
 48
 49static int fbus_source_service_check(FtkSource* thiz)
 50{
 51	return -1;
 52}
 53
 54Ret fbus_source_service_dispatch(FtkSource* thiz)
 55{
 56	int ret = 0;
 57	int size = 0;
 58	int type = FBUS_RESP_NORMAL;
 59	DECL_PRIV(thiz, priv);
 60
 61	fbus_parcel_reset(priv->parcel);
 62	ret = fbus_stream_read_n(priv->stream, (char*)&size, sizeof(size));
 63	if(size <= 0)
 64	{
 65		return RET_REMOVE;
 66	}
 67	return_val_if_fail(ret == sizeof(size), RET_REMOVE);
 68
 69	fbus_parcel_extend(priv->parcel, size);
 70	fbus_parcel_set_size(priv->parcel, size);
 71	ret = fbus_stream_read_n(priv->stream, fbus_parcel_data(priv->parcel), size);
 72
 73	ret = fbus_service_handle_request(priv->service, fbus_stream_get_fd(priv->stream), priv->parcel);
 74	if(ret != RET_OK)
 75	{
 76		fbus_parcel_reset(priv->parcel);
 77		ret = fbus_parcel_write_int(priv->parcel, ret);
 78	}
 79	
 80	ret = fbus_stream_write_n(priv->stream, (const char*)&type, sizeof(type));
 81	size = fbus_parcel_size(priv->parcel);
 82	ret = fbus_stream_write_n(priv->stream, (const char*)&size, sizeof(size));
 83	ret = fbus_stream_write_n(priv->stream, fbus_parcel_data(priv->parcel), size);
 84
 85	return ret == size ? RET_OK : RET_FAIL;
 86}
 87
 88static void fbus_source_service_destroy(FtkSource* thiz)
 89{
 90	if(thiz != NULL)
 91	{
 92		DECL_PRIV(thiz, priv);
 93		fbus_service_on_client_disconnect(priv->service, priv->stream);
 94		fbus_stream_destroy(priv->stream);
 95		FTK_FREE(thiz);
 96	}
 97
 98	return;
 99}
100
101FtkSource* fbus_source_service_create(FBusService* service, FBusStream* stream)
102{
103	FtkSource* thiz = NULL;
104	return_val_if_fail(service != NULL && stream != NULL, NULL);
105
106	thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
107	if(thiz != NULL)
108	{
109		DECL_PRIV(thiz, priv);
110
111		thiz->ref = 1;
112		priv->stream   = stream;
113		priv->service  = service;
114		priv->parcel   = fbus_parcel_create(256); 
115		thiz->get_fd   = fbus_source_service_get_fd;
116		thiz->check    = fbus_source_service_check;
117		thiz->dispatch = fbus_source_service_dispatch;
118		thiz->destroy  = fbus_source_service_destroy;
119
120		fbus_service_on_client_connect(service, stream);
121	}
122
123	return thiz;
124}
125