Esempio n. 1
0
/* grpc_run_batch_stack_build_result fills constructs a ruby BatchResult struct
   after the results have run */
static VALUE grpc_run_batch_stack_build_result(run_batch_stack* st) {
  size_t i = 0;
  VALUE result = rb_struct_new(grpc_rb_sBatchResult, Qnil, Qnil, Qnil, Qnil,
                               Qnil, Qnil, Qnil, Qnil, NULL);
  for (i = 0; i < st->op_num; i++) {
    switch(st->ops[i].op) {
      case GRPC_OP_SEND_INITIAL_METADATA:
        rb_struct_aset(result, sym_send_metadata, Qtrue);
        break;
      case GRPC_OP_SEND_MESSAGE:
        rb_struct_aset(result, sym_send_message, Qtrue);
        grpc_byte_buffer_destroy(st->ops[i].data.send_message);
        break;
      case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      case GRPC_OP_SEND_STATUS_FROM_SERVER:
        rb_struct_aset(result, sym_send_status, Qtrue);
        break;
      case GRPC_OP_RECV_INITIAL_METADATA:
        rb_struct_aset(result, sym_metadata,
                       grpc_rb_md_ary_to_h(&st->recv_metadata));
      case GRPC_OP_RECV_MESSAGE:
        rb_struct_aset(result, sym_message,
                       grpc_rb_byte_buffer_to_s(st->recv_message));
        break;
      case GRPC_OP_RECV_STATUS_ON_CLIENT:
        rb_struct_aset(
            result,
            sym_status,
            rb_struct_new(grpc_rb_sStatus,
                          UINT2NUM(st->recv_status),
                          (st->recv_status_details == NULL
                           ? Qnil
                           : rb_str_new2(st->recv_status_details)),
                          grpc_rb_md_ary_to_h(&st->recv_trailing_metadata),
                          NULL));
        break;
      case GRPC_OP_RECV_CLOSE_ON_SERVER:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      default:
        break;
    }
  }
  return result;
}
Esempio n. 2
0
/* call-seq:
   cq = CompletionQueue.new
   tag = Object.new
   timeout = 10
   server.request_call(cqueue, tag, timeout)

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
                                         VALUE tag_new, VALUE timeout) {
  grpc_rb_server *s = NULL;
  grpc_call *call = NULL;
  grpc_event *ev = NULL;
  grpc_call_error err;
  request_call_stack st;
  VALUE result;
  Data_Get_Struct(self, grpc_rb_server, s);
  if (s->wrapped == NULL) {
    rb_raise(rb_eRuntimeError, "closed!");
    return Qnil;
  } else {
    grpc_request_call_stack_init(&st);
    /* call grpc_server_request_call, then wait for it to complete using
     * pluck_event */
    err = grpc_server_request_call(
        s->wrapped, &call, &st.details, &st.md_ary,
        grpc_rb_get_wrapped_completion_queue(cqueue),
        ROBJECT(tag_new));
    if (err != GRPC_CALL_OK) {
      grpc_request_call_stack_cleanup(&st);
      rb_raise(grpc_rb_eCallError,
              "grpc_server_request_call failed: %s (code=%d)",
               grpc_call_error_detail_of(err), err);
      return Qnil;
    }
    ev = grpc_rb_completion_queue_pluck_event(cqueue, tag_new, timeout);
    if (ev == NULL) {
      grpc_request_call_stack_cleanup(&st);
      return Qnil;
    }
    if (ev->data.op_complete != GRPC_OP_OK) {
      grpc_request_call_stack_cleanup(&st);
      grpc_event_finish(ev);
      rb_raise(grpc_rb_eCallError, "request_call completion failed: (code=%d)",
               ev->data.op_complete);
      return Qnil;
    }

    /* build the NewServerRpc struct result */
    result = rb_struct_new(
        grpc_rb_sNewServerRpc,
        rb_str_new2(st.details.method),
        rb_str_new2(st.details.host),
        rb_funcall(rb_cTime, id_at, 2, INT2NUM(st.details.deadline.tv_sec),
                   INT2NUM(st.details.deadline.tv_nsec)),
        grpc_rb_md_ary_to_h(&st.md_ary),
        grpc_rb_wrap_call(call),
        NULL);
    grpc_event_finish(ev);
    grpc_request_call_stack_cleanup(&st);
    return result;
  }
  return Qnil;
}
Esempio n. 3
0
/* call-seq:
   cq = CompletionQueue.new
   tag = Object.new
   timeout = 10
   server.request_call(cqueue, tag, timeout)

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
        VALUE tag_new, VALUE timeout) {
    grpc_rb_server *s = NULL;
    grpc_call *call = NULL;
    grpc_event ev;
    grpc_call_error err;
    request_call_stack st;
    VALUE result;
    gpr_timespec deadline;
    TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
    if (s->wrapped == NULL) {
        rb_raise(rb_eRuntimeError, "destroyed!");
        return Qnil;
    } else {
        grpc_request_call_stack_init(&st);
        /* call grpc_server_request_call, then wait for it to complete using
         * pluck_event */
        err = grpc_server_request_call(
                  s->wrapped, &call, &st.details, &st.md_ary,
                  grpc_rb_get_wrapped_completion_queue(cqueue),
                  grpc_rb_get_wrapped_completion_queue(cqueue),
                  ROBJECT(tag_new));
        if (err != GRPC_CALL_OK) {
            grpc_request_call_stack_cleanup(&st);
            rb_raise(grpc_rb_eCallError,
                     "grpc_server_request_call failed: %s (code=%d)",
                     grpc_call_error_detail_of(err), err);
            return Qnil;
        }

        ev = grpc_rb_completion_queue_pluck_event(cqueue, tag_new, timeout);
        if (ev.type == GRPC_QUEUE_TIMEOUT) {
            grpc_request_call_stack_cleanup(&st);
            return Qnil;
        }
        if (!ev.success) {
            grpc_request_call_stack_cleanup(&st);
            rb_raise(grpc_rb_eCallError, "request_call completion failed");
            return Qnil;
        }

        /* build the NewServerRpc struct result */
        deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
        result = rb_struct_new(
                     grpc_rb_sNewServerRpc, rb_str_new2(st.details.method),
                     rb_str_new2(st.details.host),
                     rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
                                INT2NUM(deadline.tv_nsec)),
                     grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call), NULL);
        grpc_request_call_stack_cleanup(&st);
        return result;
    }
    return Qnil;
}
Esempio n. 4
0
/* call-seq:
   server.request_call

   Requests notification of a new call on a server. */
