コード例 #1
0
ファイル: grpc_csharp_ext.c プロジェクト: github188/grpc
GPR_EXPORT grpc_server *GPR_CALLTYPE
grpcsharp_server_create(grpc_completion_queue *cq,
                        const grpc_channel_args *args) {
  grpc_server *server = grpc_server_create(args, NULL);
  grpc_server_register_completion_queue(server, cq, NULL);
  return server;
}
コード例 #2
0
ファイル: cert.c プロジェクト: 201528013359030/grpc
int main(int argc, char **argv) {
  const char *addr = bad_ssl_addr(argc, argv);
  grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
  grpc_server_credentials *ssl_creds;
  grpc_server *server;
  gpr_slice cert_slice, key_slice;
  int ok;

  grpc_init();

  cert_slice =
      gpr_load_file("src/core/lib/tsi/test_creds/badserver.pem", 1, &ok);
  GPR_ASSERT(ok);
  key_slice =
      gpr_load_file("src/core/lib/tsi/test_creds/badserver.key", 1, &ok);
  GPR_ASSERT(ok);
  pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice);
  pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice);

  ssl_creds =
      grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1, 0, NULL);
  server = grpc_server_create(NULL, NULL);
  GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
  grpc_server_credentials_release(ssl_creds);

  gpr_slice_unref(cert_slice);
  gpr_slice_unref(key_slice);

  bad_ssl_run(server);
  grpc_shutdown();

  return 0;
}
コード例 #3
0
ファイル: server_test.c プロジェクト: 201528013359030/grpc
void test_register_method_fail(void) {
  grpc_server *server = grpc_server_create(NULL, NULL);
  void *method;
  void *method_old;
  method =
      grpc_server_register_method(server, NULL, NULL, GRPC_SRM_PAYLOAD_NONE, 0);
  GPR_ASSERT(method == NULL);
  method_old =
      grpc_server_register_method(server, "m", "h", GRPC_SRM_PAYLOAD_NONE, 0);
  GPR_ASSERT(method_old != NULL);
  method = grpc_server_register_method(
      server, "m", "h", GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER, 0);
  GPR_ASSERT(method == NULL);
  method_old =
      grpc_server_register_method(server, "m2", "h2", GRPC_SRM_PAYLOAD_NONE,
                                  GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST);
  GPR_ASSERT(method_old != NULL);
  method =
      grpc_server_register_method(server, "m2", "h2", GRPC_SRM_PAYLOAD_NONE, 0);
  GPR_ASSERT(method == NULL);
  method = grpc_server_register_method(
      server, "m2", "h2", GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER,
      GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST);
  GPR_ASSERT(method == NULL);
  grpc_server_destroy(server);
}
コード例 #4
0
int main(int argc, char **argv) {
  grpc_completion_queue *cq1;
  grpc_completion_queue *cq2;
  grpc_server *server;

  grpc_test_init(argc, argv);
  grpc_init();
  cq1 = grpc_completion_queue_create();
  cq2 = grpc_completion_queue_create();
  server = grpc_server_create(NULL);
  grpc_server_register_completion_queue(server, cq1);
  grpc_server_add_insecure_http2_port(server, "[::]:0");
  grpc_server_register_completion_queue(server, cq2);
  grpc_server_start(server);
  grpc_server_shutdown_and_notify(server, cq2, NULL);
  grpc_completion_queue_next(
      cq2, gpr_inf_future(GPR_CLOCK_REALTIME)); /* cue queue hang */
  grpc_completion_queue_shutdown(cq1);
  grpc_completion_queue_shutdown(cq2);
  grpc_completion_queue_next(cq1, gpr_inf_future(GPR_CLOCK_REALTIME));
  grpc_completion_queue_next(cq2, gpr_inf_future(GPR_CLOCK_REALTIME));
  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq1);
  grpc_completion_queue_destroy(cq2);
  grpc_shutdown();
  return 0;
}
コード例 #5
0
ファイル: rb_server.c プロジェクト: rwightman/grpc
/*
  call-seq:
    cq = CompletionQueue.new
    server = Server.new(cq, {'arg1': 'value1'})

  Initializes server instances. */
static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) {
    grpc_completion_queue *cq = NULL;
    grpc_rb_server *wrapper = NULL;
    grpc_server *srv = NULL;
    grpc_channel_args args;
    MEMZERO(&args, grpc_channel_args, 1);
    cq = grpc_rb_get_wrapped_completion_queue(cqueue);
    TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type,
                         wrapper);
    grpc_rb_hash_convert_to_channel_args(channel_args, &args);
    srv = grpc_server_create(&args, NULL);

    if (args.args != NULL) {
        xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */
    }
    if (srv == NULL) {
        rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why");
    }
    grpc_server_register_completion_queue(srv, cq, NULL);
    wrapper->wrapped = srv;

    /* Add the cq as the server's mark object. This ensures the ruby cq can't be
       GCed before the server */
    wrapper->mark = cqueue;
    return self;
}
コード例 #6
0
ファイル: rb_server.c プロジェクト: deepaklukose/grpc
/*
  call-seq:
    server = Server.new({'arg1': 'value1'})

  Initializes server instances. */
