Guest User

Untitled

a guest
Sep 25th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. ##### C Code of the socket client
  2.  
  3. #define BUFFERSIZE 1024
  4. #define MAXDATASIZE 256
  5. int c, q = 0, success;
  6. char* input_buffer;
  7. char data_buffer[MAXDATASIZE];
  8. size_t input_buffer_size = BUFFERSIZE;
  9. input_buffer = malloc(input_buffer_size);
  10.  
  11. if (input_buffer)
  12. {
  13. while((c = read(fd, data_buffer, MAXDATASIZE)) > 0)
  14. {
  15. if (c + q > input_buffer_size)
  16. {
  17. input_buffer_size *= 2; /* Arbitrary doubling of size. */
  18. char* tmp = realloc(input_buffer, input_buffer_size);
  19. if (tmp)
  20. {
  21. input_buffer = tmp;
  22. }
  23. else
  24. {
  25. /* memory allocation failure. */
  26. free(input_buffer);
  27. input_buffer = 0;
  28. break;
  29. }
  30. }
  31. memcpy(input_buffer + q, data_buffer, c);
  32. q += c;
  33. }
  34. }
  35. json_error_t error;
  36. json_t *root = json_loads( input_buffer, 0, &error );
  37. if (root) {
  38. json_t *json_status = json_object_get( root, "status" );
  39. json_t *json_command = json_object_get( root, "command" );
  40. const char *return_comamnd = json_string_value( json_command );
  41. success = (int)json_integer_value(json_status);
  42. logit("json_response: status: %i command: %s",success, return_comamnd);
  43. }
  44.  
  45.  
  46. close(fd);
  47.  
  48.  
  49. ##### Ruby Code of the socket server
  50. require "socket"
  51. require 'msgpack'
  52. require "json"
  53.  
  54. file = "/tmp/auth.sock"
  55.  
  56. File.unlink if File.exists?(file) && File.socket?(file)
  57.  
  58. serv = UNIXServer.new(file)
  59.  
  60. loop do
  61. s = serv.accept
  62. msg = s.gets
  63. msg = msg.gsub("\n", "") if msg
  64. p JSON.parse(msg)
  65. a = {}
  66. a["status"] = 1
  67. a["command"] = "hello world"
  68. s.write(JSON.generate(a))
  69. s.close
  70. end
Advertisement
Add Comment
Please, Sign In to add comment