Some more TcpClient stuff
[cascardo/gnio.git] / gnio / gtcpclient.c
index 4898399..fc03627 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
-#include <gio/gasynchelper.h>
 
 #include <string.h>
 #include <errno.h>
 
-#include "ginetaddress.h"
-#include "ginet4address.h"
-#include "ginet6address.h"
-#include "gsocket.h"
 #include "gtcpclient.h"
-#include "gnioerror.h"
-#include "ginetsocketaddress.h"
+#include "gasynchelper.h"
 
 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
 
 enum
 {
   PROP_0,
-  PROP_ADDRESS
+  PROP_ADDRESS,
+  PROP_HOSTNAME,
+  PROP_PORT
 };
 
 struct _GTcpClientPrivate
 {
-  GSocketAddress *address;
+  GInetSocketAddress *address;
+  gchar              *hostname;
+  gushort             port;
 };
 
 static void
-g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+g_tcp_client_constructed (GObject *object)
+{
+  GTcpClient *client = G_TCP_CLIENT (object);
+
+  if (client->priv->address)
+    {
+      // we've been constructed with an address, extract hostname+port
+      client->priv->hostname = g_inet_address_to_string (g_inet_socket_address_get_address (client->priv->address));
+      client->priv->port = g_inet_socket_address_get_port (client->priv->address);
+      return;
+    }
+}
+
+static void
+g_tcp_client_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
@@ -61,18 +76,42 @@ g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParam
         g_value_set_object (value, client->priv->address);
         break;
 
+      case PROP_HOSTNAME:
+        g_value_set_string (value, client->priv->hostname);
+        break;
+
+      case PROP_PORT:
+        g_value_set_uint (value, client->priv->port);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
 }
 
 static void
-g_tcp_client_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+g_tcp_client_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
   switch (prop_id)
     {
+      case PROP_ADDRESS:
+        // sink the address' floating reference
+        client->priv->address = G_INET_SOCKET_ADDRESS (g_object_ref_sink (g_value_get_object (value)));
+        break;
+
+      case PROP_HOSTNAME:
+        client->priv->hostname = g_value_dup_string (value);
+        break;
+
+      case PROP_PORT:
+        client->priv->port = g_value_get_uint (value);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -83,6 +122,8 @@ g_tcp_client_finalize (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
+  g_object_unref (client->priv->address);
+
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize) (object);
 }
@@ -92,6 +133,8 @@ g_tcp_client_dispose (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
+  g_free (client->priv->hostname);
+
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose) (object);
 }
@@ -105,6 +148,7 @@ g_tcp_client_class_init (GTcpClientClass *klass)
 
   gobject_class->finalize = g_tcp_client_finalize;
   gobject_class->dispose = g_tcp_client_dispose;
+  gobject_class->constructed = g_tcp_client_constructed;
   gobject_class->set_property = g_tcp_client_set_property;
   gobject_class->get_property = g_tcp_client_get_property;
 
@@ -112,27 +156,47 @@ g_tcp_client_class_init (GTcpClientClass *klass)
                                    g_param_spec_object ("address",
                                                         "address",
                                                         "the remote address the socket will connect to",
-                                                        G_TYPE_SOCKET_ADDRESS,
+                                                        G_TYPE_INET_SOCKET_ADDRESS,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_HOSTNAME,
+                                   g_param_spec_string ("hostname",
+                                                        "hostname",
+                                                        "the hostname of the remote address the socket will connect to",
+                                                        NULL,
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_PORT,
+                                   g_param_spec_uint ("port",
+                                                      "port",
+                                                      "the remote port the socket will connect to",
+                                                      0,
+                                                      G_MAXUSHORT,
+                                                      0,
+                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
 g_tcp_client_init (GTcpClient *client)
 {
   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client, G_TYPE_TCP_CLIENT, GTcpClientPrivate);
+
+  client->priv->address = NULL;
+  client->priv->hostname = NULL;
+  client->priv->port = 0;
 }
 
 GTcpClient *
 g_tcp_client_new (const gchar *hostname,
                   gushort      port)
 {
-  return NULL;
+  return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "hostname", hostname, "port", port, NULL));
 }
 
 GTcpClient *
-g_tcp_client_new_with_address (GInetSocketAddress *address)
+g_tcp_client_new_from_address (GInetSocketAddress *address)
 {
-  return NULL;
+  return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "address", address, NULL));
 }
 
 gboolean