/***************************************************************************
 *   Copyright (C) 2004 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   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 "evaservers.h"
#include <qdns.h>
#include <qfile.h>
#include <qdatastream.h>
#include <qdatetime.h>   // seed for rand()

#include <stdlib.h>      // rand() function

EvaServers::EvaServers(QString &dataRoot):
    gotIP(FALSE),
    isLoaded(FALSE)
{
    filename = dataRoot + "/servers";
    QTime t = QTime::currentTime();
    srand( t.hour()*12+t.minute()*60+t.second()*60 );
    isLoaded = this->loadServers();
    dns = NULL;
}


EvaServers::~EvaServers()
{
    delete dns;
}

void EvaServers::fetchAddress( bool isUdp )
{
    int num = 0;
    if(isUdp){
      num = UDPServers.count();
      fetchType = UDP;
    } else{
      num = TCPServers.count();
      fetchType = TCP;
    }
    if(num == 0 ){
        defaultAddress();
        return;
    }
    int index = rand() % num;
    serverItem addr;
    if(isUdp)
      addr = UDPServers[index];
    else
      addr = TCPServers[index];
    
    if(addr.type == Addr_IP){
        emit isReady(QHostAddress(addr.addr));
        return;
    }
  
    // the address should be a URL now, so we try to get the IP          
    if(dns!=NULL) {
        QObject::disconnect(dns, 0,0,0);
        delete dns;
    }
    dns =  new QDns(addr.addr, QDns::A);    
    QObject::connect(dns, SIGNAL(resultsReady()), this, SLOT(getResultsSlot()));
}

bool EvaServers::loadServers( )
{
    QFile file(filename);    
    if(!file.open(IO_ReadOnly)){
            return FALSE;
    }
    
    QTextStream stream(&file);
    QString line;
    QStringList lineList;
    int nextType = 0; 
    while(!stream.atEnd()){
        line = stream.readLine().stripWhiteSpace();

        if(line == "UDP"){
            nextType = UDP;
            continue;
        }else if(line == "TCP"){
            nextType = TCP;
            continue;
        } else if(line == "" ){
            nextType = 0;
            continue;
            }
                
        lineList = QStringList::split(":", line);
    
        if(lineList.size() != 2)
          continue;

        lineList[0].stripWhiteSpace();
                        
        serverItem *item = new serverItem();    
        if(lineList[0]=="URL"){        
            item->type = Addr_URL;                
        }else if(lineList[0]=="IP"){
            item->type = Addr_IP;
        }else
            continue;
        
        item->addr = lineList[1].stripWhiteSpace();
        
        if(nextType == UDP){
            UDPServers.append(*item);
        }
        
        if(nextType == TCP){
            TCPServers.append(*item);
        }
    }
    
    file.close();
    return TRUE;  
}

void EvaServers::defaultAddress()
{    
    if(fetchType == TCP){
        emit isReady(QHostAddress("218.17.209.23"));
    }else{
        emit isReady(QHostAddress("218.17.209.20"));    
    }    
}

void EvaServers::getResultsSlot( )
{
    QValueList<QHostAddress> list = dns->addresses();
    if(list.count() == 0 ){
        defaultAddress();
        return;
    }
    
    QHostAddress addr = list[0];
    emit isReady(addr);
}


