/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. * History:
  26. * ================================================================
  27. * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "fbus_typedef.h"
  31. #include "fbus_source_service.h"
  32. typedef struct _PrivInfo
  33. {
  34. FBusStream* stream;
  35. FBusParcel* parcel;
  36. FBusService* service;
  37. }PrivInfo;
  38. static int fbus_source_service_get_fd(FtkSource* thiz)
  39. {
  40. DECL_PRIV(thiz, priv);
  41. return fbus_stream_get_fd(priv->stream);
  42. }
  43. static int fbus_source_service_check(FtkSource* thiz)
  44. {
  45. return -1;
  46. }
  47. Ret fbus_source_service_dispatch(FtkSource* thiz)
  48. {
  49. int ret = 0;
  50. int size = 0;
  51. int type = FBUS_RESP_NORMAL;
  52. DECL_PRIV(thiz, priv);
  53. fbus_parcel_reset(priv->parcel);
  54. ret = fbus_stream_read_n(priv->stream, (char*)&size, sizeof(size));
  55. if(size <= 0)
  56. {
  57. return RET_REMOVE;
  58. }
  59. return_val_if_fail(ret == sizeof(size), RET_REMOVE);
  60. fbus_parcel_extend(priv->parcel, size);
  61. fbus_parcel_set_size(priv->parcel, size);
  62. ret = fbus_stream_read_n(priv->stream, fbus_parcel_data(priv->parcel), size);
  63. ret = fbus_service_handle_request(priv->service, fbus_stream_get_fd(priv->stream), priv->parcel);
  64. if(ret != RET_OK)
  65. {
  66. fbus_parcel_reset(priv->parcel);
  67. ret = fbus_parcel_write_int(priv->parcel, ret);
  68. }
  69. ret = fbus_stream_write_n(priv->stream, (const char*)&type, sizeof(type));
  70. size = fbus_parcel_size(priv->parcel);
  71. ret = fbus_stream_write_n(priv->stream, (const char*)&size, sizeof(size));
  72. ret = fbus_stream_write_n(priv->stream, fbus_parcel_data(priv->parcel), size);
  73. return ret == size ? RET_OK : RET_FAIL;
  74. }
  75. static void fbus_source_service_destroy(FtkSource* thiz)
  76. {
  77. if(thiz != NULL)
  78. {
  79. DECL_PRIV(thiz, priv);
  80. fbus_service_on_client_disconnect(priv->service, priv->stream);
  81. fbus_stream_destroy(priv->stream);
  82. FTK_FREE(thiz);
  83. }
  84. return;
  85. }
  86. FtkSource* fbus_source_service_create(FBusService* service, FBusStream* stream)
  87. {
  88. FtkSource* thiz = NULL;
  89. return_val_if_fail(service != NULL && stream != NULL, NULL);
  90. thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  91. if(thiz != NULL)
  92. {
  93. DECL_PRIV(thiz, priv);
  94. thiz->ref = 1;
  95. priv->stream = stream;
  96. priv->service = service;
  97. priv->parcel = fbus_parcel_create(256);
  98. thiz->get_fd = fbus_source_service_get_fd;
  99. thiz->check = fbus_source_service_check;
  100. thiz->dispatch = fbus_source_service_dispatch;
  101. thiz->destroy = fbus_source_service_destroy;
  102. fbus_service_on_client_connect(service, stream);
  103. }
  104. return thiz;
  105. }