viking

webkit based web browser for Enlightenment
Log | Files | Refs | LICENSE

commit 9ecc0687e6d8520a7bd237200683db0542617a31
parent a86f816552d8ebb18899a5edcd7e6b2d74b99114
Author: Kyle Milz <kmilz@ucalgary.ca>
Date:   Sat,  6 Oct 2012 17:24:36 -0600

implement a simple connection server

this connection server is spawned on the first run of the browser and
lives the entire time the browser is open.

if other browser instances are started, they will try to connect to the
server and if successful they send either a url (if specified) or
nothing. the long lived server process then spawns a new window with the
(if specified) url.

this makes sure there is only 1 program instance open, making session
saving/restoring sane (to come ...)

Diffstat:
Msrc/main.c | 116++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 107 insertions(+), 9 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -162,6 +162,72 @@ static Searchengine searchengines[] = { { "dd", "https://duckduckgo.com/html/?q=%s&t=Vimprobable" }, }; +static Eina_Bool +_socket_send(void *data, int type, Ecore_Con_Event_Server_Add *ev) +{ + char welcome[] = "http://reddit.com/r/aww"; + + printf("Server with ip %s, name %s, port %d, connected = %d!\n", + ecore_con_server_ip_get(ev->server), + ecore_con_server_name_get(ev->server), + ecore_con_server_port_get(ev->server), + ecore_con_server_connected_get(ev->server)); + ecore_con_server_send(ev->server, welcome, sizeof(welcome)); + ecore_con_server_flush(ev->server); + + ecore_main_loop_quit(); + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_socket_err(void *data, int type, Ecore_Con_Event_Server_Del *ev) +{ + if (!ev->server) + { + printf("Failed to establish connection to the server.\nExiting.\n"); + ecore_main_loop_quit(); + return ECORE_CALLBACK_RENEW; + } + + printf("Lost server with ip %s!\n", ecore_con_server_ip_get(ev->server)); + + ecore_con_server_del(ev->server); + + ecore_main_loop_quit(); + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_socket_receive(void *data, int type, Ecore_Con_Event_Client_Data *ev) +{ + char fmt[128]; + + snprintf(fmt, sizeof(fmt), + "Received %i bytes from client %s port %d:\n" + ">>>>>\n" + "%%.%is\n" + ">>>>>\n", + ev->size, ecore_con_client_ip_get(ev->client), + ecore_con_client_port_get(ev->client), ev->size); + + printf(fmt, ev->data); + + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_server_del(void *data, int type, Ecore_Con_Event_Client_Del *ev) +{ + if (!ev->client) + return ECORE_CALLBACK_RENEW; + + printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client)); + + ecore_con_client_del(ev->client); + + return ECORE_CALLBACK_RENEW; +} + EAPI_MAIN int elm_main(int argc, char *argv[]) { @@ -283,19 +349,51 @@ elm_main(int argc, char *argv[]) ad->keylistroot = make_keyslist(); ad->zoomstep = 0.1f; - /* try to restore session */ - if (config_restore_state_get(config) && session_windows_count(session) > 0 - && session_restore()) { - printf("main() session restored successfully.\n"); + + ecore_con_init(); + + Ecore_Con_Server *svr; + if (!(svr = ecore_con_server_connect(ECORE_CON_LOCAL_USER, "viking", 0, NULL))) { + printf("server not found, creating new one ..\n"); + + if (!(svr = ecore_con_server_add(ECORE_CON_LOCAL_USER, "viking", 0, NULL))) { + printf("could not create server -- this is bad!\n"); + exit(1); + } + + ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, + (Ecore_Event_Handler_Cb)_server_del, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, + (Ecore_Event_Handler_Cb)_socket_receive, NULL); + ecore_con_server_timeout_set(svr, 10); + // ecore_con_server_client_limit_set(svr, 3, 0); + + + /* try to restore session */ + if (config_restore_state_get(config) && session_windows_count(session) > 0 + && session_restore()) { + printf("main() session restored successfully.\n"); + } + else { + Arg arg; + arg.i = DefaultBuf; + window_add(&arg, ad); + } + + elm_run(); } else { - Arg arg; - arg.i = DefaultBuf; - window_add(&arg, ad); + printf("server found, sending url ..\n"); + ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, + (Ecore_Event_Handler_Cb)_socket_send, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, + (Ecore_Event_Handler_Cb)_socket_err, NULL); + + ecore_main_loop_begin(); } - /* main loop */ - elm_run(); + // printf("viking was up for %0.3f seconds\n", ecore_con_server_uptime_get(svr)); + ecore_con_shutdown(); config_save(ad->config, NULL); config_free(ad->config);