examples updated;

v0.5.xx
Alexander Vdolainen 10 years ago
parent 58b3968eed
commit 733ee9f904

@ -19,18 +19,8 @@
/* define a little bit */ /* define a little bit */
#define DEFAULT_PORT 13133 #define DEFAULT_PORT 13133
#define INVALID_PORT 11111 #define CHANNEL_COUNT 1
#define INVALID_ADDRESS "192.168.0.0" #define CLIENT_COUNT 2
#define ITERATION_COUNT 10
#define THREADS_PER_MESSAGE_TYPE 10
typedef struct
{
int type;
chnl_t *channel;
struct timespec *timeout;
sexp_t *sx;
} msg_data_t;
/*static*/ sexp_t *make_request(const char *req) /*static*/ sexp_t *make_request(const char *req)
{ {
@ -40,42 +30,90 @@ typedef struct
return sx; return sx;
} }
/*static*/ void *test_send_message(void *d) void *test_send_message(void *ch)
{ {
msg_data_t *data = (msg_data_t *)d; __DBGLINE;
int i = 0; chnl_t *chnl = (chnl_t *)ch;
for(i = 0; i < ITERATION_COUNT; ++i) { char buf[1024];
sxmsg_t *reply = NULL; int a = rand() % 100;
clock_t begin = clock(); int b = rand() % 100;
assert(msg_send(data->channel, data->sx, &reply) == 0); sprintf(buf, "(ar-add (%d %d))", a, b);
clock_t end = clock(); sexp_t *add_request = make_request(buf);
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; sxmsg_t *msg = NULL;
printf("msg_send execution time: %f seconds.\n", time_spent); int rc = 0;
clock_t start = clock();
rc = msg_send(chnl, add_request, &msg);
clock_t end = clock();
double time = (double)(end - start) / CLOCKS_PER_SEC;
printf("msg_send execution time: %lf. rc = %d\n", time, rc);
assert(rc == a + b);
destroy_sexp(add_request);
return 0x00;
}
void *test_channel(void *ctx)
{
conn_t *co = (conn_t *)ctx;
printf("Testing channel (%ld)\n", pthread_self());
chnl_t *channel = NULL;
int rc = 0;
rc = channel_open(co, &channel, 12);
if(rc) {
printf("Failed to open channel. rc=%d\n", rc);
} }
return NULL; assert(rc == 0);
// test message sending
int i;
pthread_t threads[CLIENT_COUNT];
for(i = 0; i < CLIENT_COUNT; ++i) {
if(pthread_create(&(threads[i]), NULL, test_send_message, channel)) {
fprintf(stderr, "Error creating thread\n");
return 0x00;
}
}
for(i = 0; i < CLIENT_COUNT; ++i) {
pthread_join(threads[i], NULL);
}
rc = channel_close(channel);
if(rc) {
printf("Failed to close channel. rc=%d\n", rc);
}
assert(rc == 0);
return 0x00;
} }
/*static*/ int test_messages(chnl_t *ch) /*static*/ int start_testing(conn_t *co)
{ {
int i = 0; printf("---------------=========== Testing... ===========------------------\n");
msg_data_t msg_data; int i;
msg_data.channel = ch; pthread_t threads[CHANNEL_COUNT];
msg_data.sx = make_request("(ar_add (1 2 3 4 5))"); for(i = 0; i < CHANNEL_COUNT; ++i) {
pthread_t threads[THREADS_PER_MESSAGE_TYPE]; if(pthread_create(&(threads[i]), NULL, test_channel, co)) {
for(i = 0; i < THREADS_PER_MESSAGE_TYPE; ++i) {
if(pthread_create(&(threads[i]), NULL, test_send_message, &msg_data)) {
fprintf(stderr, "Error creating thread\n"); fprintf(stderr, "Error creating thread\n");
return EINVAL; return EINVAL;
} }
printf("created a new thread\n");
} }
for(i = 0; i < THREADS_PER_MESSAGE_TYPE; ++i) { for(i = 0; i < CHANNEL_COUNT; ++i) {
pthread_join(threads[i], NULL); pthread_join(threads[i], NULL);
} }
destroy_sexp(msg_data.sx); printf("---------------=========== Testing end ===========------------------\n");
return 0;
}
/*static*/ int test_error_codes(conn_t *co)
{
printf("---------------=========== Error codes ===========------------------\n");
chnl_t *channel = NULL;
// there is no such channel on the server
assert(channel_open(co, &channel, 777) == EINVAL);
printf("---------------=========== Error codes end ===========------------------\n");
return 0; return 0;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *rootca = NULL, *cert = NULL; char *rootca = NULL, *cert = NULL;
@ -154,48 +192,11 @@ int main(int argc, char **argv)
ctx->login = login; ctx->login = login;
ctx->passwd = password; ctx->passwd = password;
int rc = 0; assert(connection_initiate(co, addr, port, cert, ctx) == 0);
assert((rc = connection_initiate(co, addr, port, cert, ctx)) == 0); assert(start_testing(co) == 0);
if(rc) { assert(test_error_codes(co) == 0);
fprintf(stderr, "Failed to initiate connection\n"); assert(connection_close(co) == 0);
goto __failed;
}
chnl_t *channel = NULL;
//assert(channel_open(co, &channel, 13) == EINVAL);
assert((rc = channel_open(co, &channel, 12)) == 0);
if(rc) {
fprintf(stderr, "Failed to open channel\n");
goto __failed;
}
sexp_t *sx = make_request("(ar_add (1 2 3 4 5))");
sxmsg_t *msg = NULL;
printf("------======== DEBUG ========------\n");
printf("channel pointer: %p\n", channel);
printf("sx pointer: %p\n", sx);
char b[1024];
print_sexp(b, 1024, sx);
printf("sx: %s\n", b);
printf("channel->connection pointer: %p\n", channel->connection);
printf("channel id: %ld - %s\n", channel->cid, channel->uuid);
printf("------======== DEBUG END ========------\n");
rc = msg_send(channel, sx, &msg);
destroy_sexp(sx);
//assert(test_messages(channel) == 0);
assert((rc = channel_close(co)) == 0);
if(rc) {
fprintf(stderr, "Failed to close channel\n");
goto __failed;
}
assert((rc = connection_close(co)) == 0);
if(rc) {
fprintf(stderr, "Failed to close connection\n");
goto __failed;
}
free(rootca); free(rootca);
free(cert); free(cert);
free(co); free(co);
@ -205,16 +206,4 @@ int main(int argc, char **argv)
free(addr); free(addr);
return 0; return 0;
__failed:
fprintf(stderr, "Failed with rc=%d", rc);
if(rootca) free(rootca);
if(cert) free(cert);
if(co) free(co);
if(ctx) free(ctx);
if(password) free(password);
if(login) free(login);
if(addr) free(addr);
return -1;
} }

