bool OnelabNetworkClient::connect() { if(_connected) return true; UInt16 bufflen = 1024; int recvlen = 0; UInt8 buff[1024]; OnelabProtocol msg(OnelabProtocol::OnelabStart); #ifdef HAVE_UDT if(_fds) { if(_sockname.size()) unix_socket_connect(_fds, _sockname.c_str()); else _connected = ip4_socket_connect(_fds, _ip) != -1; } else udt_socket_connect(_fdu, _ip); #else if(_sockname.size()) unix_socket_connect(_fds, _sockname.c_str()); else _connected = ip4_socket_connect(_fds, _ip) != -1; #endif if(!_connected) return false; msg.attrs.push_back(new OnelabAttrStart(_name)); recvlen = msg.encodeMsg(buff, bufflen); sendto(buff, recvlen); #ifdef HAVE_UDT if(_fdu)udt_socket_timeout(_fdu, 3); else #endif ip4_socket_timeout(_fds, 3); recvlen = recvfrom(msg); #ifdef HAVE_UDT if(_fdu)udt_socket_timeout(_fdu, -1); else #endif ip4_socket_timeout(_fds, 0); if(recvlen > 0 && msg.msgType() == OnelabProtocol::OnelabStart) _connected = true; return _connected; }
static void connect_event_cb(GIOChannel *chan, GError *conn_err, gpointer data) { struct serial_proxy *prx = data; int sk; if (conn_err) { error("%s", conn_err->message); goto drop; } /* Connect local */ switch (prx->type) { case UNIX_SOCKET_PROXY: sk = unix_socket_connect(prx->address); break; case TTY_PROXY: sk = tty_open(prx->address, &prx->proxy_ti); break; case TCP_SOCKET_PROXY: sk = tcp_socket_connect(prx->address); break; default: sk = -1; } if (sk < 0) goto drop; prx->local = g_io_channel_unix_new(sk); g_io_add_watch(prx->rfcomm, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, forward_data, prx); g_io_add_watch(prx->local, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, forward_data, prx); return; drop: g_io_channel_shutdown(prx->rfcomm, TRUE, NULL); g_io_channel_unref(prx->rfcomm); prx->rfcomm = NULL; }
static void *unix_conn_connect(const char *pathname, void *options) { struct unix_socket_conn *unix_conn; unix_conn = unix_socket_connect(pathname); return unix_conn; }
int main(int argc, char **argv) { char *socketname = get_config_name("control"); /* check if japlay is already running */ if (socketname && file_exists(socketname)) { int fd = unix_socket_connect(socketname); if (fd >= 0) { int i; for (i = 1; i < argc; ++i) { char *path = absolute_path(argv[i]); if (path) { sendto(fd, path, strlen(path), 0, NULL, 0); free(path); } } close(fd); return 0; } /* remove leftover socket */ unlink(socketname); } g_thread_init(NULL); gdk_threads_init(); gdk_threads_enter(); gtk_init(&argc, &argv); main_thread = g_thread_self(); if (japlay_init(&argc, argv)) { error("Can not initialize japlay\n"); return -1; } main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(main_window), APP_NAME); g_signal_connect(G_OBJECT(main_window), "destroy", G_CALLBACK(destroy_cb), NULL); g_signal_connect(G_OBJECT(main_window), "key-press-event", G_CALLBACK(key_pressed_cb), NULL); GtkWidget *menubar = gtk_menu_bar_new(); GtkWidget *file_menu = gtk_menu_new(); GtkWidget *item = gtk_menu_item_new_with_label("New playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(new_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Add directory to the playlist..."); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(add_dir_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Clear playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(clear_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_separator_menu_item_new(); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_check_menu_item_new_with_label("Enable shuffle"); g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(enable_shuffle_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_check_menu_item_new_with_label("Enable automatic volume"); g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(enable_autovol_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); /*item = gtk_menu_item_new_with_label("Scan playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(scan_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item);*/ item = gtk_separator_menu_item_new(); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Quit"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(destroy_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("File"); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), file_menu); gtk_menu_bar_append(GTK_MENU_BAR(menubar), item); static const struct button { const char *stockid; const char *help; void (*const cb)(GtkButton *button, gpointer ptr); } buttons[] = { {GTK_STOCK_MEDIA_PLAY, "Play", play_cb}, {GTK_STOCK_MEDIA_STOP, "stop", stop_cb}, {GTK_STOCK_MEDIA_PAUSE, "Pause", pause_cb}, {GTK_STOCK_MEDIA_NEXT, "Skip to the next song in the queue", next_cb}, {GTK_STOCK_OPEN, "Add files to the playlist", add_cb}, {GTK_STOCK_OK, "Add selected songs to the queue", enqueue_cb}, {GTK_STOCK_DELETE, "Remove selected songs", remove_cb}, {NULL, NULL, NULL} }; GtkWidget *toolbar = gtk_hbox_new(false, 0); int i; for (i = 0; buttons[i].stockid; ++i) { GtkWidget *button = gtk_button_new(); GtkWidget *image = gtk_image_new_from_stock(buttons[i].stockid, GTK_ICON_SIZE_SMALL_TOOLBAR); gtk_button_set_image(GTK_BUTTON(button), image); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttons[i].cb), NULL); gtk_box_pack_start(GTK_BOX(toolbar), button, false, true, 0); gtk_widget_set_tooltip_text(button, buttons[i].help); } scope_area = gtk_drawing_area_new(); gtk_widget_set_size_request(scope_area, SCOPE_WIDTH, -1); gtk_box_pack_start(GTK_BOX(toolbar), scope_area, false, true, 0); g_signal_connect(G_OBJECT(scope_area), "expose_event", G_CALLBACK(expose_event_cb), NULL); seekbar = gtk_hscale_new_with_range(0, 1, 1); gtk_range_set_update_policy(GTK_RANGE(seekbar), GTK_UPDATE_DELAYED); g_signal_connect(G_OBJECT(seekbar), "change-value", G_CALLBACK(seek_cb), NULL); g_signal_connect(G_OBJECT(seekbar), "format-value", G_CALLBACK(format_seek_cb), NULL); notebook = gtk_notebook_new(); GtkWidget *vbox = gtk_vbox_new(false, 0); gtk_box_pack_start(GTK_BOX(vbox), menubar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), toolbar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), seekbar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), notebook, true, true, 0); gtk_container_add(GTK_CONTAINER(main_window), vbox); gtk_widget_show_all(main_window); /* TODO: load all playlists */ main_playlist = new_playlist(); add_playlist_page(main_playlist, "Main"); add_playlist_page(japlay_queue, "Play queue"); add_playlist_page(japlay_history, "History"); char *playlistpath = get_config_name("main_playlist.m3u"); if (playlistpath) load_playlist(main_playlist, playlistpath); char *queuepath = get_config_name("queue.m3u"); if (queuepath) load_playlist(japlay_queue, queuepath); char *settingspath = get_config_name("settings.cfg"); if (settingspath) load_settings(settingspath); for (i = 1; i < argc; ++i) add_file_playlist(main_playlist, argv[i]); signal(SIGINT, handle_sigint); int fd = -1; if (socketname) { fd = unix_socket_create(socketname); if (fd >= 0) { GIOChannel *io = g_io_channel_unix_new(fd); g_io_add_watch(io, G_IO_IN, incoming_client, NULL); } } gtk_main(); if (fd >= 0) unlink(socketname); if (playlistpath) save_playlist_m3u(main_playlist, playlistpath); if (queuepath) save_playlist_m3u(japlay_queue, queuepath); if (settingspath) save_settings(settingspath); japlay_exit(); return 0; }