static VALUE grpc_rb_server_request_call(VALUE self) {
  grpc_rb_server *s = NULL;
  grpc_call *call = NULL;
  grpc_event ev;
  grpc_call_error err;
  request_call_stack st;
  VALUE result;
  void *tag = (void*)&st;
  grpc_completion_queue *call_queue = grpc_completion_queue_create(NULL);
  gpr_timespec deadline;

  TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
  if (s->wrapped == NULL) {
    rb_raise(rb_eRuntimeError, "destroyed!");
    return Qnil;
  }
  grpc_request_call_stack_init(&st);
  /* call grpc_server_request_call, then wait for it to complete using
   * pluck_event */
  err = grpc_server_request_call(
      s->wrapped, &call, &st.details, &st.md_ary,
      call_queue, s->queue, tag);
  if (err != GRPC_CALL_OK) {
    grpc_request_call_stack_cleanup(&st);
    rb_raise(grpc_rb_eCallError,
             "grpc_server_request_call failed: %s (code=%d)",
             grpc_call_error_detail_of(err), err);
    return Qnil;
  }

  ev = rb_completion_queue_pluck(s->queue, tag,
                                 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  if (!ev.success) {
    grpc_request_call_stack_cleanup(&st);
    rb_raise(grpc_rb_eCallError, "request_call completion failed");
    return Qnil;
  }



  /* build the NewServerRpc struct result */
  deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
  result = rb_struct_new(
      grpc_rb_sNewServerRpc, grpc_rb_slice_to_ruby_string(st.details.method),
      grpc_rb_slice_to_ruby_string(st.details.host),
      rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
                 INT2NUM(deadline.tv_nsec / 1000)),
      grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call, call_queue),
      NULL);
  grpc_request_call_stack_cleanup(&st);
  return result;
}