/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-  */
/*
 * socketcom.cc
 * Copyright (C) 2014 Unknown <root@on6jc>
 *
 * socket_test is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * socket_test is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "socketcom.h"

GIOChannel *channel;

gboolean
network_read(GIOChannel *source,
             GIOCondition cond,
             gpointer data)
{
  char bff[100];
  GError  *error = NULL;
  gsize   bytes_read;
  GIOStatus ret;
  
  printf("Channel mon installed\n");
  ret = g_io_channel_read_chars(source, bff, sizeof(bff), &bytes_read, &error);
  
  if (ret == G_IO_STATUS_ERROR)
    g_error("Error reading: %s\n", error->message);
  else {
    bff[bytes_read] = 0;
    ((netread_callback) data)(bff);
    printf("Got: %s\n", bff);
  }
  return TRUE;
}

gboolean
network_write_chars(GIOChannel *dest, char *message)
{
  GError  *error = NULL;
  gsize   bytes_written;
  GIOStatus ret;
  
  printf("Trying to write %s\n", message);
  ret = g_io_channel_write_chars(dest, message, strlen(message), 
		&bytes_written, &error);
  
  if (ret == G_IO_STATUS_ERROR)
    g_error("Error writing: %s\n", error->message);

  g_io_channel_flush(dest, &error);	// Important! Else message stays
					// in the buffer
  
  return TRUE;
}

gboolean
new_connection(GSocketService *service,
               GSocketConnection *connection,
               GObject *source_object,
               gpointer user_data)
{
  g_object_ref(connection); 
 
  GSocketAddress *sockaddr;
  GInetAddress   *addr;
  guint16         port;
  
  sockaddr = g_socket_connection_get_remote_address(connection, NULL);
  addr     = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  port     = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));
  
  printf("New Connection from %s:%d\n", g_inet_address_to_string(addr), port);

  GSocket *socket = g_socket_connection_get_socket(connection);

  gint fd = g_socket_get_fd(socket);
  channel = g_io_channel_unix_new(fd);
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, user_data);
  
  return TRUE;
}

void
start_socket(const char *host, int port, netread_callback callback)
{
  GSocketService *service;
  GInetAddress   *address;
  GSocketAddress *socket_address;
  
  service = g_socket_service_new();
  address = g_inet_address_new_from_string(host);
  socket_address = g_inet_socket_address_new(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(address);

  g_signal_connect(service, "incoming", G_CALLBACK(new_connection),
                (gpointer) callback);

  g_socket_service_start(service);
}


void
close_socket(int listenfd)
{
  close(listenfd);
}