/*
 * COPYRIGHT (c) 1998-1999 Jan-Henrik Haukeland, http://www.tildeslash.com/
 */


package ts.biff;

import java.net.*;
import java.io.*;

import ts.config.*;
import ts.event.*;


/**
 * This program connects to a WakeUp server and tries to wake it up.
 * If no server could be found or a fatal error occurs during the
 * connection the program pops up an error dialog with an appropriate
 * message and exits.
 *
 * @author  Jan-Henrik Haukeland &lt;hauk@tildeslash.com&gt;
 * @version $Id: WakeUpClient.java,v 1.9 1999/11/26 21:58:15 hauk Exp $ 
 */
public final class WakeUpClient 
{

  public static void main(String[] args) { WakeUpClient.WakeUp(); }

  private WakeUpClient() { }


  public static void WakeUp()  {
    Socket         client= null;
    BufferedReader in=     null;

    try {
      Config conf= Config.getInstance();
      File file= Util.getUserConfigFile();
      if ( file == null ) throw new ConfigException("File does not exist");
      conf.load(file);

      int port= Config.getint("wakeup.server.port");

      client= new Socket(InetAddress.getLocalHost(), port);
      in= new BufferedReader(new InputStreamReader(client.getInputStream()));

      String response;
      if ( (response= in.readLine()) != null &&
	   response.equals(Constant.WAKEUP_HELLO) ) {
	return;
      }

      Util.showInfo("Could not find a Wakeup server");
      
    } catch(ConfigException c) {
      Util.showError("Could not read config file ("+
		     Constant.USER_CONTROL_FILE+")\n"+
		     c.getMessage());
      System.exit(1);
    } catch (IOException io) {
      Util.showError("Error: "+io.getMessage());
      System.exit(1);
    } catch (Exception e) {
      Util.showError("Error: "+e.getMessage());
      e.printStackTrace();
      System.exit(1);
    } finally {
      try {
	client.close();
	in.close();
      }catch(Exception e){}
    }
  }
}
  

    


