/fbus/demo/fbus_service_echo.c

http://ftk.googlecode.com/ · C · 149 lines · 100 code · 19 blank · 30 comment · 7 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. * History:
  26. * ================================================================
  27. * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "fbus_echo_common.h"
  32. #include "fbus_service.h"
  33. static const char* fbus_service_echo_get_name(FBusService* thiz)
  34. {
  35. return FBUS_SERVICE_ECHO;
  36. }
  37. static Ret fbus_service_echo_on_client_connect(FBusService* thiz, int client_id)
  38. {
  39. ftk_logd("%s: client_id=%d\n", __func__, client_id);
  40. return RET_OK;
  41. }
  42. static Ret fbus_service_echo_on_client_disconnect(FBusService* thiz, int client_id)
  43. {
  44. ftk_logd("%s: client_id=%d\n", __func__, client_id);
  45. return RET_OK;
  46. }
  47. static Ret fbus_service_echo_handle_request(FBusService* thiz, int client_id, FBusParcel* req_resp)
  48. {
  49. int req_code = fbus_parcel_get_int(req_resp);
  50. switch(req_code)
  51. {
  52. case FBUS_ECHO_CHAR:
  53. {
  54. char data = fbus_parcel_get_char(req_resp);
  55. fbus_parcel_reset(req_resp);
  56. fbus_parcel_write_int(req_resp, RET_OK);
  57. fbus_parcel_write_char(req_resp, data);
  58. break;
  59. }
  60. case FBUS_ECHO_SHORT:
  61. {
  62. short data = fbus_parcel_get_short(req_resp);
  63. fbus_parcel_reset(req_resp);
  64. fbus_parcel_write_int(req_resp, RET_OK);
  65. fbus_parcel_write_short(req_resp, data);
  66. break;
  67. }
  68. case FBUS_ECHO_INT:
  69. {
  70. int data = fbus_parcel_get_int(req_resp);
  71. fbus_parcel_reset(req_resp);
  72. fbus_parcel_write_int(req_resp, RET_OK);
  73. fbus_parcel_write_int(req_resp, data);
  74. break;
  75. }
  76. case FBUS_ECHO_STRING:
  77. {
  78. const char* data = fbus_parcel_get_string(req_resp);
  79. fbus_parcel_reset(req_resp);
  80. fbus_parcel_write_int(req_resp, RET_OK);
  81. fbus_parcel_write_string(req_resp, data);
  82. break;
  83. }
  84. case FBUS_ECHO_BINARY:
  85. {
  86. int len = fbus_parcel_get_int(req_resp);
  87. const char* data = fbus_parcel_get_data(req_resp, len);
  88. fbus_parcel_reset(req_resp);
  89. fbus_parcel_write_int(req_resp, RET_OK);
  90. fbus_parcel_write_data(req_resp, data, len);
  91. break;
  92. }
  93. }
  94. //ftk_logd("%s: client_id=%d\n", __func__, client_id);
  95. return RET_OK;
  96. }
  97. static void fbus_service_echo_destroy(FBusService* thiz)
  98. {
  99. if(thiz != NULL)
  100. {
  101. FTK_FREE(thiz);
  102. }
  103. }
  104. static FBusService* fbus_service_echo_create(void)
  105. {
  106. FBusService* thiz = FTK_ZALLOC(sizeof(FBusService));
  107. if(thiz != NULL)
  108. {
  109. thiz->get_name = fbus_service_echo_get_name;
  110. thiz->on_client_connect = fbus_service_echo_on_client_connect;
  111. thiz->on_client_disconnect = fbus_service_echo_on_client_disconnect;
  112. thiz->handle_request = fbus_service_echo_handle_request;
  113. thiz->destroy = fbus_service_echo_destroy;
  114. fbus_service_register(thiz);
  115. }
  116. return thiz;
  117. }
  118. int main(int argc, char* argv[])
  119. {
  120. fbus_service_init(argc, argv);
  121. if(fbus_service_echo_create() != NULL)
  122. {
  123. fbus_service_run();
  124. }
  125. else
  126. {
  127. ftk_loge("Create service echo failed.\n");
  128. }
  129. return 0;
  130. }