PageRenderTime 405ms CodeModel.GetById 153ms app.highlight 8ms RepoModel.GetById 111ms app.codeStats 1ms

/fbus/fbus_source_proxy.c

http://ftk.googlecode.com/
C | 122 lines | 70 code | 23 blank | 29 comment | 16 complexity | d38a57a452e17200e372976fde2c5e38 MD5 | raw file
  1/*
  2 * File:    fbus_source_proxy.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   a source to handle service push event.
  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_source_proxy.h"
 33
 34typedef struct _PrivInfo
 35{
 36	FBusProxy*   proxy;
 37	FBusStream*  stream;
 38	FBusProxyListener listener;
 39	void* listener_ctx;
 40}PrivInfo;
 41
 42static int fbus_source_proxy_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_proxy_check(FtkSource* thiz)
 50{
 51	return -1;
 52}
 53
 54static Ret fbus_source_proxy_dispatch(FtkSource* thiz)
 55{
 56	int type = 0;
 57	int size = 0;
 58	int trigger = 0;
 59	int ret = RET_FAIL;
 60	FBusParcel* parcel = NULL;
 61	DECL_PRIV(thiz, priv);
 62	
 63	parcel = fbus_proxy_get_parcel(priv->proxy);
 64	return_val_if_fail(parcel != NULL, RET_REMOVE);
 65
 66	ret = fbus_stream_read_n(priv->stream, (char*)&type, sizeof(type));
 67	return_val_if_fail(ret == sizeof(type) && type == FBUS_RESP_PUSH, RET_FAIL);
 68	
 69	ret = fbus_stream_read_n(priv->stream, (char*)&trigger, sizeof(trigger));
 70	return_val_if_fail(ret == sizeof(trigger), RET_FAIL);
 71	
 72	ret = fbus_stream_read_n(priv->stream, (char*)&size, sizeof(size));
 73	return_val_if_fail(ret == sizeof(size), RET_REMOVE);
 74
 75	ret = fbus_parcel_extend(parcel, size);
 76	return_val_if_fail(ret == RET_OK, RET_REMOVE);
 77	
 78	fbus_parcel_set_size(parcel, size);
 79	ret = fbus_stream_read_n(priv->stream, fbus_parcel_data(parcel), size);
 80
 81	priv->listener(priv->listener_ctx, trigger, parcel);
 82
 83	return RET_OK;
 84}
 85
 86static void fbus_source_proxy_destroy(FtkSource* thiz)
 87{
 88	if(thiz != NULL)
 89	{
 90		DECL_PRIV(thiz, priv);
 91		fbus_proxy_set_notify_listener(priv->proxy, NULL, NULL);
 92		FTK_FREE(thiz);
 93	}
 94
 95	return;
 96}
 97
 98FtkSource* fbus_source_proxy_create(FBusProxy* proxy, FBusStream* stream, FBusProxyListener listener, void* ctx)
 99{
100	FtkSource* thiz = NULL;
101	return_val_if_fail(proxy != NULL && stream != NULL && listener != NULL, NULL);
102
103	thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
104	if(thiz != NULL)
105	{
106		DECL_PRIV(thiz, priv);
107
108		thiz->check    = fbus_source_proxy_check;
109		thiz->get_fd   = fbus_source_proxy_get_fd;
110		thiz->destroy  = fbus_source_proxy_destroy;
111		thiz->dispatch = fbus_source_proxy_dispatch;
112
113		thiz->ref = 1;
114		priv->proxy = proxy;
115		priv->stream = stream;
116		priv->listener = listener;
117		priv->listener_ctx = ctx;
118	}
119
120	return thiz;
121}
122