/fbus/demo/fbus_echo.c

http://ftk.googlecode.com/ · C · 148 lines · 93 code · 26 blank · 29 comment · 35 complexity · 4da7f37570f4b8a01d26f07cf4554fe4 MD5 · raw file

  1. /*
  2. * File: fbus_echo.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: echo client api.
  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_echo.h"
  31. #include "fbus_echo_common.h"
  32. FBusEcho* fbus_echo_create()
  33. {
  34. return fbus_proxy_create(FBUS_SERVICE_ECHO);
  35. }
  36. Ret fbus_echo_char(FBusEcho* thiz, char in, char* out)
  37. {
  38. Ret ret = RET_FAIL;
  39. FBusParcel* parcel = fbus_proxy_get_parcel(thiz);
  40. return_val_if_fail(parcel != NULL && out != NULL, RET_FAIL);
  41. fbus_parcel_write_int(parcel, FBUS_ECHO_CHAR);
  42. fbus_parcel_write_char(parcel, in);
  43. if((ret = fbus_proxy_request(thiz, parcel)) == RET_OK)
  44. {
  45. if((ret = fbus_parcel_get_int(parcel)) == RET_OK)
  46. {
  47. *out = fbus_parcel_get_char(parcel);
  48. }
  49. }
  50. return ret;
  51. }
  52. Ret fbus_echo_short(FBusEcho* thiz, short in, short* out)
  53. {
  54. Ret ret = RET_FAIL;
  55. FBusParcel* parcel = fbus_proxy_get_parcel(thiz);
  56. return_val_if_fail(parcel != NULL && out != NULL, RET_FAIL);
  57. fbus_parcel_write_int(parcel, FBUS_ECHO_SHORT);
  58. fbus_parcel_write_short(parcel, in);
  59. if((ret = fbus_proxy_request(thiz, parcel)) == RET_OK)
  60. {
  61. if((ret = fbus_parcel_get_int(parcel)) == RET_OK)
  62. {
  63. *out = fbus_parcel_get_short(parcel);
  64. }
  65. }
  66. return ret;
  67. }
  68. Ret fbus_echo_int(FBusEcho* thiz, int in, int* out)
  69. {
  70. Ret ret = RET_FAIL;
  71. FBusParcel* parcel = fbus_proxy_get_parcel(thiz);
  72. return_val_if_fail(parcel != NULL && out != NULL, RET_FAIL);
  73. fbus_parcel_write_int(parcel, FBUS_ECHO_INT);
  74. fbus_parcel_write_int(parcel, in);
  75. if((ret = fbus_proxy_request(thiz, parcel)) == RET_OK)
  76. {
  77. if((ret = fbus_parcel_get_int(parcel)) == RET_OK)
  78. {
  79. *out = fbus_parcel_get_int(parcel);
  80. }
  81. }
  82. return ret;
  83. }
  84. Ret fbus_echo_string(FBusEcho* thiz, const char* in, const char** out)
  85. {
  86. Ret ret = RET_FAIL;
  87. FBusParcel* parcel = fbus_proxy_get_parcel(thiz);
  88. return_val_if_fail(parcel != NULL && out != NULL, RET_FAIL);
  89. fbus_parcel_write_int(parcel, FBUS_ECHO_STRING);
  90. fbus_parcel_write_string(parcel, in);
  91. if((ret = fbus_proxy_request(thiz, parcel)) == RET_OK)
  92. {
  93. if((ret = fbus_parcel_get_int(parcel)) == RET_OK)
  94. {
  95. *out = fbus_parcel_get_string(parcel);
  96. }
  97. }
  98. return ret;
  99. }
  100. Ret fbus_echo_data(FBusEcho* thiz, const char* in, size_t in_len, const char** out, size_t* out_len)
  101. {
  102. Ret ret = RET_FAIL;
  103. FBusParcel* parcel = fbus_proxy_get_parcel(thiz);
  104. return_val_if_fail(parcel != NULL && out != NULL, RET_FAIL);
  105. fbus_parcel_write_int(parcel, FBUS_ECHO_BINARY);
  106. fbus_parcel_write_int(parcel, in_len);
  107. fbus_parcel_write_data(parcel, in, in_len);
  108. if((ret = fbus_proxy_request(thiz, parcel)) == RET_OK)
  109. {
  110. if((ret = fbus_parcel_get_int(parcel)) == RET_OK)
  111. {
  112. *out_len = fbus_parcel_get_data_size(parcel);
  113. *out = fbus_parcel_get_data(parcel, in_len);
  114. }
  115. }
  116. return ret;
  117. }
  118. void fbus_echo_destroy(FBusEcho* thiz)
  119. {
  120. fbus_proxy_destroy(thiz);
  121. return;
  122. }