static VALUE grpc_rb_server_init(VALUE self, VALUE channel_args) {
  grpc_completion_queue* cq = NULL;
  grpc_rb_server* wrapper = NULL;
  grpc_server* srv = NULL;
  grpc_channel_args args;
  MEMZERO(&args, grpc_channel_args, 1);

  grpc_ruby_once_init();

  cq = grpc_completion_queue_create_for_pluck(NULL);
  TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type,
                       wrapper);
  grpc_rb_hash_convert_to_channel_args(channel_args, &args);
  srv = grpc_server_create(&args, NULL);

  if (args.args != NULL) {
    xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */
  }
  if (srv == NULL) {
    rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why");
  }
  grpc_server_register_completion_queue(srv, cq, NULL);
  wrapper->wrapped = srv;
  wrapper->queue = cq;

  return self;
}
コード例 #7
0
ファイル: h2_ssl_proxy.c プロジェクト: madongfly/grpc
static grpc_server *create_proxy_server(const char *port) {
  grpc_server *s = grpc_server_create(NULL, NULL);
  grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key,
                                                  test_server1_cert};
  grpc_server_credentials *ssl_creds =
      grpc_ssl_server_credentials_create(NULL, &pem_cert_key_pair, 1, 0, NULL);
  GPR_ASSERT(grpc_server_add_secure_http2_port(s, port, ssl_creds));
  grpc_server_credentials_release(ssl_creds);
  return s;
}
コード例 #8
0
static void prepare_test(int is_client) {
  int port;
  char *server_hostport;
  grpc_op *op;
  g_state.is_client = is_client;
  grpc_metadata_array_init(&g_state.initial_metadata_recv);
  grpc_metadata_array_init(&g_state.trailing_metadata_recv);
  g_state.deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2);
  g_state.cq = grpc_completion_queue_create(NULL);
  g_state.cqv = cq_verifier_create(g_state.cq);
  g_state.details = NULL;
  g_state.details_capacity = 0;

  if (is_client) {
    /* create a call, channel to a non existant server */
    g_state.chan =
        grpc_insecure_channel_create("nonexistant:54321", NULL, NULL);
    g_state.call = grpc_channel_create_call(
        g_state.chan, NULL, GRPC_PROPAGATE_DEFAULTS, g_state.cq, "/Foo",
        "nonexistant", g_state.deadline, NULL);
  } else {
    g_state.server = grpc_server_create(NULL, NULL);
    grpc_server_register_completion_queue(g_state.server, g_state.cq, NULL);
    port = grpc_pick_unused_port_or_die();
    gpr_join_host_port(&server_hostport, "0.0.0.0", port);
    grpc_server_add_insecure_http2_port(g_state.server, server_hostport);
    grpc_server_start(g_state.server);
    gpr_free(server_hostport);
    gpr_join_host_port(&server_hostport, "localhost", port);
    g_state.chan = grpc_insecure_channel_create(server_hostport, NULL, NULL);
    gpr_free(server_hostport);
    g_state.call = grpc_channel_create_call(
        g_state.chan, NULL, GRPC_PROPAGATE_DEFAULTS, g_state.cq, "/Foo", "bar",
        g_state.deadline, NULL);
    grpc_metadata_array_init(&g_state.server_initial_metadata_recv);
    grpc_call_details_init(&g_state.call_details);
    op = g_state.ops;
    op->op = GRPC_OP_SEND_INITIAL_METADATA;
    op->data.send_initial_metadata.count = 0;
    op->flags = 0;
    op->reserved = NULL;
    op++;
    GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(g_state.call, g_state.ops,
                                                     (size_t)(op - g_state.ops),
                                                     tag(1), NULL));
    GPR_ASSERT(GRPC_CALL_OK ==
               grpc_server_request_call(g_state.server, &g_state.server_call,
                                        &g_state.call_details,
                                        &g_state.server_initial_metadata_recv,
                                        g_state.cq, g_state.cq, tag(101)));
    cq_expect_completion(g_state.cqv, tag(101), 1);
    cq_expect_completion(g_state.cqv, tag(1), 1);
    cq_verify(g_state.cqv);
  }
}
コード例 #9
0
void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f,
                                  grpc_channel_args *server_args) {
  fullstack_fixture_data *ffd = f->fixture_data;
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  f->server = grpc_server_create(server_args);
  grpc_server_register_completion_queue(f->server, f->cq);
  GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr));
  grpc_server_start(f->server);
}
コード例 #10
0
ファイル: server_test.c プロジェクト: 201528013359030/grpc
void test_bind_server_twice(void) {
  char *addr;
  grpc_server *server1 = grpc_server_create(NULL, NULL);
  grpc_server *server2 = grpc_server_create(NULL, NULL);
  grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
  int port = grpc_pick_unused_port_or_die();
  gpr_asprintf(&addr, "[::]:%d", port);
  grpc_server_register_completion_queue(server1, cq, NULL);
  grpc_server_register_completion_queue(server2, cq, NULL);
  GPR_ASSERT(port == grpc_server_add_insecure_http2_port(server1, addr));
  GPR_ASSERT(0 == grpc_server_add_insecure_http2_port(server2, addr));
  grpc_server_shutdown_and_notify(server1, cq, NULL);
  grpc_server_shutdown_and_notify(server2, cq, NULL);
  grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL);
  grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL);
  grpc_server_destroy(server1);
  grpc_server_destroy(server2);
  grpc_completion_queue_destroy(cq);
  gpr_free(addr);
}
コード例 #11
0
ファイル: server_ssl.c プロジェクト: gnirodi/grpc
// Simple gRPC server. This listens until client_handshake_complete occurs.
static void server_thread(void *arg) {
  const int port = *(int *)arg;

  // Load key pair and establish server SSL credentials.
  grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
  gpr_slice ca_slice, cert_slice, key_slice;
  GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
                               grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
  GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
                               grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
  GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
                               grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
  const char *ca_cert = (const char *)GPR_SLICE_START_PTR(ca_slice);
  pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice);
  pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice);
  grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create(
      ca_cert, &pem_key_cert_pair, 1, 0, NULL);

  // Start server listening on local port.
  char *addr;
  gpr_asprintf(&addr, "127.0.0.1:%d", port);
  grpc_server *server = grpc_server_create(NULL, NULL);
  GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
  free(addr);

  grpc_completion_queue *cq = grpc_completion_queue_create(NULL);

  grpc_server_register_completion_queue(server, cq, NULL);
  grpc_server_start(server);

  // Wait a bounded number of time until client_handshake_complete is set,
  // sleeping between polls.
  int retries = 10;
  while (!gpr_event_get(&client_handshake_complete) && retries-- > 0) {
    const gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1);
    grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
    GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
  }

  gpr_log(GPR_INFO, "Shutting down server");
  grpc_server_shutdown_and_notify(server, cq, NULL);
  grpc_completion_queue_shutdown(cq);

  const gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5);
  grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
  GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);

  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq);
  grpc_server_credentials_release(ssl_creds);
  gpr_slice_unref(cert_slice);
  gpr_slice_unref(key_slice);
  gpr_slice_unref(ca_slice);
}
コード例 #12
0
ファイル: h2_proxy.c プロジェクト: Indifer/grpc
void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f,
                                  grpc_channel_args *server_args) {
  fullstack_fixture_data *ffd = f->fixture_data;
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  f->server = grpc_server_create(server_args, NULL);
  grpc_server_register_completion_queue(f->server, f->cq, NULL);
  GPR_ASSERT(grpc_server_add_insecure_http2_port(
      f->server, grpc_end2end_proxy_get_server_port(ffd->proxy)));
  grpc_server_start(f->server);
}
コード例 #13
0
static void chttp2_init_server_secure_fullstack(
    grpc_end2end_test_fixture *f, grpc_channel_args *server_args,
    grpc_server_credentials *server_creds) {
  fullstack_secure_fixture_data *ffd = f->fixture_data;
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  f->server =
      grpc_server_create(f->server_cq, server_args);
  GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr, server_creds));
  grpc_server_credentials_release(server_creds);
  grpc_server_start(f->server);
}
コード例 #14
0
int main(int argc, char **argv) {
  grpc_completion_queue *cq1;
  grpc_completion_queue *cq2;
  grpc_completion_queue *cq3;
  grpc_completion_queue_attributes attr;

  grpc_server *server;

  grpc_test_init(argc, argv);
  grpc_init();

  attr.version = 1;
  attr.cq_completion_type = GRPC_CQ_NEXT;
  attr.cq_polling_type = GRPC_CQ_DEFAULT_POLLING;
  cq1 = grpc_completion_queue_create(
      grpc_completion_queue_factory_lookup(&attr), &attr, NULL);

  attr.cq_polling_type = GRPC_CQ_NON_LISTENING;
  cq2 = grpc_completion_queue_create(
      grpc_completion_queue_factory_lookup(&attr), &attr, NULL);

  attr.cq_polling_type = GRPC_CQ_NON_POLLING;
  cq3 = grpc_completion_queue_create(
      grpc_completion_queue_factory_lookup(&attr), &attr, NULL);

  server = grpc_server_create(NULL, NULL);
  grpc_server_register_completion_queue(server, cq1, NULL);
  grpc_server_add_insecure_http2_port(server, "[::]:0");
  grpc_server_register_completion_queue(server, cq2, NULL);
  grpc_server_register_completion_queue(server, cq3, NULL);

  grpc_server_start(server);
  grpc_server_shutdown_and_notify(server, cq2, NULL);
  grpc_completion_queue_next(cq2, gpr_inf_future(GPR_CLOCK_REALTIME),
                             NULL); /* cue queue hang */
  grpc_completion_queue_shutdown(cq1);
  grpc_completion_queue_shutdown(cq2);
  grpc_completion_queue_shutdown(cq3);

  grpc_completion_queue_next(cq1, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  grpc_completion_queue_next(cq2, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  grpc_completion_queue_next(cq3, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);

  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq1);
  grpc_completion_queue_destroy(cq2);
  grpc_completion_queue_destroy(cq3);
  grpc_shutdown();
  return 0;
}
コード例 #15
0
void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f,
                                  grpc_channel_args *server_args) {
  fullstack_fixture_data *ffd = f->fixture_data;
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  grpc_arg arg = grpc_load_reporting_config_create_arg(g_server_lrc);
  server_args = grpc_channel_args_copy_and_add(server_args, &arg, 1);
  f->server = grpc_server_create(server_args, NULL);
  grpc_channel_args_destroy(server_args);
  grpc_server_register_completion_queue(f->server, f->cq, NULL);
  GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr));
  grpc_server_start(f->server);
}
コード例 #16
0
static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f,
        grpc_channel_args *server_args) {
    grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
    sp_fixture_data *sfd = f->fixture_data;
    GPR_ASSERT(!f->server);
    f->server = grpc_server_create(server_args, NULL);
    GPR_ASSERT(f->server);
    grpc_server_register_completion_queue(f->server, f->cq, NULL);
    grpc_server_start(f->server);

    grpc_server_add_insecure_channel_from_fd(f->server, f->cq, sfd->fd_pair[1]);

    grpc_exec_ctx_finish(&exec_ctx);
}
コード例 #17
0
static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f,
                                          grpc_channel_args *server_args) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_endpoint_pair *sfd = f->fixture_data;
  grpc_transport *transport;
  GPR_ASSERT(!f->server);
  f->server = grpc_server_create(server_args, NULL);
  grpc_server_register_completion_queue(f->server, f->cq, NULL);
  grpc_server_start(f->server);
  transport =
      grpc_create_chttp2_transport(&exec_ctx, server_args, sfd->server, 0);
  server_setup_transport(f, transport);
  grpc_chttp2_transport_start_reading(&exec_ctx, transport, NULL, 0);
  grpc_exec_ctx_finish(&exec_ctx);
}
コード例 #18
0
void chttp2_init_server_fullstack_compression(grpc_end2end_test_fixture *f,
                                  grpc_channel_args *server_args) {
  fullstack_compression_fixture_data *ffd = f->fixture_data;
  if (ffd->server_args_compression != NULL) {
    grpc_channel_args_destroy(ffd->server_args_compression);
  }
  ffd->server_args_compression = grpc_channel_args_set_compression_algorithm(
      server_args, GRPC_COMPRESS_GZIP);
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  f->server = grpc_server_create(ffd->server_args_compression);
  grpc_server_register_completion_queue(f->server, f->cq);
  GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr));
  grpc_server_start(f->server);
}
コード例 #19
0
ファイル: _server.c プロジェクト: Zerqkboo/grpc
static int pygrpc_server_init(Server *self, PyObject *args, PyObject *kwds) {
  CompletionQueue *completion_queue;
  static char *kwlist[] = {"completion_queue", NULL};

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!:Server", kwlist,
                                   &pygrpc_CompletionQueueType,
                                   &completion_queue)) {
    return -1;
  }
  self->c_server = grpc_server_create(NULL);
  grpc_server_register_completion_queue(self->c_server,
                                        completion_queue->c_completion_queue);
  self->completion_queue = completion_queue;
  Py_INCREF(completion_queue);
  return 0;
}
コード例 #20
0
ファイル: h2_load_reporting.c プロジェクト: royalharsh/grpc
void chttp2_init_server_load_reporting(grpc_end2end_test_fixture *f,
                                       grpc_channel_args *server_args) {
  load_reporting_fixture_data *ffd = f->fixture_data;
  grpc_arg arg = grpc_load_reporting_enable_arg();
  if (f->server) {
    grpc_server_destroy(f->server);
  }
  server_args = grpc_channel_args_copy_and_add(server_args, &arg, 1);
  f->server = grpc_server_create(server_args, NULL);
  {
    grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
    grpc_channel_args_destroy(&exec_ctx, server_args);
    grpc_exec_ctx_finish(&exec_ctx);
  }
  grpc_server_register_completion_queue(f->server, f->cq, NULL);
  GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr));
  grpc_server_start(f->server);
}
コード例 #21
0
ファイル: server_chttp2_test.c プロジェクト: An-mol/grpc
void test_add_same_port_twice() {
  int port = grpc_pick_unused_port_or_die();
  char *addr = NULL;
  grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
  grpc_server *server = grpc_server_create(NULL, NULL);
  grpc_server_credentials *fake_creds =
      grpc_fake_transport_security_server_credentials_create();
  gpr_join_host_port(&addr, "localhost", port);
  GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds));
  GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds) == 0);

  grpc_server_credentials_release(fake_creds);
  gpr_free(addr);
  grpc_server_shutdown_and_notify(server, cq, NULL);
  grpc_completion_queue_pluck(cq, NULL, gpr_inf_future(GPR_CLOCK_REALTIME),
                              NULL);
  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq);
}
コード例 #22
0
ファイル: lb_policies_test.c プロジェクト: cj525/grpc
static void revive_server(const servers_fixture *f, request_data *rdata,
                          size_t i) {
  int got_port;
  gpr_log(GPR_INFO, "RAISE AGAIN SERVER %d", i);
  GPR_ASSERT(f->servers[i] == NULL);

  gpr_log(GPR_DEBUG, "revive: %s", f->servers_hostports[i]);

  f->servers[i] = grpc_server_create(NULL, NULL);
  grpc_server_register_completion_queue(f->servers[i], f->cq, NULL);
  GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port(
                  f->servers[i], f->servers_hostports[i])) > 0);
  grpc_server_start(f->servers[i]);

  GPR_ASSERT(GRPC_CALL_OK ==
             grpc_server_request_call(f->servers[i], &f->server_calls[i],
                                      &rdata->call_details[i],
                                      &f->request_metadata_recv[i], f->cq,
                                      f->cq, tag(1000 + (int)i)));
}
コード例 #23
0
ファイル: server.c プロジェクト: hongweiwang/grpc
Server *pygrpc_Server_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
  Server *self;
  CompletionQueue *cq;
  PyObject *py_args;
  grpc_channel_args c_args;
  char *keywords[] = {"cq", "args", NULL};
  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O:Server", keywords,
        &pygrpc_CompletionQueue_type, &cq, &py_args)) {
    return NULL;
  }
  if (!pygrpc_produce_channel_args(py_args, &c_args)) {
    return NULL;
  }
  self = (Server *)type->tp_alloc(type, 0);
  self->c_serv = grpc_server_create(&c_args, NULL);
  grpc_server_register_completion_queue(self->c_serv, cq->c_cq, NULL);
  pygrpc_discard_channel_args(c_args);
  self->cq = cq;
  Py_INCREF(self->cq);
  return self;
}
コード例 #24
0
ファイル: dualstack_socket_test.c プロジェクト: wilhuff/grpc
void test_connect(const char *server_host, const char *client_host, int port,
                  int expect_ok) {
  char *client_hostport;
  char *server_hostport;
  grpc_channel *client;
  grpc_server *server;
  grpc_completion_queue *cq;
  grpc_call *c;
  grpc_call *s;
  cq_verifier *cqv;
  gpr_timespec deadline;
  int got_port;
  grpc_op ops[6];
  grpc_op *op;
  grpc_metadata_array initial_metadata_recv;
  grpc_metadata_array trailing_metadata_recv;
  grpc_metadata_array request_metadata_recv;
  grpc_status_code status;
  grpc_call_error error;
  char *details = NULL;
  size_t details_capacity = 0;
  int was_cancelled = 2;
  grpc_call_details call_details;
  char *peer;
  int picked_port = 0;

  if (port == 0) {
    port = grpc_pick_unused_port_or_die();
    picked_port = 1;
  }

  gpr_join_host_port(&server_hostport, server_host, port);

  grpc_metadata_array_init(&initial_metadata_recv);
  grpc_metadata_array_init(&trailing_metadata_recv);
  grpc_metadata_array_init(&request_metadata_recv);
  grpc_call_details_init(&call_details);

  /* Create server. */
  cq = grpc_completion_queue_create(NULL);
  server = grpc_server_create(NULL, NULL);
  grpc_server_register_completion_queue(server, cq, NULL);
  GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port(
                  server, server_hostport)) > 0);
  if (port == 0) {
    port = got_port;
  } else {
    GPR_ASSERT(port == got_port);
  }
  grpc_server_start(server);
  cqv = cq_verifier_create(cq);

  /* Create client. */
  if (client_host[0] == 'i') {
    /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */
    size_t i;
    gpr_slice uri_slice;
    gpr_slice_buffer uri_parts;
    char **hosts_with_port;

    uri_slice =
        gpr_slice_new((char *)client_host, strlen(client_host), do_nothing);
    gpr_slice_buffer_init(&uri_parts);
    gpr_slice_split(uri_slice, ",", &uri_parts);
    hosts_with_port = gpr_malloc(sizeof(char *) * uri_parts.count);
    for (i = 0; i < uri_parts.count; i++) {
      char *uri_part_str = gpr_dump_slice(uri_parts.slices[i], GPR_DUMP_ASCII);
      gpr_asprintf(&hosts_with_port[i], "%s:%d", uri_part_str, port);
      gpr_free(uri_part_str);
    }
    client_hostport = gpr_strjoin_sep((const char **)hosts_with_port,
                                      uri_parts.count, ",", NULL);
    for (i = 0; i < uri_parts.count; i++) {
      gpr_free(hosts_with_port[i]);
    }
    gpr_free(hosts_with_port);
    gpr_slice_buffer_destroy(&uri_parts);
    gpr_slice_unref(uri_slice);
  } else {
    gpr_join_host_port(&client_hostport, client_host, port);
  }
  client = grpc_insecure_channel_create(client_hostport, NULL, NULL);

  gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)",
          server_hostport, client_hostport, expect_ok ? "success" : "failure");

  gpr_free(client_hostport);
  gpr_free(server_hostport);

  if (expect_ok) {
    /* Normal deadline, shouldn't be reached. */
    deadline = ms_from_now(60000);
  } else {
    /* Give up faster when failure is expected.
       BUG: Setting this to 1000 reveals a memory leak (b/18608927). */
    deadline = ms_from_now(1500);
  }

  /* Send a trivial request. */
  c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
                               "/foo", "foo.test.google.fr", deadline, NULL);
  GPR_ASSERT(c);

  memset(ops, 0, sizeof(ops));
  op = ops;
  op->op = GRPC_OP_SEND_INITIAL_METADATA;
  op->data.send_initial_metadata.count = 0;
  op->flags = expect_ok ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
  op->reserved = NULL;
  op++;
  op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  op->flags = 0;
  op->reserved = NULL;
  op++;
  op->op = GRPC_OP_RECV_INITIAL_METADATA;
  op->data.recv_initial_metadata = &initial_metadata_recv;
  op->flags = 0;
  op->reserved = NULL;
  op++;
  op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  op->data.recv_status_on_client.status = &status;
  op->data.recv_status_on_client.status_details = &details;
  op->data.recv_status_on_client.status_details_capacity = &details_capacity;
  op->flags = 0;
  op->reserved = NULL;
  op++;
  error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
  GPR_ASSERT(GRPC_CALL_OK == error);

  if (expect_ok) {
    /* Check for a successful request. */
    error = grpc_server_request_call(server, &s, &call_details,
                                     &request_metadata_recv, cq, cq, tag(101));
    GPR_ASSERT(GRPC_CALL_OK == error);
    CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
    cq_verify(cqv);

    memset(ops, 0, sizeof(ops));
    op = ops;
    op->op = GRPC_OP_SEND_INITIAL_METADATA;
    op->data.send_initial_metadata.count = 0;
    op->flags = 0;
    op++;
    op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
    op->data.send_status_from_server.trailing_metadata_count = 0;
    op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
    op->data.send_status_from_server.status_details = "xyz";
    op->flags = 0;
    op++;
    op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
    op->data.recv_close_on_server.cancelled = &was_cancelled;
    op->flags = 0;
    op++;
    error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
    GPR_ASSERT(GRPC_CALL_OK == error);

    CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
    CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
    cq_verify(cqv);

    peer = grpc_call_get_peer(c);
    gpr_log(GPR_DEBUG, "got peer: '%s'", peer);
    gpr_free(peer);

    GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
    GPR_ASSERT(0 == strcmp(details, "xyz"));
    GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
    GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
    GPR_ASSERT(was_cancelled == 1);

    grpc_call_destroy(s);
  } else {
    /* Check for a failed connection. */
    CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
    cq_verify(cqv);

    GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
  }

  grpc_call_destroy(c);

  cq_verifier_destroy(cqv);

  /* Destroy client. */
  grpc_channel_destroy(client);

  /* Destroy server. */
  grpc_server_shutdown_and_notify(server, cq, tag(1000));
  GPR_ASSERT(grpc_completion_queue_pluck(
                 cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL)
                 .type == GRPC_OP_COMPLETE);
  grpc_server_destroy(server);
  grpc_completion_queue_shutdown(cq);
  drain_cq(cq);
  grpc_completion_queue_destroy(cq);

  grpc_metadata_array_destroy(&initial_metadata_recv);
  grpc_metadata_array_destroy(&trailing_metadata_recv);
  grpc_metadata_array_destroy(&request_metadata_recv);

  grpc_call_details_destroy(&call_details);
  gpr_free(details);
  if (picked_port) {
    grpc_recycle_unused_port(port);
  }
}
コード例 #25
0
void test_connect(const char *server_host, const char *client_host, int port,
                  int expect_ok) {
  char *client_hostport;
  char *server_hostport;
  grpc_channel *client;
  grpc_server *server;
  grpc_completion_queue *client_cq;
  grpc_completion_queue *server_cq;
  grpc_call *c;
  grpc_call *s;
  cq_verifier *v_client;
  cq_verifier *v_server;
  gpr_timespec deadline;
  int got_port;
  grpc_op ops[6];
  grpc_op *op;
  grpc_metadata_array initial_metadata_recv;
  grpc_metadata_array trailing_metadata_recv;
  grpc_metadata_array request_metadata_recv;
  grpc_status_code status;
  char *details = NULL;
  size_t details_capacity = 0;
  int was_cancelled = 2;
  grpc_call_details call_details;

  if (port == 0) {
    port = grpc_pick_unused_port_or_die();
  }

  gpr_join_host_port(&server_hostport, server_host, port);

  grpc_metadata_array_init(&initial_metadata_recv);
  grpc_metadata_array_init(&trailing_metadata_recv);
  grpc_metadata_array_init(&request_metadata_recv);
  grpc_call_details_init(&call_details);

  /* Create server. */
  server_cq = grpc_completion_queue_create();
  server = grpc_server_create(NULL);
  grpc_server_register_completion_queue(server, server_cq);
  GPR_ASSERT((got_port = grpc_server_add_http2_port(server, server_hostport)) >
             0);
  if (port == 0) {
    port = got_port;
  } else {
    GPR_ASSERT(port == got_port);
  }
  grpc_server_start(server);
  v_server = cq_verifier_create(server_cq);

  /* Create client. */
  gpr_join_host_port(&client_hostport, client_host, port);
  client_cq = grpc_completion_queue_create();
  client = grpc_channel_create(client_hostport, NULL);
  v_client = cq_verifier_create(client_cq);

  gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)",
          server_hostport, client_hostport, expect_ok ? "success" : "failure");

  gpr_free(client_hostport);
  gpr_free(server_hostport);

  if (expect_ok) {
    /* Normal deadline, shouldn't be reached. */
    deadline = ms_from_now(60000);
  } else {
    /* Give up faster when failure is expected.
       BUG: Setting this to 1000 reveals a memory leak (b/18608927). */
    deadline = ms_from_now(1500);
  }

  /* Send a trivial request. */
  c = grpc_channel_create_call(client, client_cq, "/foo", "foo.test.google.fr",
                               deadline);
  GPR_ASSERT(c);

  op = ops;
  op->op = GRPC_OP_SEND_INITIAL_METADATA;
  op->data.send_initial_metadata.count = 0;
  op++;
  op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  op++;
  op->op = GRPC_OP_RECV_INITIAL_METADATA;
  op->data.recv_initial_metadata = &initial_metadata_recv;
  op++;
  op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  op->data.recv_status_on_client.status = &status;
  op->data.recv_status_on_client.status_details = &details;
  op->data.recv_status_on_client.status_details_capacity = &details_capacity;
  op++;
  GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));

  if (expect_ok) {
    /* Check for a successful request. */
    GPR_ASSERT(GRPC_CALL_OK ==
               grpc_server_request_call(server, &s, &call_details,
                                        &request_metadata_recv, server_cq,
                                        server_cq, tag(101)));
    cq_expect_completion(v_server, tag(101), GRPC_OP_OK);
    cq_verify(v_server);

    op = ops;
    op->op = GRPC_OP_SEND_INITIAL_METADATA;
    op->data.send_initial_metadata.count = 0;
    op++;
    op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
    op->data.send_status_from_server.trailing_metadata_count = 0;
    op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
    op->data.send_status_from_server.status_details = "xyz";
    op++;
    op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
    op->data.recv_close_on_server.cancelled = &was_cancelled;
    op++;
    GPR_ASSERT(GRPC_CALL_OK ==
               grpc_call_start_batch(s, ops, op - ops, tag(102)));

    cq_expect_completion(v_server, tag(102), GRPC_OP_OK);
    cq_verify(v_server);

    cq_expect_completion(v_client, tag(1), GRPC_OP_OK);
    cq_verify(v_client);

    GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
    GPR_ASSERT(0 == strcmp(details, "xyz"));
    GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
    GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
    GPR_ASSERT(was_cancelled == 0);

    grpc_call_destroy(s);
  } else {
    /* Check for a failed connection. */
    cq_expect_completion(v_client, tag(1), GRPC_OP_OK);
    cq_verify(v_client);

    GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
  }

  grpc_call_destroy(c);

  cq_verifier_destroy(v_client);
  cq_verifier_destroy(v_server);

  /* Destroy client. */
  grpc_channel_destroy(client);
  grpc_completion_queue_shutdown(client_cq);
  drain_cq(client_cq);
  grpc_completion_queue_destroy(client_cq);

  /* Destroy server. */
  grpc_server_shutdown(server);
  grpc_server_destroy(server);
  grpc_completion_queue_shutdown(server_cq);
  drain_cq(server_cq);
  grpc_completion_queue_destroy(server_cq);
}
コード例 #26
0
ファイル: server.c プロジェクト: mindis/grpc
int main(int argc, char **argv) {
  grpc_event *ev;
  call_state *s;
  char *addr_buf = NULL;
  gpr_cmdline *cl;
  int shutdown_started = 0;
  int shutdown_finished = 0;

  int secure = 0;
  char *addr = NULL;

  char *fake_argv[1];

#define MAX_ARGS 4
  grpc_arg arge[MAX_ARGS];
  grpc_arg *e;
  grpc_channel_args args = {0, NULL};

  grpc_http_server_page home_page = {"/", "text/html",
                                     "<head>\n"
                                     "<title>Echo Server</title>\n"
                                     "</head>\n"
                                     "<body>\n"
                                     "Welcome to the world of the future!\n"
                                     "</body>\n"};

  GPR_ASSERT(argc >= 1);
  fake_argv[0] = argv[0];
  grpc_test_init(1, fake_argv);

  grpc_init();
  srand(clock());
  memset(arge, 0, sizeof(arge));
  args.args = arge;

  cl = gpr_cmdline_create("echo server");
  gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
  gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
  gpr_cmdline_parse(cl, argc, argv);
  gpr_cmdline_destroy(cl);

  e = &arge[args.num_args++];
  e->type = GRPC_ARG_POINTER;
  e->key = GRPC_ARG_SERVE_OVER_HTTP;
  e->value.pointer.p = &home_page;

  if (addr == NULL) {
    gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die());
    addr = addr_buf;
  }
  gpr_log(GPR_INFO, "creating server on: %s", addr);

  cq = grpc_completion_queue_create();
  if (secure) {
    grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
                                                    test_server1_cert};
    grpc_server_credentials *ssl_creds =
        grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1);
    server = grpc_secure_server_create(ssl_creds, cq, &args);
    GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr));
    grpc_server_credentials_release(ssl_creds);
  } else {
    server = grpc_server_create(cq, &args);
    GPR_ASSERT(grpc_server_add_http2_port(server, addr));
  }
  grpc_server_start(server);

  gpr_free(addr_buf);
  addr = addr_buf = NULL;

  request_call();

  signal(SIGINT, sigint_handler);
  while (!shutdown_finished) {
    if (got_sigint && !shutdown_started) {
      gpr_log(GPR_INFO, "Shutting down due to SIGINT");
      grpc_server_shutdown(server);
      grpc_completion_queue_shutdown(cq);
      shutdown_started = 1;
    }
    ev = grpc_completion_queue_next(
        cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(1)));
    if (!ev) continue;
    s = ev->tag;
    switch (ev->type) {
      case GRPC_SERVER_RPC_NEW:
        if (ev->call != NULL) {
          /* initial ops are already started in request_call */
          grpc_call_server_accept_old(ev->call, cq, s);
          grpc_call_server_end_initial_metadata_old(ev->call,
                                                    GRPC_WRITE_BUFFER_HINT);
          GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK);
          request_call();
        } else {
          GPR_ASSERT(shutdown_started);
          gpr_free(s);
        }
        break;
      case GRPC_WRITE_ACCEPTED:
        GPR_ASSERT(ev->data.write_accepted == GRPC_OP_OK);
        GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK);
        break;
      case GRPC_READ:
        if (ev->data.read) {
          assert_read_ok(ev->tag, ev->data.read);
          GPR_ASSERT(grpc_call_start_write_old(ev->call, ev->data.read, s,
                                               GRPC_WRITE_BUFFER_HINT) ==
                     GRPC_CALL_OK);
        } else {
          GPR_ASSERT(grpc_call_start_write_status_old(ev->call, GRPC_STATUS_OK,
                                                      NULL, s) == GRPC_CALL_OK);
        }
        break;
      case GRPC_FINISH_ACCEPTED:
      case GRPC_FINISHED:
        if (gpr_unref(&s->pending_ops)) {
          grpc_call_destroy(ev->call);
          gpr_free(s);
        }
        break;
      case GRPC_QUEUE_SHUTDOWN:
        GPR_ASSERT(shutdown_started);
        shutdown_finished = 1;
        break;
      default:
        GPR_ASSERT(0);
    }
    grpc_event_finish(ev);
  }

  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq);
  grpc_shutdown();

  return 0;
}
コード例 #27
0
ファイル: grpc_csharp_ext.c プロジェクト: andrewpollock/grpc
GPR_EXPORT grpc_server *GPR_CALLTYPE
grpcsharp_server_create(const grpc_channel_args *args) {
  return grpc_server_create(args, NULL);
}
コード例 #28
0
int run_concurrent_connectivity_test() {
  struct server_thread_args args;
  memset(&args, 0, sizeof(args));

  grpc_init();

  gpr_thd_id threads[NUM_THREADS];
  gpr_thd_id server;

  char *localhost = gpr_strdup("localhost:54321");
  gpr_thd_options options = gpr_thd_options_default();
  gpr_thd_options_set_joinable(&options);

  /* First round, no server */
  gpr_log(GPR_DEBUG, "Wave 1");
  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_new(&threads[i], create_loop_destroy, localhost, &options);
  }
  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_join(threads[i]);
  }
  gpr_free(localhost);

  /* Second round, actual grpc server */
  gpr_log(GPR_DEBUG, "Wave 2");
  int port = grpc_pick_unused_port_or_die();
  gpr_asprintf(&args.addr, "localhost:%d", port);
  args.server = grpc_server_create(NULL, NULL);
  grpc_server_add_insecure_http2_port(args.server, args.addr);
  args.cq = grpc_completion_queue_create_for_next(NULL);
  grpc_server_register_completion_queue(args.server, args.cq, NULL);
  grpc_server_start(args.server);
  gpr_thd_new(&server, server_thread, &args, &options);

  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_new(&threads[i], create_loop_destroy, args.addr, &options);
  }
  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_join(threads[i]);
  }
  grpc_server_shutdown_and_notify(args.server, args.cq, tag(0xd1e));

  gpr_thd_join(server);
  grpc_server_destroy(args.server);
  grpc_completion_queue_destroy(args.cq);
  gpr_free(args.addr);

  /* Third round, bogus tcp server */
  gpr_log(GPR_DEBUG, "Wave 3");
  args.pollset = gpr_zalloc(grpc_pollset_size());
  grpc_pollset_init(args.pollset, &args.mu);
  gpr_event_init(&args.ready);
  gpr_thd_new(&server, bad_server_thread, &args, &options);
  gpr_event_wait(&args.ready, gpr_inf_future(GPR_CLOCK_MONOTONIC));

  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_new(&threads[i], create_loop_destroy, args.addr, &options);
  }
  for (size_t i = 0; i < NUM_THREADS; ++i) {
    gpr_thd_join(threads[i]);
  }

  gpr_atm_rel_store(&args.stop, 1);
  gpr_thd_join(server);
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_pollset_shutdown(&exec_ctx, args.pollset,
                        GRPC_CLOSURE_CREATE(done_pollset_shutdown, args.pollset,
                                            grpc_schedule_on_exec_ctx));
  grpc_exec_ctx_finish(&exec_ctx);

  grpc_shutdown();
  return 0;
}
コード例 #29
0
ファイル: h2_proxy.c プロジェクト: Indifer/grpc
static grpc_server *create_proxy_server(const char *port,
                                        grpc_channel_args *server_args) {
  grpc_server *s = grpc_server_create(server_args, NULL);
  GPR_ASSERT(grpc_server_add_insecure_http2_port(s, port));
  return s;
}
コード例 #30
0
ファイル: server.c プロジェクト: ELMERzark/grpc
int main(int argc, char **argv) {
  grpc_event ev;
  call_state *s;
  char *addr_buf = NULL;
  gpr_cmdline *cl;
  int shutdown_started = 0;
  int shutdown_finished = 0;

  int secure = 0;
  char *addr = NULL;

  char *fake_argv[1];

  GPR_ASSERT(argc >= 1);
  fake_argv[0] = argv[0];
  grpc_test_init(1, fake_argv);

  grpc_init();
  srand(clock());

  cl = gpr_cmdline_create("fling server");
  gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
  gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
  gpr_cmdline_parse(cl, argc, argv);
  gpr_cmdline_destroy(cl);

  if (addr == NULL) {
    gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die());
    addr = addr_buf;
  }
  gpr_log(GPR_INFO, "creating server on: %s", addr);

  cq = grpc_completion_queue_create();
  if (secure) {
    grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
                                                    test_server1_cert};
    grpc_server_credentials *ssl_creds =
        grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1, 0);
    server = grpc_server_create(NULL);
    GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
    grpc_server_credentials_release(ssl_creds);
  } else {
    server = grpc_server_create(NULL);
    GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr));
  }
  grpc_server_register_completion_queue(server, cq);
  grpc_server_start(server);

  gpr_free(addr_buf);
  addr = addr_buf = NULL;

  grpc_call_details_init(&call_details);

  request_call();

  grpc_profiler_start("server.prof");
  signal(SIGINT, sigint_handler);
  while (!shutdown_finished) {
    if (got_sigint && !shutdown_started) {
      gpr_log(GPR_INFO, "Shutting down due to SIGINT");
      grpc_server_shutdown_and_notify(server, cq, tag(1000));
      GPR_ASSERT(grpc_completion_queue_pluck(
                     cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))
                     .type == GRPC_OP_COMPLETE);
      grpc_completion_queue_shutdown(cq);
      shutdown_started = 1;
    }
    ev = grpc_completion_queue_next(
        cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
                         gpr_time_from_micros(1000000, GPR_TIMESPAN)));
    s = ev.tag;
    switch (ev.type) {
      case GRPC_OP_COMPLETE:
        switch ((gpr_intptr)s) {
          case FLING_SERVER_NEW_REQUEST:
            if (call != NULL) {
              if (0 ==
                  strcmp(call_details.method, "/Reflector/reflectStream")) {
                /* Received streaming call. Send metadata here. */
                start_read_op(FLING_SERVER_READ_FOR_STREAMING);
                send_initial_metadata();
              } else {
                /* Received unary call. Can do all ops in one batch. */
                start_read_op(FLING_SERVER_READ_FOR_UNARY);
              }
            } else {
              GPR_ASSERT(shutdown_started);
            }
            /*	    request_call();
             */
            break;
          case FLING_SERVER_READ_FOR_STREAMING:
            if (payload_buffer != NULL) {
              /* Received payload from client. */
              start_write_op();
            } else {
              /* Received end of stream from client. */
              start_send_status();
            }
            break;
          case FLING_SERVER_WRITE_FOR_STREAMING:
            /* Write completed at server  */
            grpc_byte_buffer_destroy(payload_buffer);
            payload_buffer = NULL;
            start_read_op(FLING_SERVER_READ_FOR_STREAMING);
            break;
          case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING:
            /* Metadata send completed at server */
            break;
          case FLING_SERVER_SEND_STATUS_FOR_STREAMING:
            /* Send status and close completed at server */
            grpc_call_destroy(call);
            if (!shutdown_started) request_call();
            break;
          case FLING_SERVER_READ_FOR_UNARY:
            /* Finished payload read for unary. Start all reamaining
             *  unary ops in a batch.
             */
            handle_unary_method();
            break;
          case FLING_SERVER_BATCH_OPS_FOR_UNARY:
            /* Finished unary call. */
            grpc_byte_buffer_destroy(payload_buffer);
            payload_buffer = NULL;
            grpc_call_destroy(call);
            if (!shutdown_started) request_call();
            break;
        }
        break;
      case GRPC_QUEUE_SHUTDOWN:
        GPR_ASSERT(shutdown_started);
        shutdown_finished = 1;
        break;
      case GRPC_QUEUE_TIMEOUT:
        break;
    }
  }
  grpc_profiler_stop();
  grpc_call_details_destroy(&call_details);

  grpc_server_destroy(server);
  grpc_completion_queue_destroy(cq);
  grpc_shutdown();
  return 0;
}