//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / Tove Route Service
//
//File: client.cpp
//
//Version: $Revision: 1.3 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/06 13:36:11 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Harri Sunila
//
//Description:
//      Implementation of the TOVE Route Service test client
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include <typeinfo>
#include <iostream.h>
#include <string>
#include <string.h>

#include "pf/exception.h"

#include "trs/managementclient.h"
#include "trs/routingclient.h"
#include "pf/system.h"
#include "pf/storage.h"
#include "pf/debug.h"

#include "sf/orbscheduler.h"

#include <stdio.h>

void addHosts(const string &prefix_,
              trsManagementClient &client_)
{
    FILE *f = fopen("/etc/hosts.atm", "r");
    if (f == 0)
    {
        debugUser("Couldn't open /etc/hosts.atm");
    }
    else
    {
        debugUser("Adding hosts in node " + prefix_); 
        char tmp[80];
        string address, host;
        trsLinkList links;
        pfUlong linkNumber = 1;
        pfStorage parameters;
        
        while (feof(f) == 0)
        {
            fscanf(f, "%s", tmp);
            address = tmp;
            fscanf(f, "%s ", tmp);
            host = tmp;
            
            debugPfUlong("Link to host " +
                         host + " " +
                         address +
                         " ; Link number: ",
                         linkNumber);
            
            trsLink newLink(address,
                            linkNumber,
                            parameters);
            links.push_back(newLink);
            linkNumber++;
        }
        fclose(f);
        
        trsNode node(prefix_,
                     links);
        
        try
        {
            debugUser("Registering...");
            client_.registerNode(node);
            debugUser("Done");
        }
        catch(pfException &e)
        {
            e.printInfo();
        }
    }
    return;
}

void askLinks(const string &prefix_,
              trsRoutingClient &client_)
{
    FILE *f = fopen("/etc/hosts.atm", "r");
    if (f == 0)
    {
        debugUser("Couldn't open /etc/hosts.atm");
    }
    else
    {
        debugUser("Asking links of node " + prefix_); 
        char tmp[80];
        string address;
        
        while (feof(f) == 0)
        {
            fscanf(f, "%s", tmp);
            address = tmp;
            fscanf(f, "%s ", tmp);

            debugUser("Asking link to " + address);
            try
            {
                debugTime();
                client_.selectRoute(address, 0, 0);
                debugUser("Waiting response...");
            }
            catch(pfException &e)
            {
                e.printInfo();
            }
        }
        fclose(f);
    }
    return;
}
    
    

int main(int argc, char *argv[], char **)
{
    debugOutputCout();
    
    if (argc < 2)
    {
        debugUser("Usage: client 'network prefix'");
        exit(1);
    }
    string prefix(argv[1]);

    pfSystem::init(argc, argv);

    // Create a CORBA client to manipulate the routing database
    trsManagementClient manager(prefix);

    // Create a CORBA client for route queries
    trsRoutingClient router(prefix);
    // NOTE!! Constructor  trsRoutingClient(ccProtocol *) should be used
    // instead of trsRoutingClient(const string &) in signalling software.
    // This is for testing only the routing algorithms.
    
    addHosts(prefix,
             manager);

    askLinks(prefix,
             router);

    sfORBScheduler::getBOA()->init_servers();
    pfSystem::instance()->run();

    return 0;
}
