//------------------------------------------------------------------------------ int main(int argc, char *argv[]) { GMainLoop *loop; GError * err = NULL; // Sensors Sensors s; s.scan(); // Bluetooth scan // Bluetooth b; b.scan(); #if 0 GIOChannel *channel = g_io_channel_unix_new(b.Handle()); g_io_add_watch(channel, (GIOCondition)(G_IO_IN | G_IO_OUT | G_IO_ERR | G_IO_HUP), onEvent, channel); #endif //b.close(); // socket server // GSocketService * server = g_socket_service_new(); g_socket_listener_add_inet_port((GSocketListener*)server, PORT, NULL, &err); if (err != NULL) { g_error("ERROR: %s", err->message); } g_signal_connect (server, "incoming", G_CALLBACK(onConnection), NULL); g_socket_service_start(server); // mainloop and timer // loop = g_main_loop_new ( NULL , FALSE ); g_timeout_add (5000 , OnTimer , loop); g_main_loop_run (loop); g_main_loop_unref(loop); return 0; }
static void identd_start_server (void) { GError *error = NULL; int enabled, port = 113; if (hextor_get_prefs (ph, "identd", NULL, &enabled) == 3) { if (!enabled) return; } if (hextor_get_prefs (ph, "identd_port", NULL, &port) == 2 && (port <= 0 || port > G_MAXUINT16)) { port = 113; } service = g_socket_service_new (); g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), port, NULL, &error); if (error) { hextor_printf (ph, _("*\tError starting identd server: %s"), error->message); g_object_unref (service); service = NULL; return; } /*hextor_printf (ph, "*\tIdentd listening on port: %d", port); */ g_signal_connect (G_OBJECT (service), "incoming", G_CALLBACK(identd_incoming_cb), NULL); g_socket_service_start (service); }
static gboolean net_createTCPSocket(struct node *n, guint classid) { guint port = 2300+n->classes[classid]; GError * err = NULL; GInetAddress *addr = n->netadr; syslog(LOG_DEBUG,"net_createSockets: Creating tcp socket listener " "on port %u\n", port); GSocketAddress *sa = g_inet_socket_address_new(addr,port); GSocketService *gss = g_socket_service_new(); if( g_socket_listener_add_address(G_SOCKET_LISTENER(gss), sa, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, &err) == FALSE ){ syslog(LOG_WARNING, "net_createSockets: Error while creating" "socket listener: %s\n", err->message); g_error_free(err); g_object_unref(gss); return FALSE; } n->tcpsockets[classid].n = n; n->tcpsockets[classid].socketservice = gss; n->tcpsockets[classid].socketaddress = sa; n->tcpsockets[classid].classid = classid; g_signal_connect(gss, "incoming", G_CALLBACK(tcp_listener), &(n->tcpsockets[classid])); g_socket_service_start(gss); return TRUE; }
GumDukDebugServer * gum_duk_debug_server_new (GumScriptBackend * backend, guint16 base_port) { GumDukDebugServer * server; GSocketService * service; server = g_slice_new (GumDukDebugServer); server->backend = backend; service = g_socket_service_new (); g_signal_connect (service, "incoming", G_CALLBACK (gum_duk_debug_server_on_incoming_connection), server); server->service = service; server->base_port = base_port; server->channels = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref); g_socket_service_start (service); gum_script_backend_set_debug_message_handler (backend, (GumScriptBackendDebugMessageHandler) gum_duk_debug_server_on_message, server, NULL); return server; }
static VALUE socketservice_initialize(VALUE self) { G_INITIALIZE(self, g_socket_service_new()); return Qnil; }
static void test_happy_eyeballs_cancel_delayed (void) { GSocketClient *client; GSocketService *service; GError *error = NULL; guint16 port; GMainLoop *loop; GCancellable *cancel; gboolean got_completed_event = FALSE; /* This just tests that cancellation works as expected, still emits the completed signal, * and never returns a connection */ loop = g_main_loop_new (NULL, FALSE); service = g_socket_service_new (); port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error); g_assert_no_error (error); g_socket_service_start (service); client = g_socket_client_new (); cancel = g_cancellable_new (); g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop); g_timeout_add (1, (GSourceFunc) on_timer, cancel); g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event); g_main_loop_run (loop); g_assert_true (got_completed_event); g_main_loop_unref (loop); g_object_unref (service); g_object_unref (client); g_object_unref (cancel); }
static void test_happy_eyeballs_cancel_instant (void) { GSocketClient *client; GSocketService *service; GError *error = NULL; guint16 port; GMainLoop *loop; GCancellable *cancel; gboolean got_completed_event = FALSE; /* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but * with different timing since it sends an already cancelled cancellable */ loop = g_main_loop_new (NULL, FALSE); service = g_socket_service_new (); port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error); g_assert_no_error (error); g_socket_service_start (service); client = g_socket_client_new (); cancel = g_cancellable_new (); g_cancellable_cancel (cancel); g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop); g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event); g_main_loop_run (loop); g_assert_true (got_completed_event); g_main_loop_unref (loop); g_object_unref (service); g_object_unref (client); g_object_unref (cancel); }
static void test_happy_eyeballs (void) { GSocketClient *client; GSocketService *service; GError *error = NULL; guint16 port; GMainLoop *loop; loop = g_main_loop_new (NULL, FALSE); service = g_socket_service_new (); port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error); g_assert_no_error (error); g_socket_service_start (service); /* All of the magic here actually happens in slow-connect-preload.c * which as you would guess is preloaded. So this is just making a * normal connection that happens to take 600ms each time. This will * trigger the logic to make multiple parallel connections. */ client = g_socket_client_new (); g_socket_client_connect_to_host_async (client, "localhost", port, NULL, on_connected, loop); g_main_loop_run (loop); g_main_loop_unref (loop); g_object_unref (service); g_object_unref (client); }
void cortrol_service_init() { GError * error = NULL; control_service_data.cmd_buf = NULL; /* create the new socketservice */ control_service_data.service = g_socket_service_new (); /* connect to the port */ g_socket_listener_add_inet_port ((GSocketListener*)control_service_data.service, CONTROL_PORT, NULL, &error); /* don't forget to check for errors */ if (error != NULL) { g_error ("%s", error->message); } /* listen to the 'incoming' signal */ g_signal_connect (control_service_data.service, "incoming", G_CALLBACK (incoming_callback), NULL); /* start the socket service */ g_socket_service_start (control_service_data.service); /* enter mainloop */ g_print ("Waiting for client!\n"); }
static void _eventd_evp_start(EventdPluginContext *service) { GList *sockets = NULL; service->service = g_socket_service_new(); if ( service->binds != NULL ) sockets = _eventd_evp_add_socket(sockets, service, (const gchar * const *)service->binds); if ( service->default_bind ) { const gchar *binds[] = { "tcp:" DEFAULT_BIND_PORT_STR, NULL }; sockets = _eventd_evp_add_socket(sockets, service, binds); } #ifdef HAVE_GIO_UNIX if ( service->default_unix ) { const gchar *binds[] = { "unix-runtime:" UNIX_SOCKET, NULL }; sockets = _eventd_evp_add_socket(sockets, service, binds); } #endif /* HAVE_GIO_UNIX */ g_signal_connect(service->service, "incoming", G_CALLBACK(_eventd_service_connection_handler), service); #ifdef ENABLE_AVAHI if ( ! service->no_avahi ) service->avahi = eventd_evp_avahi_start(service->avahi_name, sockets); else #endif /* ENABLE_AVAHI */ g_list_free_full(sockets, g_object_unref); }
static gboolean _j4status_io_add_server(J4statusIOContext *self) { if ( self->server != NULL ) return FALSE; self->server = g_socket_service_new(); g_signal_connect(self->server, "incoming", (GCallback) _j4status_io_server_callback, self); return TRUE; }
static gboolean initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { g_debug (G_STRLOC ": %s", G_STRFUNC); DasomServer *server = DASOM_SERVER (initable); GSocketAddress *address; GError *local_error = NULL; server->listener = G_SOCKET_LISTENER (g_socket_service_new ()); /* server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1)); */ if (g_unix_socket_address_abstract_names_supported ()) address = g_unix_socket_address_new_with_type (server->address, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT); else { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Abstract UNIX domain socket names are not supported."); return FALSE; } g_socket_listener_add_address (server->listener, address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, NULL, &local_error); g_object_unref (address); if (local_error) { g_propagate_error (error, local_error); return FALSE; } server->is_using_listener = TRUE; server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener), "incoming", (GCallback) on_new_connection, server); /* server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener), "run", G_CALLBACK (on_run), server); */ return TRUE; }
void dirserver_init(gchar* baseaddress) { syslog(LOG_DEBUG,"dirserver_init: starting directory server"); services = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); dirserversocket = multicast_createSocket("directoryserver", 2323, &sa); if( socket != NULL){ syslog(LOG_DEBUG,"dirserver_init: socket open"); GSource *source = g_socket_create_source(dirserversocket, G_IO_IN, NULL); ub_assert(source != NULL); g_source_set_callback(source, (GSourceFunc)dirserver_read, NULL, NULL); g_source_attach(source, g_main_context_default()); }else{ syslog(LOG_WARNING, "directory-server.c: warning: could not create socket"); } GError * e = NULL; GInetAddress *net_base = g_inet_address_new_from_string(baseaddress); if( net_base == NULL ){ syslog(LOG_ERR, "dirserver_init: Could not parse base address"); return; } //set up http tcp listener GSocketAddress *httpsa = g_inet_socket_address_new(net_base,8080); syslog(LOG_DEBUG,"dirserver_init: Creating tcp socket on port 8080\n"); GSocketService *gss = g_socket_service_new(); //TODO: save a reference to the gss somewhere if( g_socket_listener_add_address(G_SOCKET_LISTENER(gss), httpsa, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, &e) == FALSE ){ syslog(LOG_WARNING, "dirserver_init: error while creating socket listener: %s\n",e->message); g_error_free(e); } g_signal_connect(gss, "incoming", G_CALLBACK(dirserver_tcp_listener),NULL); g_socket_service_start(gss); g_timeout_add_seconds(1,dirserver_tick,NULL); }
void start(Server* server) { GError * error = NULL; GSocketService * service = g_socket_service_new(); g_socket_listener_add_inet_port((GSocketListener*)service, server->port, /* your port goes here */ NULL, &error); if (error != NULL) { g_error(error->message); } g_signal_connect(service, "incoming", G_CALLBACK(incoming_callback), NULL); g_socket_service_start (service); }
gint net_init(gchar* interface, gchar* baseaddress, gchar *multicastbase) { GError * e = NULL; interface = NULL; //prevent warning net_base = g_inet_address_new_from_string(baseaddress); if( net_base == NULL ){ syslog(LOG_ERR, "net_init: Could not parse base address"); return -1; } struct node n; n.netadr = net_base; n.ubnetd = NULL; syslog(LOG_DEBUG,"creating base address...\n"); interface_createAddress(&n); usleep(3000*1000); syslog(LOG_DEBUG,"done\n"); net_multicastbase = g_inet_address_new_from_string(multicastbase); if( net_multicastbase == NULL ){ syslog(LOG_ERR, "net_init: Could not parse multicast base address"); return -1; } address6_init(net_base, net_multicastbase); tcp_init(); GSocketAddress * sa = g_inet_socket_address_new(net_base,2323); //set up data tcp listener syslog(LOG_DEBUG,"net_init: Creating tcp socket on port 2323\n"); GSocketService *gss = g_socket_service_new(); //TODO: save a reference to the gss somewhere if( g_socket_listener_add_address(G_SOCKET_LISTENER(gss), sa, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, &e) == FALSE ){ syslog(LOG_WARNING, "net_init: error while creating socket listener: %s\n", e->message); g_error_free(e); } g_signal_connect(gss, "incoming", G_CALLBACK(tcp_listener),NULL); g_socket_service_start(gss); g_object_unref(sa); return 0; }
void net_createSockets(struct node *n) { GInetAddress *addr = n->netadr; gchar *tmp = g_inet_address_to_string(addr); syslog(LOG_DEBUG,"net_createSockets: Creating sockets on ip: %s\n",tmp); g_free(tmp); guint i; for(i=0; i<sizeof(n->classes); i++){ if( n->classes[i] != 0 ){ if( !(net_createUDPSocket(n, i) && net_createTCPSocket(n, i)) ) return; } } syslog(LOG_DEBUG,"net_createSockets: Creating tcp management socket listener on port 2324\n"); GError * err = NULL; GSocketAddress * samgt = g_inet_socket_address_new(addr,2324); GSocketService *gss = g_socket_service_new(); if( g_socket_listener_add_address(G_SOCKET_LISTENER(gss), samgt, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, &err) == FALSE ){ syslog(LOG_WARNING, "net_createSockets: Error while creating socket listener: %s\n", err->message); g_error_free(err); g_object_unref(gss); return; } n->mgtsocket.n = n; n->mgtsocket.socketservice = gss; n->mgtsocket.socketaddress = samgt; g_signal_connect(gss, "incoming", G_CALLBACK(tcp_listener), &(n->mgtsocket)); g_socket_service_start(gss); syslog(LOG_DEBUG,"net_createSockets: activating network for this node\n"); avahi_registerServices(n); n->netup = TRUE; }
/** * Register the flare service port */ void create_service(gchar *address, uint16_t port, GCallback *callback, gulong *handler_id) { GSocketService *service = g_socket_service_new(); GInetAddress *inet_address = g_inet_address_new_from_string(address); GSocketAddress *socket_address = g_inet_socket_address_new(inet_address, port); g_socket_listener_add_address( G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); g_object_unref(socket_address); g_object_unref(inet_address); *handler_id = g_signal_connect(service, "incoming", G_CALLBACK(callback), NULL); g_socket_service_start(service); g_assert(g_socket_service_is_active(service)); }
static gpointer service_thread_func (gpointer user_data) { ServiceData *data = user_data; GMainContext *service_context; GSocketService *service; service_context = g_main_context_new (); g_main_context_push_thread_default (service_context); data->loop = g_main_loop_new (service_context, FALSE); /* find available port and start service */ service = g_socket_service_new (); data->port = g_socket_listener_add_any_inet_port ((GSocketListener *) service, NULL, NULL); fail_unless (data->port != 0); /* get notified upon new connection */ g_signal_connect (service, "incoming", G_CALLBACK (incoming_callback), data); g_socket_service_start (service); /* service is started */ g_mutex_lock (&data->mutex); data->started = TRUE; g_cond_signal (&data->cond); g_mutex_unlock (&data->mutex); /* our service will run in the main context of this main loop */ g_main_loop_run (data->loop); g_main_context_pop_thread_default (service_context); g_main_loop_unref (data->loop); data->loop = NULL; return NULL; }
void cortrol_service_init() { GError * error = NULL; control_service_data.cmd_buf = NULL; /* create the new socketservice */ control_service_data.service = g_socket_service_new (); GInetAddress *address = g_inet_address_new_from_string(inet_ntoa(local_ip)); GSocketAddress *socket_address = g_inet_socket_address_new(address, CONTROL_PORT); g_socket_listener_add_address(G_SOCKET_LISTENER(control_service_data.service), socket_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); /* connect to the port */ /*g_socket_listener_add_inet_port ((GSocketListener*)control_service_data.service, CONTROL_PORT, NULL, &error);*/ /* don't forget to check for errors */ if (error != NULL) { g_error ("%s", error->message); } /* listen to the 'incoming' signal */ g_signal_connect (control_service_data.service, "incoming", G_CALLBACK (incoming_callback), NULL); /* start the socket service */ g_socket_service_start (control_service_data.service); /* enter mainloop */ g_print ("Waiting for client! %s\n", inet_ntoa(local_ip)); }
void ArvfbInit(void) { GError * error = NULL; gboolean isOK; ArPortType port = 8000; /* create the new socketservice */ pGSocketService = g_socket_service_new (); do{ /* connect to the port */ isOK = g_socket_listener_add_inet_port ((GSocketListener*)pGSocketService, port, NULL, &error); }while((isOK!=TRUE) && (port<9000)); /* don't forget to check for errors */ if (error != NULL) { Arch_Trace("VFB: Init Error <%s>\n!",error->message); //g_error (error->message); } else { Arch_Trace("VFB: Listener on localhost:%d\n",port); } /* listen to the 'incoming' signal */ g_signal_connect (pGSocketService, "incoming", G_CALLBACK (arvfb_incoming_callback), NULL); /* start the socket service */ g_socket_service_start (pGSocketService); }
static void BP_TCPService_BindListener (BPTCPService *filter) { // Don't bind if we're sink-only g_return_if_fail (filter->ready && !filter->init && filter->mode != BP_MODE_SINK_ONLY); filter->service = g_socket_service_new(); filter->inetAddress = g_inet_address_new_from_string(filter->address->str); filter->socketAddress = g_inet_socket_address_new(filter->inetAddress, filter->port); // add server socket to service and attach listener g_socket_listener_add_address(G_SOCKET_LISTENER(filter->service), filter->socketAddress, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); g_message ("Starting IO service..."); // Connect socket to IO service g_socket_service_start(filter->service); g_message ("IO service is up"); g_message ("Attaching signals ..."); // Pass proxy instance as user data g_signal_connect(filter->service, "incoming", G_CALLBACK(BP_TCPService_IncomingConnection), filter); g_message ("Attached!"); BP_TCPService_ValidateBind (filter); g_message ("Local IP/Port: %s:%u", filter->address->str, filter->port); filter->init = TRUE; }
int main(int argc, char **argv) { int i; //Get process id ProcessID= getpid(); // default password AppPass = defaultAppPass; // try environment variable first, set to default if failed IvyBus = getenv("IVYBUS"); if (IvyBus == NULL) IvyBus = defaultIvyBus; // Parse options for (i = 1; i < argc; ++i) { if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { print_help(); exit(0); } else if (strcmp(argv[i], "-t") == 0) { tcp_port = atoi(argv[++i]); } else if (strcmp(argv[i], "-u") == 0) { udp_port = atoi(argv[++i]); } else if (strcmp(argv[i], "-b") == 0) { IvyBus = argv[++i]; } else if (strcmp(argv[i], "-p") == 0) { AppPass = argv[++i]; } else if (strcmp(argv[i], "-v") == 0) { verbose = 1; } else if (strcmp(argv[i], "-utcp") == 0) { uTCP = 1; } else { printf("App Server: Unknown option\n"); print_help(); exit(0); } } if (verbose) { printf("### Paparazzi App Server ###\n"); printf("Server listen port (TCP) : %d\n", tcp_port); if (uTCP) { printf("Server using TCP communication..\n"); }else{ printf("Server broadcast port (UDP) : %d\n", udp_port); } printf("Control Pass : %s\n", AppPass); printf("Ivy Bus : %s\n", IvyBus); fflush(stdout); } //Create tcp listener #if !GLIB_CHECK_VERSION (2, 35, 1) // init GLib type system (only for older version) g_type_init(); #endif GSocketService *service = g_socket_service_new(); GInetAddress *address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6); //G_SOCKET_FAMILY_IPV4 could be used GSocketAddress *socket_address = g_inet_socket_address_new(address, tcp_port); //Add listener g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); g_object_unref(socket_address); g_object_unref(address); g_socket_service_start(service); //Connect listening signal g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL); //Here comes the ivy bindings IvyInit ("PPRZ_App_Server", "Papparazzi App Server Ready!", NULL, NULL, NULL, NULL); IvyBindMsg(Ivy_All_Msgs, NULL, "(^ground (\\S*) (\\S*) .*)"); IvyBindMsg(on_app_server_NEW_AC, NULL, "ground NEW_AIRCRAFT (\\S*)"); IvyBindMsg(on_app_server_GET_CONFIG, NULL, "(\\S*) ground CONFIG (\\S*) (\\S*) (\\S*) (\\S*) (\\S*) (\\S*) (\\S*)"); IvyBindMsg(on_app_server_AIRCRAFTS, NULL, "(\\S*) ground AIRCRAFTS (\\S*)"); IvyStart(IvyBus); GMainLoop *loop = g_main_loop_new(NULL, FALSE); if (verbose) { printf("Starting App Server\n"); fflush(stdout); } IvySendMsg("app_server"); g_timeout_add(100, request_ac_list, NULL); g_main_loop_run(loop); if (verbose) { printf("Stoping App Server\n"); fflush(stdout); } return 0; }
int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; GMainLoop *loop; GSocketAddress *address; GSocketService *listener; char *path, *base; char *http_address = NULL; int http_port = 0; int display = 1; const GOptionEntry entries[] = { { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" }, { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" }, { NULL } }; context = g_option_context_new ("[:DISPLAY] - broadway display daemon"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("option parsing failed: %s\n", error->message); exit (1); } if (argc > 1) { if (*argv[1] != ':') { g_printerr ("Usage broadwayd [:DISPLAY]\n"); exit (1); } display = strtol(argv[1]+1, NULL, 10); if (display == 0) { g_printerr ("Failed to parse display num %s\n", argv[1]); exit (1); } } if (http_port == 0) http_port = 8080 + (display - 1); server = broadway_server_new (http_address, http_port, &error); if (server == NULL) { g_printerr ("%s\n", error->message); return 1; } base = g_strdup_printf ("broadway%d.socket", display); path = g_build_filename (g_get_user_runtime_dir (), base, NULL); g_free (base); g_print ("Listening on %s\n", path); address = g_unix_socket_address_new_with_type (path, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT); g_free (path); listener = g_socket_service_new (); if (!g_socket_listener_add_address (G_SOCKET_LISTENER (listener), address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, G_OBJECT (server), NULL, &error)) { g_printerr ("Can't listen: %s\n", error->message); return 1; } g_object_unref (address); g_signal_connect (listener, "incoming", G_CALLBACK (incoming_client), NULL); g_socket_service_start (G_SOCKET_SERVICE (listener)); loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); return 0; }
/** * tp_stream_tube_channel_offer_async: * @self: an outgoing #TpStreamTubeChannel * @params: (allow-none) (transfer none): parameters of the tube, or %NULL * @callback: a callback to call when the tube has been offered * @user_data: data to pass to @callback * * Offer an outgoing stream tube. When the tube has been offered, @callback * will be called. You can then call tp_stream_tube_channel_offer_finish() * to get the result of the operation. * * You have to connect to the #TpStreamTubeChannel::incoming signal to get a * #TpStreamTubeConnection each time a contact establishes a connection to * the tube. * * Since: 0.13.2 */ void tp_stream_tube_channel_offer_async (TpStreamTubeChannel *self, GHashTable *params, GAsyncReadyCallback callback, gpointer user_data) { GHashTable *properties; GHashTable *supported_sockets; GError *error = NULL; g_return_if_fail (TP_IS_STREAM_TUBE_CHANNEL (self)); g_return_if_fail (self->priv->result == NULL); g_return_if_fail (tp_channel_get_requested (TP_CHANNEL (self))); if (self->priv->service != NULL) { g_critical ("Can't reoffer Tube!"); return; } self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, tp_stream_tube_channel_offer_async); properties = _tp_channel_get_immutable_properties (TP_CHANNEL (self)); supported_sockets = tp_asv_get_boxed (properties, TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SUPPORTED_SOCKET_TYPES, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP); if (!_tp_set_socket_address_type_and_access_control_type (supported_sockets, &self->priv->socket_type, &self->priv->access_control, &error)) { operation_failed (self, error); g_clear_error (&error); return; } DEBUG ("Using socket type %u with access control %u", self->priv->socket_type, self->priv->access_control); self->priv->service = g_socket_service_new (); switch (self->priv->socket_type) { #ifdef HAVE_GIO_UNIX case TP_SOCKET_ADDRESS_TYPE_UNIX: { self->priv->address = _tp_create_temp_unix_socket ( self->priv->service, &error); /* check there wasn't an error on the final attempt */ if (self->priv->address == NULL) { operation_failed (self, error); g_clear_error (&error); return; } } break; #endif /* HAVE_GIO_UNIX */ case TP_SOCKET_ADDRESS_TYPE_IPV4: case TP_SOCKET_ADDRESS_TYPE_IPV6: { GInetAddress *localhost; GSocketAddress *in_address; localhost = g_inet_address_new_loopback ( self->priv->socket_type == TP_SOCKET_ADDRESS_TYPE_IPV4 ? G_SOCKET_FAMILY_IPV4 : G_SOCKET_FAMILY_IPV6); in_address = g_inet_socket_address_new (localhost, 0); g_socket_listener_add_address ( G_SOCKET_LISTENER (self->priv->service), in_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, &self->priv->address, &error); g_object_unref (localhost); g_object_unref (in_address); if (error != NULL) { operation_failed (self, error); g_clear_error (&error); return; } break; } default: /* should have already errored */ g_assert_not_reached (); break; } tp_g_signal_connect_object (self->priv->service, "incoming", G_CALLBACK (service_incoming_cb), self, 0); g_socket_service_start (self->priv->service); _offer_with_address (self, params); }
int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; GMainLoop *loop; GInetAddress *inet; GSocketAddress *address; GSocketService *listener; char *path, *basename; char *http_address = NULL; char *unixsocket_address = NULL; int http_port = 0; char *ssl_cert = NULL; char *ssl_key = NULL; char *display; int port = 0; const GOptionEntry entries[] = { { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" }, { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" }, #ifdef G_OS_UNIX { "unixsocket", 'u', 0, G_OPTION_ARG_STRING, &unixsocket_address, "Unix domain socket address", "ADDRESS" }, #endif { "cert", 'c', 0, G_OPTION_ARG_STRING, &ssl_cert, "SSL certificate path", "PATH" }, { "key", 'k', 0, G_OPTION_ARG_STRING, &ssl_key, "SSL key path", "PATH" }, { NULL } }; setlocale (LC_ALL, ""); context = g_option_context_new ("[:DISPLAY] - broadway display daemon"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("option parsing failed: %s\n", error->message); exit (1); } display = NULL; if (argc > 1) { if (*argv[1] != ':') { g_printerr ("Usage broadwayd [:DISPLAY]\n"); exit (1); } display = argv[1]; } if (display == NULL) { #ifdef G_OS_UNIX if (g_unix_socket_address_abstract_names_supported ()) display = ":0"; else #endif display = ":tcp"; } if (g_str_has_prefix (display, ":tcp")) { port = strtol (display + strlen (":tcp"), NULL, 10); inet = g_inet_address_new_from_string ("127.0.0.1"); g_print ("Listening on 127.0.0.1:%d\n", port + 9090); address = g_inet_socket_address_new (inet, port + 9090); g_object_unref (inet); } #ifdef G_OS_UNIX else if (display[0] == ':' && g_ascii_isdigit(display[1])) { port = strtol (display + strlen (":"), NULL, 10); basename = g_strdup_printf ("broadway%d.socket", port + 1); path = g_build_filename (g_get_user_runtime_dir (), basename, NULL); g_free (basename); g_print ("Listening on %s\n", path); address = g_unix_socket_address_new_with_type (path, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT); g_free (path); } #endif else { g_printerr ("Failed to parse display %s\n", display); exit (1); } if (http_port == 0) http_port = 8080 + port; if (unixsocket_address != NULL) server = broadway_server_on_unix_socket_new (unixsocket_address, &error); else server = broadway_server_new (http_address, http_port, ssl_cert, ssl_key, &error); if (server == NULL) { g_printerr ("%s\n", error->message); return 1; } listener = g_socket_service_new (); if (!g_socket_listener_add_address (G_SOCKET_LISTENER (listener), address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, G_OBJECT (server), NULL, &error)) { g_printerr ("Can't listen: %s\n", error->message); return 1; } g_object_unref (address); g_signal_connect (listener, "incoming", G_CALLBACK (incoming_client), NULL); g_socket_service_start (G_SOCKET_SERVICE (listener)); loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); return 0; }
static gboolean cockpit_web_server_initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { CockpitWebServer *server = COCKPIT_WEB_SERVER (initable); gboolean ret = FALSE; gboolean failed; int n, fd; server->socket_service = g_socket_service_new (); n = sd_listen_fds (0); if (n > 0) { /* We got file descriptors passed in, use those. */ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { GSocket *s = NULL; gboolean b; s = g_socket_new_from_fd (fd, error); if (s == NULL) { g_prefix_error (error, "Failed to acquire passed socket %i: ", fd); goto out; } b = g_socket_listener_add_socket (G_SOCKET_LISTENER (server->socket_service), s, NULL, error); g_object_unref (s); if (!b) { g_prefix_error (error, "Failed to add listener for socket %i: ", fd); goto out; } } server->socket_activated = TRUE; } else { /* No fds passed in, let's listen on our own. */ if (server->port == 0) { server->port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (server->socket_service), NULL, error); failed = (server->port == 0); } else { failed = !g_socket_listener_add_inet_port (G_SOCKET_LISTENER (server->socket_service), server->port, NULL, error); } if (failed) { g_prefix_error (error, "Failed to bind to port %d: ", server->port); goto out; } } g_signal_connect (server->socket_service, "incoming", G_CALLBACK (on_incoming), server); ret = TRUE; out: return ret; }
int main(int argc, char *argv[]) { GMainLoop* main_loop; GSocketService* socket; GError* error = NULL; GstBus *bus; guint bus_watch_id; config_t * conf; video_server_t* server; guint major = glib_major_version; guint minor = glib_minor_version; guint micro = glib_micro_version; guint nano = 0; fprintf(stderr, "Glib version %d.%d.%d\n", major, minor, micro); /* initialise gsatreamer */ gst_init(&argc, &argv); gst_version(&major, &minor, µ, &nano); fprintf(stderr, "GStreamer version %d.%d.%d (%d)\n", major, minor, micro, nano); conf = config_create(argc, argv); server = video_server_create(conf); main_loop = g_main_loop_new (NULL, FALSE); g_unix_signal_add(SIGHUP, signal_handler, main_loop); g_unix_signal_add(SIGTERM, signal_handler, main_loop); g_unix_signal_add(SIGINT, signal_handler, main_loop); /* Message handler */ bus = gst_pipeline_get_bus(GST_PIPELINE(server->pipeline)); bus_watch_id = gst_bus_add_watch(bus, bus_cb, main_loop); gst_object_unref(bus); /* command socket */ socket = g_socket_service_new(); g_socket_listener_add_inet_port((GSocketListener *) socket, conf->cmd_port, NULL, &error); if (error != NULL) fprintf(stderr, "%s\n", error->message); g_signal_connect(socket, "incoming", G_CALLBACK(socket_callback), server); g_socket_service_start(socket); /* Set the pipeline to "playing" state */ fprintf(stderr, "Video server playing\n"); gst_element_set_state(server->pipeline, GST_STATE_PLAYING); /* Iterate */ fprintf(stderr, "Running...\n"); g_main_loop_run(main_loop); /* Stop */ fprintf(stderr, "Stopping video server\n"); gst_element_set_state(server->pipeline, GST_STATE_NULL); fprintf(stderr, "Deleting pipeline\n"); video_server_delete(server); g_source_remove(bus_watch_id); g_main_loop_unref(main_loop); return 0; }
int main(int argc, char **argv) { int i; // default password AppPass = defaultAppPass; // try environment variable first, set to default if failed IvyBus = getenv("IVYBUS"); if (IvyBus == NULL) IvyBus = defaultIvyBus; // Look for paparazzi folder (PAPARAZZI_HOME or assume local path by default) char* PprzFolder = getenv("PAPARAZZI_HOME"); if (PprzFolder == NULL) PprzFolder = defaultPprzFolder; // Parse options for (i = 1; i < argc; ++i) { if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { print_help(); exit(0); } else if (strcmp(argv[i], "-t") == 0) { tcp_port = atoi(argv[++i]); } else if (strcmp(argv[i], "-u") == 0) { udp_port = atoi(argv[++i]); } else if (strcmp(argv[i], "-b") == 0) { IvyBus = argv[++i]; } else if (strcmp(argv[i], "-p") == 0) { AppPass = argv[++i]; } else if (strcmp(argv[i], "-v") == 0) { verbose = 1; } else { printf("App Server: Unknown option\n"); print_help(); exit(0); } } if (verbose) { printf("### Paparazzi App Server ###\n"); printf("Using Paparazzi Folder : %s\n", PprzFolder); printf("Server listen port (TCP) : %d\n", tcp_port); printf("Server broadcast port (UDP) : %d\n", udp_port); printf("Control Pass : %s\n", AppPass); printf("Ivy Bus : %s\n", IvyBus); fflush(stdout); } //Parse conf.xml parse_ac_data(PprzFolder); //Create tcp listener #if !GLIB_CHECK_VERSION (2, 35, 1) // init GLib type system (only for older version) g_type_init(); #endif GSocketService *service = g_socket_service_new(); GInetAddress *address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6); //G_SOCKET_FAMILY_IPV4 could be used GSocketAddress *socket_address = g_inet_socket_address_new(address, tcp_port); //Add listener g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL); g_object_unref(socket_address); g_object_unref(address); g_socket_service_start(service); //Connect listening signal g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL); //Here comes the ivy bindings IvyInit ("PPRZ_App_Server", "Papparazzi App Server Ready", NULL, NULL, NULL, NULL); IvyBindMsg(Ivy_All_Msgs, NULL, "(^ground .*)"); IvyBindMsg(Ivy_All_Msgs, NULL, "(^\\S* AIRSPEED (\\S*) (\\S*) (\\S*) (\\S*))"); IvyStart(IvyBus); GMainLoop *loop = g_main_loop_new(NULL, FALSE); if (verbose) { printf("Starting App Server\n"); fflush(stdout); } g_main_loop_run(loop); if (verbose) { printf("Stoping App Server\n"); fflush(stdout); } return 0; }
static gboolean cockpit_web_server_initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { CockpitWebServer *server = COCKPIT_WEB_SERVER (initable); gboolean ret = FALSE; gboolean failed = FALSE; int n, fd; server->socket_service = g_socket_service_new (); n = sd_listen_fds (0); if (n > 0) { /* We got file descriptors passed in, use those. */ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { GSocket *s = NULL; gboolean b; int type; socklen_t l = sizeof (type); /* * HACK: Workaround g_error() happy code in GSocket * https://bugzilla.gnome.org/show_bug.cgi?id=746339 */ if (getsockopt (fd, SOL_SOCKET, SO_TYPE, &type, &l) < 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "invalid socket passed via systemd activation: %d: %s", fd, g_strerror (errno)); goto out; } s = g_socket_new_from_fd (fd, error); if (s == NULL) { g_prefix_error (error, "Failed to acquire passed socket %i: ", fd); goto out; } b = cockpit_web_server_add_socket (server, s, error); g_object_unref (s); if (!b) { g_prefix_error (error, "Failed to add listener for socket %i: ", fd); goto out; } } server->socket_activated = TRUE; } else { /* No fds passed in, let's listen on our own. */ if (server->port == 0) { server->port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (server->socket_service), NULL, error); failed = (server->port == 0); } else if (server->port > 0) { failed = !g_socket_listener_add_inet_port (G_SOCKET_LISTENER (server->socket_service), server->port, NULL, error); } if (failed) { g_prefix_error (error, "Failed to bind to port %d: ", server->port); goto out; } } g_signal_connect (server->socket_service, "incoming", G_CALLBACK (on_incoming), server); ret = TRUE; out: return ret; }
int main(int argc, char **argv){ GMainLoop *main_loop; GSocketService *service; GInetAddress *iaddr; GSocketAddress *saddr; AppData *app_data; GError *error = NULL; // Init g_set_prgname(argv[0]); g_mem_set_vtable(glib_mem_profiler_table); g_type_init(); // Start the daemon start_daemon(g_get_prgname(), LOG_LOCAL0); syslog(LOG_NOTICE, "-----------------------------------\n"); syslog(LOG_NOTICE, "Daemon has been started\n"); // atexit(cleanup); // Initialize App Data structire app_data = g_slice_new0(AppData); app_data->queue = g_queue_new(); app_data->hosts = NULL; app_data->running_backups = NULL; // For global access to app_data app_data_aux_ptr = app_data; // Read configuration if (!read_config(app_data)){ syslog(LOG_ERR, "Failed to read config file \"%s\"", CONFIG_FILE); exit(-1); } // If no hosts configured in config file, try reading from MySQL if (g_list_length(app_data->hosts) == 0){ syslog(LOG_NOTICE, "No hosts configured in /etc/busy/busy.conf, now reading hosts from MySQL"); app_data->hosts = db_read_hosts(app_data->mysql); } if (g_list_length(app_data->hosts) == 0){ syslog(LOG_ERR, "Absolutely no hosts configured! Please configure at least one host either in %s or use the Web frontend", CONFIG_FILE); //~ syslog(LOG_ERR, "I won't waste any more cpu time now and exit"); //~ exit(0); } else { GList *p; for (p = app_data->hosts; p != NULL; p = p->next){ Host *host = p->data; host_dump(host); syslog(LOG_NOTICE, "%s %f", host_get_name(host), host_get_max_age(host)); } } // Setup listener for incoming TCP (localhost port 4000) service = g_socket_service_new(); iaddr = g_inet_address_new_from_string("127.0.0.1"); saddr = g_inet_socket_address_new(iaddr, 4000); g_socket_listener_add_address(G_SOCKET_LISTENER(service), saddr, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, NULL, NULL, &error); g_object_unref(iaddr); g_object_unref(saddr); g_socket_service_start(service); g_signal_connect(service, "incoming", (GCallback)on_incoming, app_data); // Call wakeup and archive function once wakeup(app_data); do_archive(app_data); // Check preiodically for backups, queue and archives, g_timeout_add_seconds(WAKEUP_INTERVAL, (GSourceFunc)wakeup, app_data); g_timeout_add_seconds(10, (GSourceFunc)do_backup, app_data); g_timeout_add_seconds(6 * 3600, (GSourceFunc)do_archive, app_data); // Run main loop main_loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(main_loop); return (EXIT_SUCCESS); }