Ejemplo n.º 1
0
static void sendIncompleteRequest(InspectorServerTest* test, gconstpointer)
{
    GOwnPtr<GError> error;

    // Connect to the inspector server.
    GRefPtr<GSocketClient> client = adoptGRef(g_socket_client_new());
    GRefPtr<GSocketConnection> connection = adoptGRef(g_socket_client_connect_to_host(client.get(), "127.0.0.1", 2999, 0, &error.outPtr()));
    g_assert_no_error(error.get());

    // Send incomplete request (missing blank line after headers) and check if inspector server
    // replies. The server should not reply to an incomplete request and the test should timeout
    // on read.
    GOutputStream* ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection.get()));
    // Request missing blank line after headers.
    const gchar* incompleteRequest = "GET /devtools/page/1 HTTP/1.1\r\nHost: Localhost\r\n";
    g_output_stream_write(ostream, incompleteRequest, strlen(incompleteRequest), 0, &error.outPtr());
    g_assert_no_error(error.get());

    GInputStream* istream = g_io_stream_get_input_stream(G_IO_STREAM(connection.get()));
    char response[16];
    memset(response, 0, sizeof(response));
    GRefPtr<GCancellable> cancel = adoptGRef(g_cancellable_new());
    g_input_stream_read_async(istream, response, sizeof(response) - 1, G_PRIORITY_DEFAULT, cancel.get(), 0, 0);
    // Give a chance for the server to reply.
    test->wait(1);
    g_cancellable_cancel(cancel.get());
    // If we got any answer it means the server replied to an incomplete request, lets fail.
    g_assert(response[0] == '\0');
}
Ejemplo n.º 2
0
// poll if there is a message on port needed to be transmitted or processed.
// port: see localhost:port, socket
// pMsg: both input ArMsg and output ArMsg
boolean ArvfbPoll(ArPortType port,ArMsgType* pMsg)
{
	boolean rv= FALSE;
	GError * error = NULL;

	uint32 command = pMsg->Command;

	/* create a new connection */
	GSocketConnection * connection = NULL;
	GSocketClient * client = g_socket_client_new();

	/* connect to the host */
	connection = g_socket_client_connect_to_host (client,
										   (gchar*)"localhost",
										    port, /* your port goes here */
											NULL,
											&error);

	/* don't forget to check for errors */
	if (error != NULL)
	{
		g_print("SERVER: <%s>!\n",error->message);
		g_object_unref(client);
		return FALSE;
	}

	/* use the connection */
	GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
	g_output_stream_write  (ostream,
						  pMsg,
						  pMsg->Length+MSG_MIN_LENGTH,
						  NULL,
						  &error);
	GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
	gssize size = g_input_stream_read(istream,pMsg,sizeof(ArMsgType),NULL,&error);
	if( (size > MSG_MIN_LENGTH) &&
		((MSG_MIN_LENGTH+pMsg->Length)==size) &&
		((command|MSG_CMD_ACK) == pMsg->Command) )
	{
		rv = TRUE;
	}
	else
	{   // <= MSG_MIN_LENGTH, default nothing has been gotten on the port
		rv = FALSE;
	}
	/* don't forget to check for errors */
	if (error != NULL)
	{
		g_print("SERVER: <%s>!\n",error->message);
	}
	g_object_unref(connection);
	g_object_unref(client);
	return rv;
}
Ejemplo n.º 3
0
gboolean xr_client_open(xr_client_conn* conn, const char* uri, GError** err)
{
  GError* local_err = NULL;

  g_return_val_if_fail(conn != NULL, FALSE);
  g_return_val_if_fail(uri != NULL, FALSE);
  g_return_val_if_fail(!conn->is_open, FALSE);
  g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

  xr_trace(XR_DEBUG_CLIENT_TRACE, "(conn=%p, uri=%s)", conn, uri);

  // parse URI format: http://host:8080/RES
  g_free(conn->host);
  g_free(conn->resource);
  conn->host = NULL;
  conn->resource = NULL;
  if (!_parse_uri(uri, &conn->secure, &conn->host, &conn->resource))
  {
    g_set_error(err, XR_CLIENT_ERROR, XR_CLIENT_ERROR_FAILED, "invalid URI format: %s", uri);
    return FALSE;
  }

  // enable/disable TLS
  if (conn->secure)
  {
    g_socket_client_set_tls(conn->client, TRUE);
    g_socket_client_set_tls_validation_flags(conn->client, G_TLS_CERTIFICATE_VALIDATE_ALL & ~G_TLS_CERTIFICATE_UNKNOWN_CA & ~G_TLS_CERTIFICATE_BAD_IDENTITY);
  }
  else
  {
    g_socket_client_set_tls(conn->client, FALSE);
  }

  conn->conn = g_socket_client_connect_to_host(conn->client, conn->host, 80, NULL, &local_err);
  if (local_err)
  {
    g_propagate_prefixed_error(err, local_err, "Connection failed: ");
    return FALSE;
  }

  xr_set_nodelay(g_socket_connection_get_socket(conn->conn));

  conn->http = xr_http_new(G_IO_STREAM(conn->conn));
  g_free(conn->session_id);
  conn->session_id = g_strdup_printf("%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int());
  conn->is_open = 1;

  xr_client_set_http_header(conn, "X-SESSION-ID", conn->session_id);

  return TRUE;
}
Ejemplo n.º 4
0
GSocketConnection *
xg_socket_client_connect_to_host     (GSocketClient *client,
                                      const gchar *host_and_port,
                                      guint16 default_port)
{
    GSocketConnection *c;
    c = g_socket_client_connect_to_host (client,
                                         host_and_port,
                                         default_port,
                                         NULL,
                                         NULL);
    if (c == NULL)
        g_critical ("g_socket_client_connect_to_host() returned NULL");
    return c;
}
Ejemplo n.º 5
0
MCState *mc_state_new(char *host, int port) {
	GSocketClient *client;
	MCState *state = g_new0(MCState, 1);

	state->in = mc_buf_new();
	state->out = mc_buf_new();

	client = g_socket_client_new();
	state->conn = g_socket_client_connect_to_host(client, host, port, NULL, NULL);
	state->istream = g_io_stream_get_input_stream(G_IO_STREAM(state->conn));
	state->ostream = g_io_stream_get_output_stream(G_IO_STREAM(state->conn));

	g_object_unref(client);

	return state;
}
Ejemplo n.º 6
0
int main (int argc, char *argv[])
{
  GError * error = NULL;
  gchar *str = "Hello server!";

  /* create a new connection */
  GSocketConnection * connection = NULL;
  GSocketClient * client = g_socket_client_new();

  /* connect to the host */
  connection = g_socket_client_connect_to_host (client,
                                               (gchar*)"localhost",
                                                1500, /* your port goes here */
                                                NULL,
                                                &error);

  /* don't forget to check for errors */
  if (error != NULL)
  {
    g_error ("%s", error->message);
  }
  else
  {
    g_print ("Connection successful!\n");
  }

  /* use the connection */
  GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
  GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
  g_output_stream_write  (ostream,
                          str, /* your message goes here */
                          strlen(str) + 1, /* length of your message */
                          NULL,
                          &error);
  /* don't forget to check for errors */
  if (error != NULL)
  {
    g_error ("%s", error->message);
  }

  g_object_unref(client);
  if (connection != NULL)
  {
    g_object_unref(connection);
  }
  return 0;
}
int add_slave_to_control(struct UpDeviceNode *devnode)
{
	GError * error = NULL;
	g_print ("%s\n", __FUNCTION__);

	devnode->user_data.client = g_socket_client_new();
	devnode->user_data.connection = g_socket_client_connect_to_host (devnode->user_data.client, devnode->user_data.ip_addr, CONTROL_PORT, NULL, &error);
	if (error != NULL)
	{
		g_print ("%s\n", error->message);
	}
	else
	{
		g_print ("Connect to %s is successful!\n", devnode->user_data.ip_addr);
	}
	return 0;
}
Ejemplo n.º 8
0
static VALUE
rg_connect_to_host(int argc, VALUE *argv, VALUE self)
{
        VALUE host_and_port, default_port, cancellable;
        GError *error = NULL;
        GSocketConnection *connection;

        rb_scan_args(argc, argv, "21", &host_and_port, &default_port, &cancellable);
        connection = g_socket_client_connect_to_host(_SELF(self),
                                                     RVAL2CSTR(host_and_port),
                                                     RVAL2GUINT16(default_port),
                                                     RVAL2GCANCELLABLE(cancellable),
                                                     &error);
        if (connection == NULL)
                rbgio_raise_error(error);

        return self;
}
Ejemplo n.º 9
0
int
main (int argc, char *argv[])
{
   /* initialize glib */
  //g_type_init ();

  GError * error = NULL;

  /* create a new connection */
  GSocketConnection * connection = NULL;
  GSocketClient * client = g_socket_client_new();

  /* connect to the host */
  connection = g_socket_client_connect_to_host (client,
                                               (gchar*)"localhost",
                                                2345, /* your port goes here */
                                                NULL,
                                                &error);

  /* don't forget to check for errors */
  if (error != NULL)
  {
      g_error (error->message);
  }
  else
  {
      g_print ("Connection successful!\n");
  }

  /* use the connection */
  //GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
  GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
  g_output_stream_write  (ostream,
                          "Hello server!", /* your message goes here */
                          13, /* length of your message */
                          NULL,
                          &error);
  /* don't forget to check for errors */
  if (error != NULL)
  {
      g_error (error->message);
  }
  return 0;
}
Ejemplo n.º 10
0
static gboolean do_connect(MegaHttpClient* http_client, GCancellable* cancellable, GError** err)
{
  GError* local_err = NULL;

  g_return_val_if_fail(MEGA_IS_HTTP_CLIENT(http_client), FALSE);
  g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

  MegaHttpClientPrivate* priv = http_client->priv;

  do_disconnect(http_client);

  // enable/disable TLS
  if (priv->https)
  {
    if (!g_tls_backend_supports_tls(g_tls_backend_get_default())) 
    {
      g_set_error(err, MEGA_HTTP_CLIENT_ERROR, MEGA_HTTP_CLIENT_ERROR_OTHER, "TLS backend not found, please install glib-networking.");
      return FALSE;
    }

    g_socket_client_set_tls(priv->client, TRUE);
  }
  else
  {
    g_socket_client_set_tls(priv->client, FALSE);
  }


  priv->conn = g_socket_client_connect_to_host(priv->client, priv->host, priv->port, cancellable, &local_err);
  if (!priv->conn)
  {
    g_propagate_prefixed_error(err, local_err, "Connection failed: ");
    return FALSE;
  }
  
  GDataInputStream* data_stream = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(http_client->priv->conn)));
  g_data_input_stream_set_newline_type(data_stream, G_DATA_STREAM_NEWLINE_TYPE_ANY);

  priv->istream = G_INPUT_STREAM(data_stream);
  priv->ostream = g_object_ref(g_io_stream_get_output_stream(G_IO_STREAM(http_client->priv->conn)));

  return TRUE;
}
Ejemplo n.º 11
0
int main(int argc, char *argv[]) {
    LibWCRelay *relay = libwc_relay_new();
    GSocketClient *socket_client = g_socket_client_new();
    GSocketConnection *socket_connection;
    gboolean result;
    gchar *ping_result;
    GError *error = NULL;

    if (argc < 2) {
        fprintf(stderr, "Usage: test-client <host[:port]>\n");
        exit(1);
    }

    socket_connection = g_socket_client_connect_to_host(socket_client, argv[1],
                                                        49153, NULL, &error);
    END_IF_FAIL(socket_connection != NULL);

    libwc_relay_password_set(relay, "test");
    libwc_relay_connection_set(relay, G_IO_STREAM(socket_connection),
                               g_socket_connection_get_socket(socket_connection));

    result = libwc_relay_connection_init(relay, NULL, &error);

    if (result)
        printf("Connection successful!\n");
    else {
        fprintf(stderr, "Connection unsuccessful: %s\n",
                error->message);
        exit(1);
    }

    ping_result = libwc_relay_ping(relay, NULL, &error, TEST_PING_MESSAGE);

    if (ping_result && strcmp(ping_result, TEST_PING_MESSAGE) == 0)
        printf("Ping successful!\n");
    else {
        fprintf(stderr, "Ping unsuccessful: %s\n",
                error->message);
        exit(1);
    }
}
static void
create_connection (GSocketConnection ** client_conn,
    GSocketConnection ** server_conn)
{
  ServiceData *data;
  GThread *service_thread;
  GSocketClient *client = g_socket_client_new ();

  data = g_new0 (ServiceData, 1);
  g_mutex_init (&data->mutex);
  g_cond_init (&data->cond);

  service_thread = g_thread_new ("service thread", service_thread_func, data);
  fail_unless (service_thread != NULL);

  /* wait for the service to start */
  g_mutex_lock (&data->mutex);
  while (!data->started) {
    g_cond_wait (&data->cond, &data->mutex);
  }
  g_mutex_unlock (&data->mutex);

  /* create the tcp link */
  *client_conn = g_socket_client_connect_to_host (client, (gchar *) "localhost",
      data->port, NULL, NULL);
  fail_unless (*client_conn != NULL);
  fail_unless (g_socket_connection_is_connected (*client_conn));

  g_thread_join (service_thread);
  *server_conn = data->conn;
  data->conn = NULL;
  fail_unless (g_socket_connection_is_connected (*server_conn));

  g_mutex_clear (&data->mutex);
  g_cond_clear (&data->cond);
  g_free (data);
  g_object_unref (client);
}
Ejemplo n.º 13
0
// forward the message to the port
void ArvfbSend(ArPortType port,const ArMsgType* pMsg)
{
	GError * error = NULL;

	/* create a new connection */
	GSocketConnection * connection = NULL;
	GSocketClient * client = g_socket_client_new();

	/* connect to the host */
	connection = g_socket_client_connect_to_host (client,
										   (gchar*)"localhost",
										    port, /* your port goes here */
											NULL,
											&error);

	/* don't forget to check for errors */
	if (error != NULL)
	{
		g_print("SERVER: <%s>!\n",error->message);
		g_object_unref(client);
		return;
	}
	/* use the connection */
	GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
	g_output_stream_write  (ostream,
						  pMsg,
						  pMsg->Length+MSG_MIN_LENGTH,
						  NULL,
						  &error);
	/* don't forget to check for errors */
	if (error != NULL)
	{
		g_print("SERVER: <%s>!\n",error->message);
	}
	g_object_unref(connection);
	g_object_unref(client);
}
Ejemplo n.º 14
0
void cortrol_service_init()
{
  GError * error = NULL;
  int i;

  for (i = 0; i < SERVER_LIST_NUM; i++)
    {
      /* create a new connection */
      control_service_data[i].client = g_socket_client_new();

      /* connect to the host */
      control_service_data[i].connection = g_socket_client_connect_to_host (control_service_data[i].client,
          control_service_data[i].server_ip, CONTROL_PORT, NULL, &error);
      /* don't forget to check for errors */
      if (error != NULL)
	{
	  g_print ("%s", error->message);
	}
      else
	{
	  g_print ("Connection successful!\n");
	}
    }
}
Ejemplo n.º 15
0
static gboolean
test_lpd_queue (GSocketClient *client,
                gchar         *address,
                gint           port,
                GCancellable  *cancellable,
                gchar         *queue_name)
{
  GSocketConnection *connection;
  gboolean           result = FALSE;
  GError            *error = NULL;

  connection = g_socket_client_connect_to_host (client,
                                                address,
                                                port,
                                                cancellable,
                                                &error);

  if (connection != NULL)
    {
      if (G_IS_TCP_CONNECTION (connection))
        {
          GOutputStream *output;
          GInputStream  *input;
          gssize         bytes_read, bytes_written;
          gchar          buffer[BUFFER_LENGTH];
          gint           length;

          output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
          input = g_io_stream_get_input_stream (G_IO_STREAM (connection));

          /* This LPD command is explained in RFC 1179, section 5.2 */
          length = g_snprintf (buffer, BUFFER_LENGTH, "\2%s\n", queue_name);

          bytes_written = g_output_stream_write (output,
                                                 buffer,
                                                 length,
                                                 NULL,
                                                 &error);

          if (bytes_written != -1)
            {
              bytes_read = g_input_stream_read (input,
                                                buffer,
                                                BUFFER_LENGTH,
                                                NULL,
                                                &error);

              if (bytes_read != -1)
                {
                  if (bytes_read > 0 && buffer[0] == 0)
                    {
                      /* This LPD command is explained in RFC 1179, section 6.1 */
                      length = g_snprintf (buffer, BUFFER_LENGTH, "\1\n");

                      bytes_written = g_output_stream_write (output,
                                                             buffer,
                                                             length,
                                                             NULL,
                                                             &error);

                      result = TRUE;
                    }
                }
              else
                {
                  g_clear_error (&error);
                }
            }
          else
            {
              g_clear_error (&error);
            }
        }

      g_io_stream_close (G_IO_STREAM (connection), NULL, NULL);
      g_object_unref (connection);
    }

  return result;
}
Ejemplo n.º 16
0
int
main (int argc, char *argv[])
{
  GOptionContext *context;
  GSocketClient *client;
  GSocketConnection *connection;
  GSocketAddress *address;
  GCancellable *cancellable;
  GOutputStream *out;
  GError *error = NULL;
  char buffer[1000];

  g_type_init ();

  context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
  g_option_context_add_main_entries (context, cmd_entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      return 1;
    }

  if (argc != 2)
    {
      g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
      return 1;
    }

  if (async)
    loop = g_main_loop_new (NULL, FALSE);

  if (cancel_timeout)
    {
      GThread *thread;
      cancellable = g_cancellable_new ();
      thread = g_thread_new ("cancel", cancel_thread, cancellable);
      g_thread_unref (thread);
    }
  else
    {
      cancellable = NULL;
    }

  client = g_socket_client_new ();
  if (io_timeout)
    g_socket_client_set_timeout (client, io_timeout);
  if (verbose)
    g_signal_connect (client, "event", G_CALLBACK (socket_client_event), NULL);

  if (async)
    {
      GAsyncResult *res;
      g_socket_client_connect_to_host_async (client, argv[1], 7777,
					     cancellable, async_cb, &res);
      g_main_loop_run (loop);
      connection = g_socket_client_connect_to_host_finish (client, res, &error);
      g_object_unref (res);
    }
  else
    {
      connection = g_socket_client_connect_to_host (client,
						    argv[1],
						    7777,
						    cancellable, &error);
    }
  if (connection == NULL)
    {
      g_printerr ("%s can't connect: %s\n", argv[0], error->message);
      return 1;
    }
  g_object_unref (client);

  address = g_socket_connection_get_remote_address (connection, &error);
  if (!address)
    {
      g_printerr ("Error getting remote address: %s\n",
		  error->message);
      return 1;
    }
  g_print ("Connected to address: %s\n",
	   socket_address_to_string (address));
  g_object_unref (address);

  if (graceful)
    g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);

  out = g_io_stream_get_output_stream (G_IO_STREAM (connection));

  while (fgets(buffer, sizeof (buffer), stdin) != NULL)
    {
      /* FIXME if (async) */
      if (!g_output_stream_write_all (out, buffer, strlen (buffer),
				      NULL, cancellable, &error))
	{
	  g_warning ("send error: %s\n",  error->message);
	  g_error_free (error);
	  error = NULL;
	}
    }

  g_print ("closing stream\n");
  if (async)
    {
      GAsyncResult *res;
      g_io_stream_close_async (G_IO_STREAM (connection),
			       0, cancellable, async_cb, &res);
      g_main_loop_run (loop);
      if (!g_io_stream_close_finish (G_IO_STREAM (connection),
				     res, &error))
	{
	  g_object_unref (res);
	  g_warning ("close error: %s\n",  error->message);
	  return 1;
	}
      g_object_unref (res);
    }
  else
    {
      if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
	{
	  g_warning ("close error: %s\n",  error->message);
	  return 1;
	}
    }

  g_object_unref (connection);

  return 0;
}
Ejemplo n.º 17
0
static void
_pp_host_get_lpd_devices_thread (GTask        *task,
                                 gpointer      source_object,
                                 gpointer      task_data,
                                 GCancellable *cancellable)
{
  GSocketConnection *connection;
  PpPrintDevice     *device;
  PpHost            *host = (PpHost *) source_object;
  PpHostPrivate     *priv = host->priv;
  GSocketClient     *client;
  PpDevicesList     *result;
  GSDData           *data = (GSDData *) task_data;
  GError            *error = NULL;
  GList             *candidates = NULL;
  GList             *iter;
  gchar             *found_queue = NULL;
  gchar             *candidate;
  gchar             *address;
  gint               port;
  gint               i;

  if (priv->port == PP_HOST_UNSET_PORT)
    port = PP_HOST_DEFAULT_LPD_PORT;
  else
    port = priv->port;

  address = g_strdup_printf ("%s:%d", priv->hostname, port);
  if (address == NULL || address[0] == '/')
    goto out;

  result = data->devices;
  data->devices = NULL;

  client = g_socket_client_new ();

  connection = g_socket_client_connect_to_host (client,
                                                address,
                                                port,
                                                cancellable,
                                                &error);

  if (connection != NULL)
    {
      g_io_stream_close (G_IO_STREAM (connection), NULL, NULL);
      g_object_unref (connection);

      /* Most of this list is taken from system-config-printer */
      candidates = g_list_append (candidates, g_strdup ("PASSTHRU"));
      candidates = g_list_append (candidates, g_strdup ("AUTO"));
      candidates = g_list_append (candidates, g_strdup ("BINPS"));
      candidates = g_list_append (candidates, g_strdup ("RAW"));
      candidates = g_list_append (candidates, g_strdup ("TEXT"));
      candidates = g_list_append (candidates, g_strdup ("ps"));
      candidates = g_list_append (candidates, g_strdup ("lp"));
      candidates = g_list_append (candidates, g_strdup ("PORT1"));

      for (i = 0; i < 8; i++)
        {
          candidates = g_list_append (candidates, g_strdup_printf ("LPT%d", i));
          candidates = g_list_append (candidates, g_strdup_printf ("LPT%d_PASSTHRU", i));
          candidates = g_list_append (candidates, g_strdup_printf ("COM%d", i));
          candidates = g_list_append (candidates, g_strdup_printf ("COM%d_PASSTHRU", i));
        }

      for (i = 0; i < 50; i++)
        candidates = g_list_append (candidates, g_strdup_printf ("pr%d", i));

      for (iter = candidates; iter != NULL; iter = iter->next)
        {
          candidate = (gchar *) iter->data;

          if (test_lpd_queue (client,
                              address,
                              port,
                              cancellable,
                              candidate))
            {
              found_queue = g_strdup (candidate);
              break;
            }
        }

      if (found_queue != NULL)
        {
          device = g_new0 (PpPrintDevice, 1);
          device->device_class = g_strdup ("network");
          device->device_uri = g_strdup_printf ("lpd://%s:%d/%s",
                                                priv->hostname,
                                                port,
                                                found_queue);
          /* Translators: The found device is a Line Printer Daemon printer */
          device->device_name = g_strdup (_("LPD Printer"));
          device->host_name = g_strdup (priv->hostname);
          device->host_port = port;
          device->acquisition_method = ACQUISITION_METHOD_LPD;

          result->devices = g_list_append (result->devices, device);
        }

      g_list_free_full (candidates, g_free);
    }

  g_object_unref (client);

out:
  g_task_return_pointer (task, result, (GDestroyNotify) pp_devices_list_free);
  g_object_unref (task);

  g_free (address);
}
Ejemplo n.º 18
0
int
main (int argc, char *argv[])
{
   /* initialize glib */
  g_type_init ();

  GError * error = NULL;

  /* create a new connection */
  GSocketConnection *X = NULL, *Y = NULL;
  GSocketClient *clientX = g_socket_client_new();
  GSocketClient *clientY = g_socket_client_new();

  /* connect to the host */
  X = g_socket_client_connect_to_host (clientX,
                                       (gchar*)"localhost",
                                       8516, /* your port goes here */
                                       NULL,
                                       &error);
  Y = g_socket_client_connect_to_host (clientY,
                                       (gchar*)"localhost",
                                       8516, /* your port goes here */
                                       NULL,
                                       &error);

  /* use the connection */
  GInputStream * istreamX = g_io_stream_get_input_stream (G_IO_STREAM (X));
  GOutputStream * ostreamX = g_io_stream_get_output_stream (G_IO_STREAM (X));
  GInputStream * istreamY = g_io_stream_get_input_stream (G_IO_STREAM (Y));
  GOutputStream * ostreamY = g_io_stream_get_output_stream (G_IO_STREAM (Y));

  // A CONNECT message that registers this client under the name XXX
  JzMsg *msg1 = jz_msg_new_connect ("XXX", 0);
  GByteArray *arr1 = jz_msg_to_byte_array (msg1);
  g_output_stream_write  (ostreamX,
                          arr1->data,
                          arr1->len, /* length of your message */
                          NULL,
                          &error);

  JzMsg *msg2 = jz_msg_new_connect ("YYY", 0);
  GByteArray *arr2 = jz_msg_to_byte_array (msg2);
  g_output_stream_write  (ostreamY,
                          arr2->data, arr2->len,
                          NULL,
                          &error);

  g_usleep(100000);

  // A CALL request
  GByteArray *empty_data = g_byte_array_new();
  JzMsg *msg3 = jz_msg_new_call_request (1 /* LCN */,
                                         "XXX" /* Calling address */,
                                         "YYY" /* called adress */,
                                         3, // packet facility
                                         3, // window facility
                                         3, // throughput facility
                                         empty_data);

  GByteArray *arr3 = jz_msg_to_byte_array (msg3);
  g_output_stream_write (ostreamX,
                         arr3->data, arr3->len,
                         NULL,
                         &error);

  
  g_usleep(2 * 1000000);
  
  return 0;
}
Ejemplo n.º 19
0
void lllp_next_song_cb(GObject *listener, GAsyncResult *res, gpointer pstore_library) {
	GtkListStore *store_library = GTK_LIST_STORE(pstore_library);
	gboolean valid = FALSE;
	GSocketConnection *connection;
	GSocketClient *player_client;
	GSocketAddress *address;
	GSocket *socket;
	GInputStream *stream;
	GOutputStream *output_stream;
	GInetAddress *inet_address;
	GInetAddress *player_with_the_song;
	GtkTreeIter iter;
	gchar *player_with_the_song_char;
	struct force_vector_s force_vector;
	gchar *remote_file;
	gint random_number;
	gsize count = 0;
	int i;
	struct force_vector_s rand_force_vector;

	connection = g_socket_listener_accept_finish(G_SOCKET_LISTENER(listener), res, NULL, NULL);
	address = g_socket_connection_get_remote_address(connection, NULL);
	inet_address = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(address));

	socket = g_socket_connection_get_socket(connection);
	stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));

	g_input_stream_read_all(stream, &force_vector, sizeof(force_vector), NULL, NULL, NULL);
	
	valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store_library), &iter);
	while(valid == TRUE) {
		valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store_library), &iter);
		count++;
	}

	gint rand;
	float distance = 100, old_distance = 100;
	float treshold = 0.95;
	float treshold_distance = 4.0;

	do {
		/* TO FUNCTIONALIZE */
		rand = g_random_int_range(0, count);

		if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store_library), &iter)) {
			for(i = 0; i < rand - 1; ++i) {
				gtk_tree_model_iter_next(GTK_TREE_MODEL(store_library), &iter);
			}
			gtk_tree_model_get(GTK_TREE_MODEL(store_library), &iter, FORCE_AMP, &rand_force_vector.amplitude,
				FORCE_ATK, &rand_force_vector.attack, FORCE_FREQ, &rand_force_vector.frequency,
				FORCE_TEMPO, &rand_force_vector.tempo, -1);
		}
		treshold -= 0.01;
		treshold_distance += 0.01;
	}  while((bl_cosine_similarity(rand_force_vector, force_vector) < treshold) ||
		((distance = bl_distance(rand_force_vector, force_vector)) > treshold_distance));
	
	gtk_tree_model_get(GTK_TREE_MODEL(store_library), &iter, AFILE, &remote_file, INET_ADDRESS, &player_with_the_song_char, -1);
	player_with_the_song = g_inet_address_new_from_string(player_with_the_song_char);

	if(g_inet_address_equal(player_with_the_song, inet_address) == TRUE) {
		rand_force_vector.attack = rand_force_vector.frequency = rand_force_vector.tempo = rand_force_vector.amplitude = 0;
		player_client = g_socket_client_new();
		connection = g_socket_client_connect_to_host(player_client, g_inet_address_to_string(inet_address), 5353, NULL, NULL);
		output_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
		g_output_stream_write_all(output_stream, &rand_force_vector, sizeof(rand_force_vector), &count, NULL, NULL);
		g_io_stream_close(G_IO_STREAM(connection), NULL, NULL);

		g_socket_listener_accept_async(G_SOCKET_LISTENER(listener), NULL, lllp_next_song_cb, store_library);
	
		return;
	}
	player_client = g_socket_client_new();
	connection = g_socket_client_connect_to_host(player_client, g_inet_address_to_string(inet_address), 5353, NULL, NULL);
	output_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
	g_output_stream_write_all(output_stream, &rand_force_vector, sizeof(rand_force_vector), &count, NULL, NULL);
	g_io_stream_close(G_IO_STREAM(connection), NULL, NULL);

	player_with_the_song_char = g_inet_address_to_string(player_with_the_song);
	connection = g_socket_client_connect_to_host(player_client, player_with_the_song_char, 19144, NULL, NULL);
	printf("%p\n", connection);
	output_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
	count = strlen(g_inet_address_to_string(inet_address)) + 1;
	g_output_stream_write_all(output_stream, &count, sizeof(gsize), NULL, NULL, NULL);
	g_output_stream_write_all(output_stream, g_inet_address_to_string(inet_address), count, NULL, NULL, NULL);
	count = strlen(remote_file) + 1;
	g_output_stream_write_all(output_stream, &count, sizeof(gsize), NULL, NULL, NULL);
	g_output_stream_write_all(output_stream, remote_file, count, NULL, NULL, NULL);
	g_output_stream_close(output_stream, NULL, NULL);

	g_socket_listener_accept_async(G_SOCKET_LISTENER(listener), NULL, lllp_next_song_cb, store_library);
}
Ejemplo n.º 20
0
static void skk_skk_serv_real_reload (SkkDict* base, GError** error) {
	SkkSkkServ * self;
	GError * _inner_error_ = NULL;
	self = (SkkSkkServ*) base;
	skk_skk_serv_close_connection (self);
	{
		GSocketClient* _tmp0_;
		GSocketClient* client;
		GSocketClient* _tmp1_;
		const gchar* _tmp2_;
		guint16 _tmp3_;
		GSocketConnection* _tmp4_ = NULL;
		GSocketConnection* _tmp5_;
		GSocketConnection* _tmp6_;
		guint8 _tmp7_;
		gsize bytes_written = 0UL;
		GSocketConnection* _tmp8_;
		GOutputStream* _tmp9_;
		GOutputStream* _tmp10_;
		gsize _tmp11_ = 0UL;
		GSocketConnection* _tmp12_;
		GOutputStream* _tmp13_;
		GOutputStream* _tmp14_;
		GSocketConnection* _tmp15_;
		GInputStream* _tmp16_;
		GInputStream* _tmp17_;
		gssize _tmp18_ = 0L;
		gssize len;
		gssize _tmp19_;
		_tmp0_ = g_socket_client_new ();
		client = _tmp0_;
		_tmp1_ = client;
		_tmp2_ = self->priv->host;
		_tmp3_ = self->priv->port;
		_tmp4_ = g_socket_client_connect_to_host (_tmp1_, _tmp2_, _tmp3_, NULL, &_inner_error_);
		_tmp5_ = _tmp4_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (client);
			goto __catch17_g_error;
		}
		_tmp6_ = _g_object_ref0 (_tmp5_);
		_g_object_unref0 (self->priv->connection);
		self->priv->connection = _tmp6_;
		self->priv->buffer[0] = (guint8) '2';
		_tmp7_ = self->priv->buffer[0];
		_tmp8_ = self->priv->connection;
		_tmp9_ = g_io_stream_get_output_stream ((GIOStream*) _tmp8_);
		_tmp10_ = _tmp9_;
		g_output_stream_write_all (_tmp10_, self->priv->buffer + 0, (gsize) (1 - 0), &_tmp11_, NULL, &_inner_error_);
		bytes_written = _tmp11_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (client);
			goto __catch17_g_error;
		}
		_tmp12_ = self->priv->connection;
		_tmp13_ = g_io_stream_get_output_stream ((GIOStream*) _tmp12_);
		_tmp14_ = _tmp13_;
		g_output_stream_flush (_tmp14_, NULL, &_inner_error_);
		if (_inner_error_ != NULL) {
			_g_object_unref0 (client);
			goto __catch17_g_error;
		}
		_tmp15_ = self->priv->connection;
		_tmp16_ = g_io_stream_get_input_stream ((GIOStream*) _tmp15_);
		_tmp17_ = _tmp16_;
		_tmp18_ = g_input_stream_read (_tmp17_, self->priv->buffer, (gsize) 4096, NULL, &_inner_error_);
		len = _tmp18_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (client);
			goto __catch17_g_error;
		}
		_tmp19_ = len;
		if (_tmp19_ <= ((gssize) 0)) {
			skk_skk_serv_close_connection (self);
		}
		_g_object_unref0 (client);
	}
	goto __finally17;
	__catch17_g_error:
	{
		GError* e = NULL;
		const gchar* _tmp20_;
		guint16 _tmp21_;
		GError* _tmp22_;
		const gchar* _tmp23_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp20_ = self->priv->host;
		_tmp21_ = self->priv->port;
		_tmp22_ = e;
		_tmp23_ = _tmp22_->message;
		g_warning ("skkserv.vala:67: can't open skkserv at %s:%u: %s", _tmp20_, (guint) _tmp21_, _tmp23_);
		skk_skk_serv_close_connection (self);
		_g_error_free0 (e);
	}
	__finally17:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
}
static gboolean
run_server (guint * http_port, guint * https_port)
{
  guint port = SOUP_ADDRESS_ANY_PORT;
  guint ssl_port = SOUP_ADDRESS_ANY_PORT;
  const char *ssl_cert_file = GST_TEST_FILES_PATH "/test-cert.pem";
  const char *ssl_key_file = GST_TEST_FILES_PATH "/test-key.pem";
  static int server_running = 0;
  GSocketAddress *address;
  GError *err = NULL;

  SoupAuthDomain *domain = NULL;

  if (server_running)
    return TRUE;

  server_running = 1;

  *http_port = *https_port = 0;

  server = soup_server_new (NULL, NULL);
  if (!server) {
    GST_DEBUG ("Unable to create server");
    return FALSE;
  }
  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
  domain = soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM, realm,
      SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_cb,
      SOUP_AUTH_DOMAIN_ADD_PATH, basic_auth_path, NULL);
  soup_server_add_auth_domain (server, domain);
  g_object_unref (domain);
  domain = soup_auth_domain_digest_new (SOUP_AUTH_DOMAIN_REALM, realm,
      SOUP_AUTH_DOMAIN_DIGEST_AUTH_CALLBACK, digest_auth_cb,
      SOUP_AUTH_DOMAIN_ADD_PATH, digest_auth_path, NULL);
  soup_server_add_auth_domain (server, domain);
  g_object_unref (domain);

  address = g_inet_socket_address_new_from_string ("0.0.0.0", port);
  soup_server_listen (server, address, 0, &err);
  g_object_unref (address);
  if (err) {
    stop_server ();
    g_clear_error (&err);
    return FALSE;
  }

  *http_port = get_port_from_server (server);
  GST_DEBUG ("HTTP server listening on port %u", *http_port);

  if (ssl_cert_file && ssl_key_file) {
    GTlsBackend *backend = g_tls_backend_get_default ();

    if (backend != NULL && g_tls_backend_supports_tls (backend)) {
      ssl_server = soup_server_new (SOUP_SERVER_SSL_CERT_FILE, ssl_cert_file,
          SOUP_SERVER_SSL_KEY_FILE, ssl_key_file, NULL);
    } else {
      GST_INFO ("No TLS support");
    }

    if (ssl_server) {
      GST_INFO ("HTTPS server listening on port %u", *https_port);
      soup_server_add_handler (ssl_server, NULL, server_callback, NULL, NULL);
      address = g_inet_socket_address_new_from_string ("0.0.0.0", ssl_port);
      soup_server_listen (ssl_server, address, SOUP_SERVER_LISTEN_HTTPS, &err);
      g_object_unref (address);

      if (err) {
        GST_ERROR ("Failed to start HTTPS server: %s", err->message);
        stop_server ();
        g_clear_error (&err);
        return FALSE;
      }

      *https_port = get_port_from_server (ssl_server);
      GST_DEBUG ("HTTPS server listening on port %u", *https_port);
    }
  }

  /* check if we can connect to our local http server */
  {
    GSocketConnection *conn;
    GSocketClient *client;

    client = g_socket_client_new ();
    g_socket_client_set_timeout (client, 2);
    conn = g_socket_client_connect_to_host (client, "127.0.0.1", *http_port,
        NULL, NULL);
    if (conn == NULL) {
      GST_INFO ("Couldn't connect to http server 127.0.0.1:%u", *http_port);
      g_object_unref (client);
      stop_server ();
      return FALSE;
    }
    g_object_unref (conn);

    if (ssl_server == NULL)
      goto skip_https_check;

    conn = g_socket_client_connect_to_host (client, "127.0.0.1", *https_port,
        NULL, NULL);
    if (conn == NULL) {
      GST_INFO ("Couldn't connect to https server 127.0.0.1:%u", *https_port);
      g_object_unref (client);
      stop_server ();
      return FALSE;
    }
    g_object_unref (conn);

  skip_https_check:

    g_object_unref (client);
  }

  return TRUE;
}