Exemplo n.º 1
0
int main(int argc, char**argv)
{
  ProtobufCService *service;
  ProtobufC_RPC_Client *client;
  ProtobufC_RPC_AddressType address_type=0;
  const char *name = NULL;
  unsigned i;

  for (i = 1; i < (unsigned) argc; i++)
    {
      if (starts_with (argv[i], "--tcp="))
        {
          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
        usage ();
    }

  if (name == NULL)
    die ("missing --tcp=HOST:PORT or --unix=PATH");
  
  service = protobuf_c_rpc_client_new (address_type, name, &foo__dir_lookup__descriptor, NULL);
  if (service == NULL)
    die ("error creating client");
  client = (ProtobufC_RPC_Client *) service;

  fprintf (stderr, "Connecting... ");
  while (!protobuf_c_rpc_client_is_connected (client))
    protobuf_c_dispatch_run (protobuf_c_dispatch_default ());
  fprintf (stderr, "done.\n");

  for (;;)
    {
      char buf[1024];
      Foo__Name query = FOO__NAME__INIT;
      protobuf_c_boolean is_done = 0;
      fprintf (stderr, ">> ");
      if (fgets (buf, sizeof (buf), stdin) == NULL)
        break;
      if (is_whitespace (buf))
        continue;
      chomp_trailing_whitespace (buf);
      query.name = buf;
      foo__dir_lookup__by_name (service, &query, handle_query_response, &is_done);
      while (!is_done)
        protobuf_c_dispatch_run (protobuf_c_dispatch_default ());
    }
  return 0;
}
Exemplo n.º 2
0
Arquivo: client.c Projeto: JDrit/MemDB
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;
}
Exemplo n.º 3
0
int main (int argc, char ** argv) {
  ProtobufCService *service;
  ProtobufC_RPC_Client * client;
  const char * name = NULL;
  unsigned i;

  // the ping message we will be sending
  Ping ping = PING__INIT;

  for (i = 1; i < (unsigned) argc; i++) {
    if (starts_with (argv[i], "--tcp=")) {
      name = strchr (argv[i], '=') + 1;
    }
  }

  if (name == NULL) {
    fprintf(stderr, "missing --tcp=HOST:PORT");
    return 1;
  }

  // service creates an rpc client, and client is a special cast
  service = protobuf_c_rpc_client_new (PROTOBUF_C_RPC_ADDRESS_TCP, name, &ping_service__descriptor, NULL);
  client = (ProtobufC_RPC_Client *) service;

  fprintf (stderr, "Connecting... ");
  while (!protobuf_c_rpc_client_is_connected (client))
    protobuf_c_dispatch_run (protobuf_c_dispatch_default ());
  fprintf (stderr, "done.\n");

  // create the message
  ping.message = strdup("HELLO WORLD");

  // send it on it's way
  protobuf_c_boolean is_done = 0;
  printf("sending ping... ");
  // handle_ping_response will be called with the resulting PingResponse
  ping_service__reply_to_ping(service, &ping, handle_ping_response, &is_done);

  while (!is_done)
    protobuf_c_dispatch_run (protobuf_c_dispatch_default ());
}
int main(int argc, char **argv)
{
    ProtobufCService *service;
    ProtobufCService *service2;
    ProtobufC_RPC_Client *client;
    ProtobufC_RPC_Client *client2;
    ProtobufC_RPC_AddressType address_type = 0;
    ProtobufC_RPC_AddressType address_type2 = 0;
    const char *name = NULL;
    unsigned i;

    for (i = 1; i < (unsigned) argc; i++) {
	if (starts_with(argv[i], "--tcp=")) {
	    address_type = PROTOBUF_C_RPC_ADDRESS_TCP;
	    name = strchr(argv[i], '=') + 1;
	} else if (starts_with(argv[i], "--unix=")) {
	    address_type2 = PROTOBUF_C_RPC_ADDRESS_LOCAL;
	    name = strchr(argv[i], '=') + 1;
	} else
	    usage();
    }
	
	
	if (name == NULL )
	    die("missing --tcp=HOST:PORT or --unix=PATH");
	service =
	    protobuf_c_rpc_client_new(address_type, name,
				      &foo__dir_lookup__descriptor, NULL);

	if (service == NULL)
	    die("error creating client");
	else
	{
		client = (ProtobufC_RPC_Client *) service;
		fprintf(stderr, "Connecting... ");
		while (!protobuf_c_rpc_client_is_connected(client))
	    		protobuf_c_dispatch_run(protobuf_c_dispatch_default());
		fprintf(stderr, "done.\n");
	}

    int choice = 1;
    char buf[1024];
    char *person, *show_arr;
    for(;;)
    {
    	fprintf(stderr, ">> ");
    	//	fgets(buf, sizeof(buf), stdin)
	if (gets(buf) == NULL)
		break;
    	if (starts_with(buf, "name")) 
    	{
		choice =1;
	    	person = strchr(buf, '=') + 1;
	    	if (is_whitespace(person))
			continue;
	    	chomp_trailing_whitespace(person);
	}
	if(starts_with(buf,"array"))
	{
		choice =0;	
	    	show_arr = strchr(buf, '=') + 1;
	    	if (is_whitespace(show_arr))
			continue;
	    	chomp_trailing_whitespace(show_arr);
	}
	if(choice == 1)
	{
	    choice = -1;
	    Foo__Name query = FOO__NAME__INIT;
	    protobuf_c_boolean is_done = 0;
	    query.name = person;
	    printf("person = %s\n",person);
	    foo__dir_lookup__by_name(service, &query,
				     handle_query_response, &is_done);
	    while (!is_done)
		protobuf_c_dispatch_run(protobuf_c_dispatch_default());
  	 } 
	else if (choice == 0)
	 {
		choice = -1;
	 	Foo__Name2 query = FOO__NAME2__INIT;
	    	protobuf_c_boolean is_done = 0;
		query.name = show_arr;
		printf("show_arr name = %s\n",show_arr);
	    	foo__dir_lookup__by_name2(service, &query,
				       handle_query_response2, &is_done);
	    	while (!is_done) 
	    	{
			protobuf_c_dispatch_run(protobuf_c_dispatch_default());
			//protobuf_c_dispatch_run(protobuf_c_dispatch_default())        
	    	}
	 }
	 else
	 {
		printf("input wrong!\n");	 
	}
    }
    return 0;
}