diff options
Diffstat (limited to 'code/test')
-rw-r--r-- | code/test/Makefile | 2 | ||||
-rw-r--r-- | code/test/server.c | 46 | ||||
-rw-r--r-- | code/test/stress.c | 53 |
3 files changed, 51 insertions, 50 deletions
diff --git a/code/test/Makefile b/code/test/Makefile index 7992127..02ade87 100644 --- a/code/test/Makefile +++ b/code/test/Makefile @@ -1,6 +1,6 @@ CFLAGS=-I.. -O3 -Wno-int-to-void-pointer-cast LDFLAGS=-lm -pthread -dep=../libecpcore.a ../crypto/libecpcr.a ../htable/libecpht.a ../posix/libecptr.a ../posix/libecptm.a +dep=../libecpcore.a ../crypto/libecpcr.a ../htable/libecpht.a ../posix/libecptr.a ../posix/libecptm.a ../init.o %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/code/test/server.c b/code/test/server.c index a03267e..5bb36ef 100644 --- a/code/test/server.c +++ b/code/test/server.c @@ -17,11 +17,13 @@ ECPConnHandler handler_c; ECPNode node; ECPConnection conn; -#define PTYPE_MSG 16 +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 -ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_open_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { uint32_t seq = 0; + ecp_conn_handle_open(conn, t, p, s); if (s < 0) { printf("OPEN ERR:%ld\n", s); return 0; @@ -31,52 +33,47 @@ ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize unsigned char *buf = ecp_pld_get_buf(payload); char *msg = "PERA JE CAR!"; + ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); return 0; } -ssize_t handle_msg_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { printf("MSG C:%s size:%ld\n", p, s); - return s; } -ssize_t handle_msg_s(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_s(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { printf("MSG S:%s size:%ld\n", p, s); unsigned char payload[ECP_SIZE_PLD(1000)]; unsigned char *buf = ecp_pld_get_buf(payload); char *msg = "VAISTINU JE CAR!"; + ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); return s; } -int conn_create(ECPConnection *c, unsigned char *p, size_t s) { - c->handler = &handler_s; - return ECP_OK; -} - int main(int argc, char *argv[]) { int rv; rv = ecp_init(&ctx_s); printf("ecp_init RV:%d\n", rv); + rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); printf("ecp_dhkey_generate RV:%d\n", rv); rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_conn_hander_init(&handler_s); - - handler_s.f[PTYPE_MSG] = handle_msg_s; - sock_s.conn_create = conn_create; - rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); printf("ecp_sock_open RV:%d\n", rv); @@ -85,6 +82,11 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx_c); printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_c); + handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; + handler_c.msg[MTYPE_MSG] = handle_msg_c; + ctx_c.handler[CTYPE_TEST] = &handler_c; rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); printf("ecp_dhkey_generate RV:%d\n", rv); @@ -101,16 +103,10 @@ int main(int argc, char *argv[]) { rv = ecp_node_init(&ctx_c, &node, "127.0.0.1:3000", &key_perma_s.public); printf("ecp_node_init RV:%d\n", rv); - rv = ecp_conn_create(&conn, &sock_c); + rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); printf("ecp_conn_create RV:%d\n", rv); - rv = ecp_conn_hander_init(&handler_c); - printf("ecp_conn_hander_init RV:%d\n", rv); - - handler_c.f[ECP_PTYPE_OPEN] = handle_open_c; - handler_c.f[PTYPE_MSG] = handle_msg_c; - - rv = ecp_conn_open(&conn, &node, &handler_c); + rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); while (1) sleep(1); diff --git a/code/test/stress.c b/code/test/stress.c index c278af1..808611e 100644 --- a/code/test/stress.c +++ b/code/test/stress.c @@ -16,7 +16,8 @@ #define NUM_C 256 #define MSG_RATE 65 -#define PTYPE_MSG 16 +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 ECPContext ctx_s; ECPDHKey key_perma_s; @@ -84,7 +85,8 @@ void *sender(ECPConnection *c) { c->sock->ctx->rng(&rnd, sizeof(uint32_t)); usleep(rnd % (2000000/msg_rate)); - ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + ecp_pld_set_type(payload, MTYPE_MSG); + ssize_t _rv = ecp_send(c, payload, sizeof(payload)); if (c_start && (_rv > 0)) { pthread_mutex_lock(&t_mtx[idx]); t_sent[idx]++; @@ -93,9 +95,12 @@ void *sender(ECPConnection *c) { } } -ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { - int idx = (int)(c->conn_data); - int rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)c); +ssize_t handle_open_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { + int idx = (int)(conn->conn_data); + int rv = 0; + + ecp_conn_handle_open(conn, t, p, s); + rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); if (rv) { char msg[256]; sprintf(msg, "THD %d CREATE\n", idx); @@ -105,8 +110,8 @@ ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize return 0; } -ssize_t handle_msg_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { - int idx = (int)(c->conn_data); +ssize_t handle_msg_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { + int idx = (int)(conn->conn_data); unsigned char payload[ECP_SIZE_PLD(1000)]; if (c_start) { @@ -115,21 +120,18 @@ ssize_t handle_msg_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_ pthread_mutex_unlock(&t_mtx[idx]); } - // ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + // ecp_pld_set_type(payload, MTYPE_MSG); + // ssize_t _rv = ecp_send(c, payload, sizeof(payload)); return s; } -ssize_t handle_msg_s(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_s(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { unsigned char payload[ECP_SIZE_PLD(1000)]; - ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + ecp_pld_set_type(payload, MTYPE_MSG); + ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); return s; } -int conn_create(ECPConnection *c, unsigned char *p, size_t s) { - c->handler = &handler_s; - return ECP_OK; -} - int main(int argc, char *argv[]) { char addr[256]; int rv; @@ -165,14 +167,15 @@ int main(int argc, char *argv[]) { sigaction(SIGINFO, &actINFO, NULL); rv = ecp_init(&ctx_s); + if (!rv) rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + if (!rv) rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); - if (!rv) rv = ecp_conn_hander_init(&handler_s); - handler_s.f[PTYPE_MSG] = handle_msg_s; for (i=0; i<num_s; i++) { if (!rv) rv = ecp_sock_create(&sock_s[i], &ctx_s, &key_perma_s); - sock_s[i].conn_create = conn_create; strcpy(addr, "0.0.0.0:"); sprintf(addr+strlen(addr), "%d", 3000+i); if (!rv) rv = ecp_sock_open(&sock_s[i], addr); @@ -187,15 +190,17 @@ int main(int argc, char *argv[]) { } } - rv = ecp_conn_hander_init(&handler_c); + rv = ecp_conn_handler_init(&handler_c); - handler_c.f[ECP_PTYPE_OPEN] = handle_open_c; - handler_c.f[PTYPE_MSG] = handle_msg_c; + handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; + handler_c.msg[MTYPE_MSG] = handle_msg_c; for (i=0; i<num_c; i++) { pthread_mutex_init(&t_mtx[i], NULL); if (!rv) rv = ecp_init(&ctx_c[i]); + ctx_c[i].handler[CTYPE_TEST] = &handler_c; + if (!rv) rv = ecp_dhkey_generate(&ctx_c[i], &key_perma_c[i]); if (!rv) rv = ecp_sock_create(&sock_c[i], &ctx_c[i], &key_perma_c[i]); if (!rv) rv = ecp_sock_open(&sock_c[i], NULL); @@ -203,13 +208,13 @@ int main(int argc, char *argv[]) { if (!rv) rv = pthread_create(&r_thd[i], NULL, (void *(*)(void *))ecp_receiver, (void *)&sock_c[i]); strcpy(addr, "127.0.0.1:"); - sprintf(addr+strlen(addr), "%d", 3000+(i % num_s)); + sprintf(addr+strlen(addr), "%d", 3000 + (i % num_s)); if (!rv) rv = ecp_node_init(&ctx_c[i], &node[i], addr, &key_perma_s.public); - if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i]); + if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i], CTYPE_TEST); conn[i].conn_data = (void *)i; - if (!rv) rv = ecp_conn_open(&conn[i], &node[i], &handler_c); + if (!rv) rv = ecp_conn_open(&conn[i], &node[i]); if (rv) { char msg[256]; |