/* Jungle Monkey
 * Copyright (C) 1999-2001  The Regents of the University of Michigan
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */




#include "jmutil.h"
#include "jmintl.h"
#include "btp/b_conn.h"


gboolean    jm_running			= FALSE;

gchar*	    jm_hostname		     	= NULL;
GInetAddr*  jm_iface 			= NULL;

GInetAddr*  jm_btp_iface	 	= NULL;
BPeer*      jm_btp_bpeer 		= NULL;

GInetAddr*  jm_mtp_iface 		= NULL;
MtpServer*  jm_mtp_server 		= NULL;

GURL*	    jm_mtp_rvous_url 		= NULL;
gboolean    jm_mtp_rvous_locally	= TRUE;
gboolean    jm_mtp_mirror_on_download 	= FALSE;

ShaCache*   jm_sha_cache		= NULL;


gboolean
jmutil_init (gchar* hostname_name, gboolean use_dns,
	     gchar* interface_name, 
	     gint btp_port, gboolean force_btp_port,  
	     gint mtp_port, gboolean force_mtp_port,
	     gchar* rendezvous_url_str)
{

  g_return_val_if_fail (!jm_running, TRUE);

  /* If an interface was specified, make sure it resoves to an address */
  if (interface_name)
    {
      /* Convert interface_name to InetAddr. */
      jm_iface = gnet_inetaddr_new (interface_name, 0);
      if (!jm_iface)
	{
	  g_warning (_("Bad interface name: %s.\n"), interface_name);
	  goto error;
	}
    }

  /* Otherwise, auto-detect the interface */
  else
    {
      jm_iface = gnet_inetaddr_autodetect_internet_interface ();
      if (!jm_iface)
	{
	  g_warning (_("Could not auto-detect an internet interface. "
		       "Use the --interface option to set it manually.\n"));
	  goto error;
	}
    }

  /* If a hostname was specified, use it */
  if (hostname_name)
    {
      jm_hostname = g_strdup (hostname_name);
    }

  /* Autodetect the hostname */
  else
    {
      if (use_dns)
	jm_hostname = gnet_inetaddr_get_name (jm_iface);

      if (!jm_hostname ||
	  !gnet_inetaddr_is_internet_domainname (jm_hostname))
	jm_hostname = gnet_inetaddr_get_canonical_name (jm_iface);

      g_assert (jm_hostname);

      if (!gnet_inetaddr_is_internet_domainname (jm_hostname))
	{
	  g_warning (_("Could not auto-detect hostname.  "
		       "Use the --hostname option to set it manually.\n"));
	  goto error;
	}
    }

  if (rendezvous_url_str)
    {
      jm_mtp_rvous_url = gnet_url_new (rendezvous_url_str);
      jm_mtp_rvous_locally = FALSE;
      if (!jm_mtp_rvous_url)
	{
	  g_warning (_("Bad rendezvous URL: %s\n"), rendezvous_url_str);
	  goto error;
	}
      gnet_url_set_protocol (jm_mtp_rvous_url, "mtp");
    }


  /* Start BTP */
  jm_btp_iface = gnet_inetaddr_clone (jm_iface);
  gnet_inetaddr_set_port (jm_btp_iface, btp_port);
  jm_btp_bpeer = b_peer_new (jm_hostname, jm_btp_iface, force_btp_port);
  if (!jm_btp_bpeer)
    {
      g_warning (_("Error starting BTP peer\n"));
      goto error;
    }

  /* Start MTP */
  jm_mtp_iface = gnet_inetaddr_clone (jm_iface);
  gnet_inetaddr_set_port (jm_mtp_iface, mtp_port);
  jm_mtp_server = mtp_server_new (jm_hostname, jm_mtp_iface, 
				  force_mtp_port, 
				  MTP_SERVER_TYPE_BOTH, 
				  FALSE);

  if (!jm_mtp_server)
    {
      g_warning (_("Error staring MTP server\n"));
      goto error;
    }

  /* Set MTP URL if there isn't one */
  if (!jm_mtp_rvous_url)
    {
      jm_mtp_rvous_locally = TRUE;
      jm_mtp_rvous_url = 
	gnet_url_new_fields ("mtp", jm_hostname, 
			     jm_mtp_server->server->port, NULL);
    }


  
  jm_sha_cache = sha_cache_new ();

  jm_running = TRUE;

  return FALSE;

 error:
  jmutil_shutdown ();
  return TRUE;
}



gboolean
jmutil_shutdown (void)
{
  jm_running = FALSE;

  g_free (jm_hostname);			jm_hostname = NULL;
  gnet_inetaddr_delete (jm_iface);  	jm_iface = NULL;

  gnet_inetaddr_delete (jm_btp_iface);	jm_btp_iface = NULL;
  b_peer_delete (jm_btp_bpeer);		jm_btp_bpeer = NULL;

  gnet_inetaddr_delete (jm_mtp_iface);	jm_mtp_iface = NULL;
  mtp_server_delete (jm_mtp_server);    jm_mtp_server = NULL;

  gnet_url_delete (jm_mtp_rvous_url);   jm_mtp_rvous_url = NULL;
  jm_mtp_rvous_locally = TRUE;
  jm_mtp_mirror_on_download = FALSE;

  sha_cache_delete (jm_sha_cache);      jm_sha_cache = NULL;

  return (b_conn_num_conns == 0);
}



syntax highlighted by Code2HTML, v. 0.9.1