Exemple #1
0
void* client_thread(void *args) {
    int sockfd = 0;
    int n = 0;
    uint8_t recvBuff[1024];
    struct sockaddr_in serv_addr;
    memset(recvBuff, '0', sizeof(recvBuff));

    /*
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        handle_error("Error: could not create socket");
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(4000);
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        handle_error("connect");
    }*/


    ProtobufC_RPC_AddressType address_type = PROTOBUF_C_RPC_ADDRESS_TCP;
    ProtobufCService *service = protobuf_c_rpc_client_new(address_type, "localhost:5050", &messages__database__descriptor, NULL);
    ProtobufC_RPC_Client *client = (ProtobufC_RPC_Client *) service;
    //protobuf_c_rpc_client_set_autoreconnect_period (client, 1000);

    log_info("starting to connect...");
    while (!protobuf_c_rpc_client_is_connected(client)) {
    	protobuf_c_rpc_dispatch_run(protobuf_c_rpc_dispatch_default());
        log_info("connecting...");
    }
    log_info("connected");

    Messages__GetRequest request = MESSAGES__GET_REQUEST__INIT;
    request.key = "jd";
    protobuf_c_boolean is_done = 0;
    messages__database__get(service, &request, handle_response, &is_done);
    while (!is_done) {
    	protobuf_c_rpc_dispatch_run(protobuf_c_rpc_dispatch_default());
    }

    return NULL;
}
Exemple #2
0
int main(int argc, char**argv)
{
  ProtobufC_RPC_Server *server;
  ProtobufC_RPC_AddressType address_type=0;
  const char *name = NULL;
  unsigned i;

  for (i = 1; i < (unsigned) argc; i++)
    {
      if (starts_with (argv[i], "--port="))
        {
          address_type = PROTOBUF_C_RPC_ADDRESS_TCP;
          name = strchr (argv[i], '=') + 1;
        }
      else if (starts_with (argv[i], "--unix="))
        {
          address_type = PROTOBUF_C_RPC_ADDRESS_LOCAL;
          name = strchr (argv[i], '=') + 1;
        }
      else if (starts_with (argv[i], "--database="))
        {
          load_database (strchr (argv[i], '=') + 1);
        }
      else
        usage ();
    }

  if (database_size == 0)
    die ("missing --database=FILE (try --database=example.database)");
  if (name == NULL)
    die ("missing --port=NUM or --unix=PATH");
  
  signal (SIGPIPE, SIG_IGN);

  server = protobuf_c_rpc_server_new (address_type, name, (ProtobufCService *) &the_dir_lookup_service, NULL);

  for (;;)
    protobuf_c_rpc_dispatch_run (protobuf_c_rpc_dispatch_default ());
  return 0;
}