@ -73,11 +73,12 @@ usrtc_t *__rettlist(conn_t *c)
} }
/* RPC functions implementation */ /* RPC functions implementation */
static int __ar_add(void *data, sexp_t *sx) static int __ar_add(void *m, sexp_t *sx)
{ {
sexp_t *lsx = NULL; sexp_t *lsx = NULL, *sx_iter;
int idx; sxmsg_t *msg = (sxmsg_t *)m;
sexp_t *sx_iter; int idx, rc = 0;
if(sexp_list_cdr(sx, &lsx)) { if(sexp_list_cdr(sx, &lsx)) {
printf("Invalid protocol\n"); printf("Invalid protocol\n");
return EINVAL; return EINVAL;
@ -89,11 +90,10 @@ static int __ar_add(void *data, sexp_t *sx)
printf("Invalid value type\n"); printf("Invalid value type\n");
return EINVAL; return EINVAL;
} }
sum += atoi(sx->val); sum += atoi(sx_iter->val);
} }
printf("Result = %ld\n", sum); rc = msg_return(msg, sum);
return rc;
return 0;
} }
static int __ar_multiply(void *data, sexp_t *sx) static int __ar_multiply(void *data, sexp_t *sx)
@ -112,9 +112,12 @@ static int __ar_multiply(void *data, sexp_t *sx)
printf("Invalid value type\n"); printf("Invalid value type\n");
return EINVAL; return EINVAL;
} }
mult *= atoi(sx->val); mult *= atoi(sx_iter->val);
} }
printf("Result = %ld\n", mult); char buf[128];
sprintf(buf, "(ar_mult_set (%ld))", mult);
printf("result %s\n", buf);
return 0; return 0;
} }
@ -188,6 +191,7 @@ int main(int argc, char **argv)
fprintf(stderr, "Failed to init rpc list\n Failure.\n"); fprintf(stderr, "Failed to init rpc list\n Failure.\n");
return opt; return opt;
} }
/* we will add one channel with type id 12 "Demo rpc list" */ /* we will add one channel with type id 12 "Demo rpc list" */
opt = sntl_rpclist_add(fulist, 12, "Demo RPC list", NULL); opt = sntl_rpclist_add(fulist, 12, "Demo RPC list", NULL);
if(opt) { if(opt) {
@ -203,6 +207,7 @@ int main(int argc, char **argv)
} }
opt = sntl_rpclist_add_function(fulist, 12, "ar-multiply", __ar_multiply); opt = sntl_rpclist_add_function(fulist, 12, "ar-multiply", __ar_multiply);
if(opt) goto __fail; if(opt) goto __fail;
/* ok, setup it */ /* ok, setup it */
connections_subsystem_setrpclist_function(__rettlist); connections_subsystem_setrpclist_function(__rettlist);

Loading…
Cancel
Save