examples updated;

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

@ -19,18 +19,8 @@
/* define a little bit */
#define DEFAULT_PORT 13133
#define INVALID_PORT 11111
#define INVALID_ADDRESS "192.168.0.0"
#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;
#define CHANNEL_COUNT 1
#define CLIENT_COUNT 2
/*static*/ sexp_t *make_request(const char *req)
{
@ -40,39 +30,87 @@ typedef struct
return sx;
}
/*static*/ void *test_send_message(void *d)
void *test_send_message(void *ch)
{
msg_data_t *data = (msg_data_t *)d;
int i = 0;
for(i = 0; i < ITERATION_COUNT; ++i) {
sxmsg_t *reply = NULL;
clock_t begin = clock();
assert(msg_send(data->channel, data->sx, &reply) == 0);
__DBGLINE;
chnl_t *chnl = (chnl_t *)ch;
char buf[1024];
int a = rand() % 100;
int b = rand() % 100;
sprintf(buf, "(ar-add (%d %d))", a, b);
sexp_t *add_request = make_request(buf);
sxmsg_t *msg = NULL;
int rc = 0;
clock_t start = clock();
rc = msg_send(chnl, add_request, &msg);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("msg_send execution time: %f seconds.\n", time_spent);
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;
msg_data_t msg_data;
msg_data.channel = ch;
msg_data.sx = make_request("(ar_add (1 2 3 4 5))");
pthread_t threads[THREADS_PER_MESSAGE_TYPE];
for(i = 0; i < THREADS_PER_MESSAGE_TYPE; ++i) {
if(pthread_create(&(threads[i]), NULL, test_send_message, &msg_data)) {
printf("---------------=========== Testing... ===========------------------\n");
int i;
pthread_t threads[CHANNEL_COUNT];
for(i = 0; i < CHANNEL_COUNT; ++i) {
if(pthread_create(&(threads[i]), NULL, test_channel, co)) {
fprintf(stderr, "Error creating thread\n");
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);
}
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;
}
@ -154,47 +192,10 @@ int main(int argc, char **argv)
ctx->login = login;
ctx->passwd = password;
int rc = 0;
assert((rc = connection_initiate(co, addr, port, cert, ctx)) == 0);
if(rc) {
fprintf(stderr, "Failed to initiate connection\n");
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;
}
assert(connection_initiate(co, addr, port, cert, ctx) == 0);
assert(start_testing(co) == 0);
assert(test_error_codes(co) == 0);
assert(connection_close(co) == 0);
free(rootca);
free(cert);
@ -205,16 +206,4 @@ int main(int argc, char **argv)
free(addr);
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 */
static int __ar_add(void *data, sexp_t *sx)
static int __ar_add(void *m, sexp_t *sx)
{
sexp_t *lsx = NULL;
int idx;
sexp_t *sx_iter;
sexp_t *lsx = NULL, *sx_iter;
sxmsg_t *msg = (sxmsg_t *)m;
int idx, rc = 0;
if(sexp_list_cdr(sx, &lsx)) {
printf("Invalid protocol\n");
return EINVAL;
@ -89,11 +90,10 @@ static int __ar_add(void *data, sexp_t *sx)
printf("Invalid value type\n");
return EINVAL;
}
sum += atoi(sx->val);
sum += atoi(sx_iter->val);
}
printf("Result = %ld\n", sum);
return 0;
rc = msg_return(msg, sum);
return rc;
}
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");
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;
}
@ -188,6 +191,7 @@ int main(int argc, char **argv)
fprintf(stderr, "Failed to init rpc list\n Failure.\n");
return opt;
}
/* we will add one channel with type id 12 "Demo rpc list" */
opt = sntl_rpclist_add(fulist, 12, "Demo RPC list", NULL);
if(opt) {
@ -203,6 +207,7 @@ int main(int argc, char **argv)
}
opt = sntl_rpclist_add_function(fulist, 12, "ar-multiply", __ar_multiply);
if(opt) goto __fail;
/* ok, setup it */
connections_subsystem_setrpclist_function(__rettlist);

Loading…
Cancel
Save