/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. * History:
  26. * ================================================================
  27. * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "fbus_source_proxy.h"
  31. typedef struct _PrivInfo
  32. {
  33. FBusProxy* proxy;
  34. FBusStream* stream;
  35. FBusProxyListener listener;
  36. void* listener_ctx;
  37. }PrivInfo;
  38. static int fbus_source_proxy_get_fd(FtkSource* thiz)
  39. {
  40. DECL_PRIV(thiz, priv);
  41. return fbus_stream_get_fd(priv->stream);
  42. }
  43. static int fbus_source_proxy_check(FtkSource* thiz)
  44. {
  45. return -1;
  46. }
  47. static Ret fbus_source_proxy_dispatch(FtkSource* thiz)
  48. {
  49. int type = 0;
  50. int size = 0;
  51. int trigger = 0;
  52. int ret = RET_FAIL;
  53. FBusParcel* parcel = NULL;
  54. DECL_PRIV(thiz, priv);
  55. parcel = fbus_proxy_get_parcel(priv->proxy);
  56. return_val_if_fail(parcel != NULL, RET_REMOVE);
  57. ret = fbus_stream_read_n(priv->stream, (char*)&type, sizeof(type));
  58. return_val_if_fail(ret == sizeof(type) && type == FBUS_RESP_PUSH, RET_FAIL);
  59. ret = fbus_stream_read_n(priv->stream, (char*)&trigger, sizeof(trigger));
  60. return_val_if_fail(ret == sizeof(trigger), RET_FAIL);
  61. ret = fbus_stream_read_n(priv->stream, (char*)&size, sizeof(size));
  62. return_val_if_fail(ret == sizeof(size), RET_REMOVE);
  63. ret = fbus_parcel_extend(parcel, size);
  64. return_val_if_fail(ret == RET_OK, RET_REMOVE);
  65. fbus_parcel_set_size(parcel, size);
  66. ret = fbus_stream_read_n(priv->stream, fbus_parcel_data(parcel), size);
  67. priv->listener(priv->listener_ctx, trigger, parcel);
  68. return RET_OK;
  69. }
  70. static void fbus_source_proxy_destroy(FtkSource* thiz)
  71. {
  72. if(thiz != NULL)
  73. {
  74. DECL_PRIV(thiz, priv);
  75. fbus_proxy_set_notify_listener(priv->proxy, NULL, NULL);
  76. FTK_FREE(thiz);
  77. }
  78. return;
  79. }
  80. FtkSource* fbus_source_proxy_create(FBusProxy* proxy, FBusStream* stream, FBusProxyListener listener, void* ctx)
  81. {
  82. FtkSource* thiz = NULL;
  83. return_val_if_fail(proxy != NULL && stream != NULL && listener != NULL, NULL);
  84. thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  85. if(thiz != NULL)
  86. {
  87. DECL_PRIV(thiz, priv);
  88. thiz->check = fbus_source_proxy_check;
  89. thiz->get_fd = fbus_source_proxy_get_fd;
  90. thiz->destroy = fbus_source_proxy_destroy;
  91. thiz->dispatch = fbus_source_proxy_dispatch;
  92. thiz->ref = 1;
  93. priv->proxy = proxy;
  94. priv->stream = stream;
  95. priv->listener = listener;
  96. priv->listener_ctx = ctx;
  97. }
  98. return thiz;
  99. }