summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Zaoui <daniel.zaoui@samsung.com>2015-08-03 13:07:20 +0300
committerDaniel Zaoui <daniel.zaoui@samsung.com>2015-08-16 08:18:24 +0300
commitc8ff2a71ffc85da2dd7b2c02336cf25a1a3f5273 (patch)
tree6dbe4bf899eac3e0552e7d4bbea360f7b77c4d65
parentff36ce8987c294bb6ab9cea71a19d7c564002870 (diff)
Improve network infras and make CLI async
-rw-r--r--src/bin/CMakeLists.txt1
-rw-r--r--src/bin/cli.c71
-rw-r--r--src/lib/debug_common.c21
-rw-r--r--src/lib/debug_common.h2
-rw-r--r--src/lib/debug_eo.c4
-rw-r--r--src/lib/edbg_preload.c31
-rw-r--r--src/lib/network.c5
-rw-r--r--src/lib/network.h2
8 files changed, 86 insertions, 51 deletions
diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt
index a5ed8cd..b2a0b2e 100644
--- a/src/bin/CMakeLists.txt
+++ b/src/bin/CMakeLists.txt
@@ -20,6 +20,7 @@ set(EOLIAN_INCLUDE_FLAG -I \"${EOLIAN_EO_DIR}/eo-1\" -I${CMAKE_CURRENT_SOURCE_DI
20add_executable(edbg_cli ${SOURCES}) 20add_executable(edbg_cli ${SOURCES})
21 21
22target_link_libraries(edbg_cli 22target_link_libraries(edbg_cli
23 pthread
23 edbg_common 24 edbg_common
24 ${EINA_LIBRARIES} 25 ${EINA_LIBRARIES}
25 ${EET_LIBRARIES} 26 ${EET_LIBRARIES}
diff --git a/src/bin/cli.c b/src/bin/cli.c
index de14b0b..7f2e819 100644
--- a/src/bin/cli.c
+++ b/src/bin/cli.c
@@ -14,6 +14,11 @@
14static Eina_Hash *vars_hash = NULL; 14static Eina_Hash *vars_hash = NULL;
15static Eina_Bool exit_required = EINA_FALSE; 15static Eina_Bool exit_required = EINA_FALSE;
16 16
17#define MAX_BUF 1000000
18static char *buffer = NULL;
19
20static pthread_t thread;
21
17static const char *good_commands [] = 22static const char *good_commands [] =
18{ 23{
19 "quit", 24 "quit",
@@ -135,14 +140,9 @@ _char_consume(Lexer *l, char c)
135 char *word = _next_word(l, NULL); 140 char *word = _next_word(l, NULL);
136 debug_eo_list_request_pack(word, &buf, &size); 141 debug_eo_list_request_pack(word, &buf, &size);
137 free(word); 142 free(word);
138 debug_pack("LIST_REQ", buf, size, &buf2, &size2); 143 debug_pack("LIST_REQ", "LIST_RESP", buf, size, &buf2, &size2);
139 network_send(-1, buf2, size2); 144 network_send(-1, buf2, size2);
140 free(buf); 145 free(buf);
141 buf = malloc(10000000);
142 int nb = network_receive(buf, 10000000);
143 Eina_List *objs = debug_eo_objects_list(buf, nb);
144 Obj_Info *info;
145 EINA_LIST_FREE(objs, info) printf("Object %p (%s)\n", info->ptr, info->kl_name);
146 146
147 break; 147 break;
148 } 148 }
@@ -216,12 +216,41 @@ _char_consume(Lexer *l, char c)
216 return EINA_TRUE; 216 return EINA_TRUE;
217} 217}
218 218
219static Eina_Bool
220_list_resp_handler(const void *buf, int size, void **buf_ret, int *size_ret)
221{
222 Obj_Info *info;
223 Eina_List *objs = debug_eo_objects_list(buf, size);
224 EINA_LIST_FREE(objs, info) printf("Object %p (%s)\n", info->ptr, info->kl_name);
225 *buf_ret = NULL;
226 *size_ret = 0;
227 return EINA_TRUE;
228}
229
230static void *
231_thread_start(void *arg EINA_UNUSED)
232{
233 while (EINA_TRUE)
234 {
235 int nbBytes = network_receive(-1, buffer, MAX_BUF);
236 if (nbBytes <= 0) return EINA_FALSE;
237 else
238 {
239 void *buf_ret = NULL;
240 int size_ret = 0;
241 if (debug_unpack(buffer, nbBytes, &buf_ret, &size_ret))
242 {
243 if (size_ret) network_send(-1, buf_ret, size_ret);
244 }
245 }
246 }
247 return NULL;
248}
249
219int 250int
220main() 251main()
221{ 252{
222 struct termios oldt, newt; 253 struct termios oldt, newt;
223 int poll = epoll_create(10);
224 struct epoll_event event, *events;
225 254
226 Lexer l; 255 Lexer l;
227 _lexer_reset(&l); 256 _lexer_reset(&l);
@@ -233,30 +262,24 @@ main()
233 newt.c_lflag &= ~(ICANON); 262 newt.c_lflag &= ~(ICANON);
234 tcsetattr(STDIN_FILENO, TCSANOW, &newt); 263 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
235 264
236 event.data.fd = STDIN_FILENO;
237 event.events = EPOLLIN | EPOLLET;
238 epoll_ctl (poll, EPOLL_CTL_ADD, STDIN_FILENO, &event);
239
240 events = calloc(MAX_EVENTS, sizeof(event));
241
242 debug_common_init(); 265 debug_common_init();
243 debug_eo_init(); 266 debug_eo_init();
244 network_init(NETWORK_CLIENT); 267 network_init(NETWORK_CLIENT);
268 debug_opcode_register("LIST_RESP", _list_resp_handler);
269
270 buffer = malloc(MAX_BUF);
271 pthread_create(&thread, NULL, _thread_start, NULL);
272
245 while(!exit_required) 273 while(!exit_required)
246 { 274 {
247 int i, n = epoll_wait(poll, events, MAX_EVENTS, -1); 275 char c = '\0';
248 for (i = 0; i < n; i++) 276 if (read(STDIN_FILENO, &c, 1) == 1)
249 { 277 {
250 if (events[i].data.fd == STDIN_FILENO) 278 _char_consume(&l, c);
251 {
252 char c = '\0';
253 if (read(0, &c, 1) == 1)
254 {
255 _char_consume(&l, c);
256 }
257 }
258 } 279 }
259 } 280 }
281
282 free(buffer);
260 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 283 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
261 284
262 return 0; 285 return 0;
diff --git a/src/lib/debug_common.c b/src/lib/debug_common.c
index c86072a..6582eb5 100644
--- a/src/lib/debug_common.c
+++ b/src/lib/debug_common.c
@@ -6,7 +6,8 @@
6typedef struct 6typedef struct
7{ 7{
8 unsigned int len; 8 unsigned int len;
9 char opcode[50]; 9 char opcode_req[50];
10 char opcode_resp[50];
10} _Eo_Request; 11} _Eo_Request;
11 12
12static Eina_Hash *_opcodes_hash = NULL; 13static Eina_Hash *_opcodes_hash = NULL;
@@ -24,13 +25,14 @@ debug_opcode_register(const char *opcode, Debug_Handler_Cb handler)
24} 25}
25 26
26Eina_Bool 27Eina_Bool
27debug_pack(const char *opcode, void *buf, int size, void **buf_ret, int *size_ret) 28debug_pack(const char *opcode, const char *opcode_resp, void *buf, int size, void **buf_ret, int *size_ret)
28{ 29{
29 _Eo_Request r; 30 _Eo_Request r;
30 int sz_ret = sizeof(r) + size; 31 int sz_ret = sizeof(r) + size;
31 char *_buf = malloc(sz_ret); 32 char *_buf = malloc(sz_ret);
32 r.len = sz_ret; 33 r.len = sz_ret;
33 memcpy(r.opcode, opcode, strlen(opcode) + 1); 34 memcpy(r.opcode_req, opcode, strlen(opcode) + 1);
35 memcpy(r.opcode_resp, opcode_resp, strlen(opcode_resp) + 1);
34 memcpy(_buf, &r, sizeof(r)); 36 memcpy(_buf, &r, sizeof(r));
35 memcpy(_buf + sizeof(r), buf, size); 37 memcpy(_buf + sizeof(r), buf, size);
36 *buf_ret = _buf; 38 *buf_ret = _buf;
@@ -42,8 +44,15 @@ Eina_Bool
42debug_unpack(const void *buf, int size, void *buf_ret, int *size_ret) 44debug_unpack(const void *buf, int size, void *buf_ret, int *size_ret)
43{ 45{
44 const _Eo_Request *r = buf; 46 const _Eo_Request *r = buf;
45 printf("Request %s\n", r->opcode); 47 printf("Opcode %s\n", r->opcode_req);
46 Debug_Handler_Cb handler = eina_hash_find(_opcodes_hash, r->opcode); 48 Debug_Handler_Cb handler = eina_hash_find(_opcodes_hash, r->opcode_req);
47 if (handler) handler((char *)buf + sizeof(*r), size - sizeof(*r), buf_ret, size_ret); 49 if (handler)
50 {
51 void *hbuf = NULL;
52 int hsize = 0;
53 handler((char *)buf + sizeof(*r), size - sizeof(*r), &hbuf, &hsize);
54 if (hsize) debug_pack(r->opcode_resp, "", hbuf, hsize, buf_ret, size_ret);
55 free(hbuf);
56 }
48 return EINA_TRUE; 57 return EINA_TRUE;
49} 58}
diff --git a/src/lib/debug_common.h b/src/lib/debug_common.h
index af2773a..ed019ca 100644
--- a/src/lib/debug_common.h
+++ b/src/lib/debug_common.h
@@ -5,7 +5,7 @@ typedef Eina_Bool (*Debug_Handler_Cb)(const void *buf, int size, void **buf_ret,
5 5
6void debug_common_init(); 6void debug_common_init();
7 7
8Eina_Bool debug_pack(const char *opcode, void *buf, int size, void **buf_ret, int *size_ret); 8Eina_Bool debug_pack(const char *opcode, const char *opcode_resp, void *buf, int size, void **buf_ret, int *size_ret);
9 9
10Eina_Bool debug_unpack(const void *buf, int size, void *buf_ret, int *size_ret); 10Eina_Bool debug_unpack(const void *buf, int size, void *buf_ret, int *size_ret);
11 11
diff --git a/src/lib/debug_eo.c b/src/lib/debug_eo.c
index b1f6cc5..866e46c 100644
--- a/src/lib/debug_eo.c
+++ b/src/lib/debug_eo.c
@@ -34,10 +34,7 @@ _list_req_handle(const void *buf, int size, void **buf_ret, int *size_ret)
34 } 34 }
35 else resp.objs = objs_list; 35 else resp.objs = objs_list;
36 36
37 int sz;
38 *buf_ret = eet_data_descriptor_encode(_list_resp_edd, &resp, size_ret); 37 *buf_ret = eet_data_descriptor_encode(_list_resp_edd, &resp, size_ret);
39 //void *_buf = eet_data_descriptor_encode(_list_resp_edd, &resp, &sz);
40 //debug_pack("LIST_RESP", _buf, sz, buf_ret, size_ret);
41 if (req->classname) eina_list_free(resp.objs); 38 if (req->classname) eina_list_free(resp.objs);
42 39
43 return EINA_TRUE; 40 return EINA_TRUE;
@@ -90,7 +87,6 @@ debug_eo_init()
90 EET_DATA_DESCRIPTOR_ADD_LIST(_list_resp_edd, _List_Response, "objects", objs, _obj_info_edd); 87 EET_DATA_DESCRIPTOR_ADD_LIST(_list_resp_edd, _List_Response, "objects", objs, _obj_info_edd);
91 88
92 debug_opcode_register("LIST_REQ", _list_req_handle); 89 debug_opcode_register("LIST_REQ", _list_req_handle);
93// debug_opcode_register("LIST_RESP", _list_resp_handle);
94} 90}
95 91
96Eina_Bool 92Eina_Bool
diff --git a/src/lib/edbg_preload.c b/src/lib/edbg_preload.c
index e0601f8..7fa205b 100644
--- a/src/lib/edbg_preload.c
+++ b/src/lib/edbg_preload.c
@@ -11,19 +11,21 @@
11#include "debug_eo.h" 11#include "debug_eo.h"
12#include "debug_common.h" 12#include "debug_common.h"
13 13
14#define MAX_BUF 1000000
15static char *buffer = NULL;
16
14static Eina_Bool 17static Eina_Bool
15_buffer_recv(int fd) 18_buffer_recv(int fd)
16{ 19{
17 char buffer[10000]; 20 int nbBytes = network_receive(fd, buffer, MAX_BUF);
18 int nbBytes = recv(fd, buffer, 10000, 0); 21 if (nbBytes <= 0) return EINA_FALSE;
19 if (nbBytes <=0 ) return EINA_FALSE;
20 else 22 else
21 { 23 {
22 void *buf_ret; 24 void *buf_ret;
23 int size_ret; 25 int size_ret;
24 if (debug_unpack(buffer, nbBytes, &buf_ret, &size_ret)) 26 if (debug_unpack(buffer, nbBytes, &buf_ret, &size_ret))
25 { 27 {
26 if (size_ret) send(fd, buf_ret, size_ret, 0); 28 if (size_ret) network_send(fd, buf_ret, size_ret);
27 } 29 }
28 } 30 }
29 return EINA_TRUE; 31 return EINA_TRUE;
@@ -33,12 +35,7 @@ static pthread_t thread;
33static void * 35static void *
34_thread_start(void *arg EINA_UNUSED) 36_thread_start(void *arg EINA_UNUSED)
35{ 37{
36 if (network_init(NETWORK_APP)) 38 network_loop(_buffer_recv);
37 {
38 debug_common_init();
39 debug_eo_init();
40 network_loop(_buffer_recv);
41 }
42 return NULL; 39 return NULL;
43} 40}
44 41
@@ -50,7 +47,13 @@ eo_init(void)
50 if (_eo_init()) 47 if (_eo_init())
51 { 48 {
52 if (_eo_init_counter++ > 0) return EINA_TRUE; 49 if (_eo_init_counter++ > 0) return EINA_TRUE;
53 pthread_create(&thread, NULL, _thread_start, NULL); 50 if (network_init(NETWORK_APP))
51 {
52 debug_common_init();
53 debug_eo_init();
54 buffer = malloc(MAX_BUF);
55 pthread_create(&thread, NULL, _thread_start, NULL);
56 }
54 } 57 }
55 return EINA_FALSE; 58 return EINA_FALSE;
56} 59}
@@ -61,7 +64,11 @@ eo_shutdown(void)
61 Eina_Bool(*_eo_shutdown)() = dlsym(RTLD_NEXT, __func__); 64 Eina_Bool(*_eo_shutdown)() = dlsym(RTLD_NEXT, __func__);
62 if (_eo_shutdown()) 65 if (_eo_shutdown())
63 { 66 {
64 if (--_eo_init_counter == 0) network_shutdown(); 67 if (--_eo_init_counter == 0)
68 {
69 network_shutdown();
70 free(buffer);
71 }
65 return EINA_TRUE; 72 return EINA_TRUE;
66 } 73 }
67 return EINA_FALSE; 74 return EINA_FALSE;
diff --git a/src/lib/network.c b/src/lib/network.c
index c777394..ade5bde 100644
--- a/src/lib/network.c
+++ b/src/lib/network.c
@@ -87,10 +87,9 @@ network_send(int fd, const void *buffer, int len)
87} 87}
88 88
89int 89int
90network_receive(void *buffer, unsigned int max_len) 90network_receive(int fd, void *buffer, unsigned int max_len)
91{ 91{
92 if (type != NETWORK_CLIENT) return -1; 92 return recv(fd == -1 ? mainfd : fd, buffer, max_len, 0);
93 return recv(mainfd, buffer, max_len, 0);
94} 93}
95 94
96void 95void
diff --git a/src/lib/network.h b/src/lib/network.h
index d4f6250..bb594e2 100644
--- a/src/lib/network.h
+++ b/src/lib/network.h
@@ -21,7 +21,7 @@ void
21network_send(int fd, const void *buffer, int len); 21network_send(int fd, const void *buffer, int len);
22 22
23int 23int
24network_receive(void *buffer, unsigned int max_len); 24network_receive(int fd, void *buffer, unsigned int max_len);
25 25
26void 26void
27network_close(int fd); 27network_close(int fd);