client.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "internal.h"
  34. #include "cgrpc.h"
  35. #include <assert.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. cgrpc_client *cgrpc_client_create(const char *address) {
  40. cgrpc_client *c = (cgrpc_client *) malloc(sizeof (cgrpc_client));
  41. // create the client
  42. grpc_channel_args client_args;
  43. client_args.num_args = 0;
  44. c->client = grpc_insecure_channel_create(address, &client_args, NULL);
  45. c->completion_queue = grpc_completion_queue_create(NULL);
  46. return c;
  47. }
  48. void cgrpc_client_destroy(cgrpc_client *c) {
  49. grpc_channel_destroy(c->client);
  50. c->client = NULL;
  51. grpc_completion_queue_shutdown(c->completion_queue);
  52. cgrpc_completion_queue_drain(c->completion_queue);
  53. grpc_completion_queue_destroy(c->completion_queue);
  54. free(c);
  55. }
  56. cgrpc_call *cgrpc_client_create_call(cgrpc_client *client,
  57. const char *method,
  58. const char *host,
  59. double timeout) {
  60. // create call
  61. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  62. grpc_call *client_call = grpc_channel_create_call(client->client,
  63. NULL,
  64. GRPC_PROPAGATE_DEFAULTS,
  65. client->completion_queue,
  66. method,
  67. host,
  68. deadline,
  69. NULL);
  70. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  71. memset(call, 0, sizeof(cgrpc_call));
  72. call->call = client_call;
  73. return call;
  74. }
  75. cgrpc_completion_queue *cgrpc_client_completion_queue(cgrpc_client *client) {
  76. return client->completion_queue;
  77. }