Example #1
0
static ProtobufCService *
find_proxy (const char **path, cb_info_t **proxy_pt)
{
    ProtobufCService *rpc_client = NULL;
    GList *proxies = NULL;
    GList *iter = NULL;

    *proxy_pt = NULL;

    /* Retrieve a list of proxies for this path */
    proxies = cb_match (&proxy_list, *path,
            CB_MATCH_EXACT|CB_MATCH_WILD|CB_MATCH_CHILD);
    if (!proxies)
        return NULL;

    /* Find the first good proxy */
    for (iter = proxies; iter; iter = g_list_next (iter))
    {
        cb_info_t *proxy = iter->data;
        int len = strlen (proxy->path);

        /* Setup IPC */
        rpc_client = rpc_client_connect (proxy_rpc, proxy->uri);
        if (!rpc_client)
        {
            ERROR ("Invalid PROXY CB %s (%s)\n", proxy->path, proxy->uri);
            cb_destroy (proxy);
            INC_COUNTER (counters.proxied_no_handler);
            continue;
        }
        INC_COUNTER (counters.proxied);
        INC_COUNTER (proxy->count);

        /* Strip proxied path */
        if (proxy->path[len-1] == '*')
            len -= 1;
        if (proxy->path[len-1] == '/')
            len -= 1;
        *path = *path  + len;
        DEBUG ("PROXY CB \"%s\" to \"%s\"\n", *path, proxy->uri);
        *proxy_pt = proxy;
        break;
    }
    g_list_free_full (proxies, (GDestroyNotify) cb_release);
    return rpc_client;
}
Example #2
0
int main(int argc , char ** argv){
	printf("test client begin!\n");	
	rpc_client *client = rpc_client_new();
	rpc_client_connect(client,"127.0.0.1",8778);
	printf("test client started success!");
	
	pointer output;
	size_t len;
	char input[256];
	int i, result;

	rpc_code code;

	for (i = 0; i < 10000; i++) {
		len = sprintf(input, "%d %d", i, i + 1);
		printf("call!\n");
		code = rpc_client_call(client, "TestService", "add", input, strlen(
				input) + 1, &output, &len);
		if (code != RPC_OK) {
			printf("call error %s\n", rpc_code_format(code));
			exit(1);
		}
		result = *(int*) output;
		if (result != (2 * i + 1)) {
			printf("i is %d,result:%d should %d\n", i, result, 2 * i + 1);
			exit(1);
		}
		printf("result:%d\n", result);
	}
	int j;
	for (j = 0; j < 10; j++) {
		for (i = 0; i < 10000; i++) {
			len = sprintf(input, "%d %d", i, i + 1);
			rpc_client_call_async(client, "TestService", "add", strdup(input),
					strlen(input) + 1, cb_add, INT_TO_POINTER(i));
		}
    rpc_sleep(1000);
	}

	printf("test ok\n");
	for (;;) {
		rpc_sleep(1000);
	}
	return EXIT_SUCCESS;
}
Example #3
0
int main()
{
   char host_name[80];
   HNDLE hConn;
   INT status;
   BYTE b;
   WORD w;
   INT i;
   float f;
   double d;

   printf("Remote host name: ");
   ss_gets(host_name, sizeof(host_name));

   /* register this as an RPC client with rpc_list */
   rpc_register_client("MYCLIENT", rpc_list);

   /* connect to RPC server */
   status = rpc_client_connect(host_name, 1750, "", &hConn);
   if (status != RPC_SUCCESS) {
      printf("Cannot connect to RPC server running on %s at port 1750.\n", host_name);
      return 0;
   }

   f = 3.5f;
   d = 4.5;

   /* rpc_mytest just doubles numbers */
   rpc_client_call(hConn, RPC_MYTEST, 1, 2, 3l, f, d, &b, &w, &i, &f, &d);

   printf("\nResult should be:  2 4 6 7.0 9.0\n");
   printf("Result is:         %d %d %d %1.1f %1.1lf\n", b, w, i, f, d);

   printf("\nhit return...");
   b = getchar();

   /* disconnect this client from server */
   rpc_client_disconnect(hConn, FALSE);
   return 1;
}
Example #4
0
int cam_init_rpc(char *host_name, char *exp_name, char *fe_name,
                 char *client_name, char *rpc_server)
{
   INT status, i, size;
   char str[256];
   HNDLE hDB, hKey, hRootKey, hSubkey;

   if (rpc_server[0]) {
      /* connect directly to RPC server, not to MIDAS experiment */
      midas_connect = FALSE;

#ifdef OS_WINNT
      {
         WSADATA WSAData;

         /* Start windows sockets */
         if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
            return RPC_NET_ERROR;
      }
#endif

      rpc_set_name(client_name);
      rpc_register_functions(rpc_get_internal_list(0), NULL);
      rpc_register_functions(rpc_get_internal_list(1), NULL);

      status = rpc_client_connect(rpc_server, 1750, client_name, &hConn);
      if (status != RPC_SUCCESS) {
         printf("Cannot connect to RPC server running on %s at port 1750.\n", rpc_server);
         return status;
      }
   } else {
      /* connect to MIDAS experiment */
      midas_connect = TRUE;

      /* turn off message display, turn on message logging */
      cm_set_msg_print(MT_ALL, 0, NULL);

      /* connect to experiment */
      status = cm_connect_experiment(host_name, exp_name, client_name, 0);
      if (status != CM_SUCCESS)
         return CM_UNDEF_EXP;

      /* connect to the database */
      cm_get_experiment_database(&hDB, &hKey);

      /* find CNAF server if not specified */
      strcpy(str, fe_name);
      if (fe_name[0] == '\0') {
         /* find client which exports CNAF function */
         status = db_find_key(hDB, 0, "System/Clients", &hRootKey);
         if (status == DB_SUCCESS) {
            for (i = 0, status = 0;; i++) {
               status = db_enum_key(hDB, hRootKey, i, &hSubkey);
               if (status == DB_NO_MORE_SUBKEYS) {
                  printf("No client currently exports the CNAF functionality.\n");
                  cm_disconnect_experiment();
                  return CM_UNDEF_EXP;
               }

               sprintf(str, "RPC/%d", RPC_CNAF16);
               status = db_find_key(hDB, hSubkey, str, &hKey);
               if (status == DB_SUCCESS) {
                  size = sizeof(str);
                  db_get_value(hDB, hSubkey, "Name", str, &size, TID_STRING, TRUE);
                  break;
               }
            }
         }
      }

      /* register CNAF_RPC call */
      if (cm_connect_client(str, &hConn) == CM_SUCCESS) {
         DWORD data, size, q, x;

         /* test if CNAF function implemented */
         size = sizeof(WORD);
         status =
             rpc_client_call(hConn, RPC_CNAF16, CNAF_TEST, 0, 0, 0, 0, 0, &data, &size,
                             &q, &x);

         if (status != RPC_SUCCESS) {
            printf("CNAF functionality not implemented by frontend %s\n", fe_name);
            cm_disconnect_client(hConn, FALSE);
            return CM_NO_CLIENT;
         }
      } else {
         printf("Cannot connect to frontend %s\n", fe_name);
         return CM_NO_CLIENT;
      }
   }

   return SUCCESS;
}
Example #5
0
static char *
provide_get (const char *path)
{
    GList *providers = NULL;
    char *value = NULL;
    GList *iter = NULL;

    /* Retrieve a list of providers for this path */
    providers = cb_match (&provide_list, path,
            CB_MATCH_EXACT|CB_MATCH_WILD|CB_MATCH_CHILD|CB_MATCH_WILD_PATH);
    if (!providers)
        return 0;

    /* Find the first good provider */
    for (iter = providers; iter; iter = g_list_next (iter))
    {
        cb_info_t *provider = iter->data;
        ProtobufCService *rpc_client;
        get_data_t data = {0};
        Apteryx__Provide provide = APTERYX__PROVIDE__INIT;

        /* Check for local provider */
        if (provider->id == getpid ())
        {
            apteryx_provide_callback cb = (apteryx_provide_callback) (long) provider->cb;
            DEBUG ("PROVIDE LOCAL \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
                                       provider->path, provider->id, provider->cb);
            value = cb (path);
            break;
        }

        DEBUG ("PROVIDE CB \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
               provider->path, provider->id, provider->cb);

        /* Setup IPC */
        rpc_client = rpc_client_connect (rpc, provider->uri);
        if (!rpc_client)
        {
            /* Throw away the no good validator */
            ERROR ("Invalid PROVIDE CB %s (0x%"PRIx64",0x%"PRIx64")\n",
                   provider->path, provider->id, provider->cb);
            cb_destroy (provider);
            INC_COUNTER (counters.provided_no_handler);
            continue;
        }

        /* Do remote get */
        provide.path = (char *) path;
        provide.id = provider->id;
        provide.cb = provider->cb;
        apteryx__client__provide (rpc_client, &provide, handle_get_response, &data);
        if (!data.done)
        {
            INC_COUNTER (counters.provided_timeout);
            ERROR ("No response from provider for path \"%s\"\n", (char *)path);
            rpc_client_release (rpc, rpc_client, false);
        }
        else
        {
            rpc_client_release (rpc, rpc_client, true);
        }

        /* Result */
        INC_COUNTER (counters.provided);
        INC_COUNTER (provider->count);
        if (data.value)
        {
            value = data.value;
            break;
        }
    }
    g_list_free_full (providers, (GDestroyNotify) cb_release);

    return value;
}
Example #6
0
static void
notify_watchers (const char *path)
{
    GList *watchers = NULL;
    GList *iter = NULL;
    char *value = NULL;
    size_t vsize;

    /* Retrieve a list of watchers for this path */
    watchers = cb_match (&watch_list, path,
            CB_MATCH_EXACT|CB_MATCH_WILD|CB_MATCH_CHILD|CB_MATCH_WILD_PATH);
    if (!watchers)
        return;

    /* Find the new value for this path */
    value = NULL;
    vsize = 0;
    db_get (path, (unsigned char**)&value, &vsize);

    /* Call each watcher */
    for (iter = watchers; iter; iter = g_list_next (iter))
    {
        cb_info_t *watcher = iter->data;
        ProtobufCService *rpc_client;
        protobuf_c_boolean is_done = false;
        Apteryx__Watch watch = APTERYX__WATCH__INIT;

        /* Check for local watcher */
        if (watcher->id == getpid ())
        {
            apteryx_watch_callback cb = (apteryx_watch_callback) (long) watcher->cb;
            DEBUG ("WATCH LOCAL \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
                    watcher->path, watcher->id, watcher->cb);
            cb (path, value);
            continue;
        }

        DEBUG ("WATCH CB %s = %s (%s 0x%"PRIx64",0x%"PRIx64",%s)\n",
                path, value, watcher->path, watcher->id, watcher->cb, watcher->uri);

        /* Setup IPC */
        rpc_client = rpc_client_connect (rpc, watcher->uri);
        if (!rpc_client)
        {
            /* Throw away the no good validator */
            ERROR ("Invalid WATCH CB %s (0x%"PRIx64",0x%"PRIx64")\n",
                   watcher->path, watcher->id, watcher->cb);
            cb_destroy (watcher);
            INC_COUNTER (counters.watched_no_handler);
            continue;
        }

        /* Do remote watch */
        watch.path = (char *)path;
        watch.value = value;
        watch.id = watcher->id;
        watch.cb = watcher->cb;
        apteryx__client__watch (rpc_client, &watch, handle_watch_response, &is_done);
        if (!is_done)
        {
            INC_COUNTER (counters.watched_timeout);
            ERROR ("Failed to notify watcher for path \"%s\"\n", (char *)path);
            rpc_client_release (rpc, rpc_client, false);
        }
        else
        {
            rpc_client_release (rpc, rpc_client, true);
        }

        INC_COUNTER (counters.watched);
        INC_COUNTER (watcher->count);
    }
    g_list_free_full (watchers, (GDestroyNotify) cb_release);

    /* Free memory if allocated */
    if (value)
        g_free (value);
}
Example #7
0
static int
validate_set (const char *path, const char *value)
{
    GList *validators = NULL;
    GList *iter = NULL;
    int32_t result = 0;

    /* Retrieve a list of validators for this path */
    validators = cb_match (&validation_list, path,
            CB_MATCH_EXACT|CB_MATCH_WILD|CB_MATCH_CHILD|CB_MATCH_WILD_PATH);
    if (!validators)
        return 0;

    /* Protect sensitive values with this lock - released in apteryx_set */
    pthread_mutex_lock (&validating);

    /* Call each validator */
    for (iter = validators; iter; iter = g_list_next (iter))
    {
        cb_info_t *validator = iter->data;
        ProtobufCService *rpc_client;
        Apteryx__Validate validate = APTERYX__VALIDATE__INIT;

        /* Check for local validator */
        if (validator->id == getpid ())
        {
            apteryx_watch_callback cb = (apteryx_watch_callback) (long) validator->cb;
            DEBUG ("VALIDATE LOCAL \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
                    validator->path, validator->id, validator->cb);
            cb (path, value);
            continue;
        }

        DEBUG ("VALIDATE CB %s = %s (0x%"PRIx64",0x%"PRIx64")\n",
                 validator->path, value, validator->id, validator->cb);

        /* Setup IPC */
        rpc_client = rpc_client_connect (rpc, validator->uri);
        if (!rpc_client)
        {
            /* Throw away the no good validator */
            ERROR ("Invalid VALIDATE CB %s (0x%"PRIx64",0x%"PRIx64")\n",
                    validator->path, validator->id, validator->cb);
            cb_destroy (validator);
            INC_COUNTER (counters.validated_no_handler);
            continue;
        }

        /* Do remote validate */
        validate.path = (char *)path;
        validate.value = (char *)value;
        validate.id = validator->id;
        validate.cb = validator->cb;
        apteryx__client__validate (rpc_client, &validate, handle_validate_response, &result);
        if (result < 0)
        {
            DEBUG ("Set of %s to %s rejected by process %"PRIu64" (%d)\n",
                    (char *)path, (char*)value, validator->id, result);
            INC_COUNTER (counters.validated_timeout);
            rpc_client_release (rpc, rpc_client, false);
            break;
        }
        else
        {
            rpc_client_release (rpc, rpc_client, true);
        }

        INC_COUNTER (counters.validated);
    }
    g_list_free_full (validators, (GDestroyNotify) cb_release);

    /* This one is fine, but lock is still held */
    return result < 0 ? result : 1;
}
Example #8
0
/* This function returns true if indexers were called (list may still be NULL) */
static bool
index_get (const char *path, GList **result)
{
    GList *indexers = NULL;
    GList *results = NULL;
    GList *iter = NULL;

    /* Retrieve a list of providers for this path */
    indexers = cb_match (&index_list, path,
            CB_MATCH_EXACT|CB_MATCH_WILD|CB_MATCH_CHILD);
    if (!indexers)
    {
        *result = NULL;
        return false;
    }

    /* Find the first good indexer */
    for (iter = indexers; iter; iter = g_list_next (iter))
    {
        cb_info_t *indexer = iter->data;
        ProtobufCService *rpc_client;
        Apteryx__Index index = APTERYX__INDEX__INIT;
        search_data_t data = {0};

        /* Check for local provider */
        if (indexer->id == getpid ())
        {
            apteryx_index_callback cb = (apteryx_index_callback) (long) indexer->cb;
            DEBUG ("INDEX LOCAL \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
                    indexer->path, indexer->id, indexer->cb);
            results = cb (path);
            break;
        }

        DEBUG ("INDEX CB \"%s\" (0x%"PRIx64",0x%"PRIx64")\n",
                indexer->path, indexer->id, indexer->cb);

        /* Setup IPC */
        rpc_client = rpc_client_connect (rpc, indexer->uri);
        if (!rpc_client)
        {
            /* Throw away the no good validator */
            ERROR ("Invalid INDEX CB %s (0x%"PRIx64",0x%"PRIx64")\n",
                    indexer->path, indexer->id, indexer->cb);
            cb_destroy (indexer);
            INC_COUNTER (counters.indexed_no_handler);
            continue;
        }

        /* Do remote get */
        index.path = (char *) path;
        index.id = indexer->id;
        index.cb = indexer->cb;
        apteryx__client__index (rpc_client, &index, handle_search_response, &data);
        if (!data.done)
        {
            INC_COUNTER (counters.indexed_timeout);
            ERROR ("No response from indexer for path \"%s\"\n", (char *)path);
            rpc_client_release (rpc, rpc_client, false);
        }
        else
        {
            rpc_client_release (rpc, rpc_client, true);
        }

        /* Result */
        INC_COUNTER (counters.indexed);
        INC_COUNTER (indexer->count);
        if (data.paths)
        {
            results = data.paths;
            break;
        }
    }
    g_list_free_full (indexers, (GDestroyNotify) cb_release);

    *result = results;
    return true;
}