
/**
 * Überschrift: JAVA based NECMD CGS client
 * Beschreibung: COAMS NECMD CGS-client for GPMS functionality
 * Copyright: Copyright (c) Wolfgang Scherer<p>
 * Organisation: Austria Telecommunications<p>
 * @author Wolfgang Scherer
 * @version 1.0
 */

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

public class GPMSCLIENT {

  Socket cltsock;
  OutputStream outs;
  OutputStreamWriter outw;
  // AsyncReadSocket inps;
  InputStream inss;
  InputStreamReader inpsr;
  DataInputStream inpds;
  BufferedReader ibrd;
  
  String sessid;

  public GPMSCLIENT(String host, int sock) throws java.io.IOException {
  cltsock = new Socket(host,sock);
  // inps = new AsyncReadSocket(cltsock);
  outs = cltsock.getOutputStream();
  inss = cltsock.getInputStream();
  inpsr = new InputStreamReader(inss);
  // inpds = new DataInputStream(inss);
  ibrd = new BufferedReader(inpsr);
  outw = new OutputStreamWriter(outs);
  sessid = null;
  // System.out.println("GMPSCLIENT: Local "+cltsock.getLocalAddress()+
  //                    " port "+cltsock.getLocalPort()+
  //                    " connected to "+cltsock.getInetAddress()+
  //                    " port "+cltsock.getPort());
  }

  public void close() throws java.io.IOException {
    cltsock.close();
    outw = null;
    outs = null;
    inpds = null;
    inss = null;
    inpsr = null;
    ibrd = null;
  }


  public String operation(String command) throws java.io.IOException {
    String resp;
    StringBuffer res = new StringBuffer(5000);
    boolean done = false;
    boolean first = true;
    outw.write(command);
    outw.write('\r');
    outw.write('\n');
    outw.flush();
    // System.out.println("OPERATION: sent "+command);
    while (!done) {
      // resp = inpds.readLine();
      resp = ibrd.readLine();
      // System.out.println("  OPERATION: got "+resp);
      if (!first) res.append('\\');
      res.append(resp);
      if (first) {
        if (!resp.startsWith("125 STX")) {
          done = true;
          }
        }
      else {
        if (resp.startsWith("226 ETX")) {
          done = true;
          }
        }
      first = false;
      }
    return res.toString();
  }

  public void login(String username, String password)
                    throws java.io.IOException {
    String resp = this.operation("USER,"+username+","+password);
    System.out.println("LOGIN.RESP="+resp);
  }

  public String gpms(String number) throws java.io.IOException {
    return this.operation("GPMS,"+number);
  }

  public String exit() throws java.io.IOException {
    return this.operation("EXIT");
  }

  public String quit() throws java.io.IOException {
    return this.operation("QUIT");
  }

  public static void main(String[] args) throws java.io.IOException {
    String usrinp;
    String resp;
    String msisdn = "436651000001";
    String rnode = "janus";
    int sockno = 8758;
    int cnt = 1;
    GPMSCLIENT gc = null;
    if (args.length>5) cnt = Integer.parseInt(args[5]);
    if (args.length>3) msisdn = args[4];
    if (args.length>1) sockno = Integer.parseInt(args[1]);
    if (args.length>0) rnode = args[0];
    for (int i = 1; i<=cnt; i++) {
      Runtime rt = Runtime.getRuntime();
      long isFree = rt.freeMemory();
      long wasFree;
      long wasFreeBefore=isFree;
      do { wasFree=isFree;
           rt.gc();
	   isFree=rt.freeMemory();
      } while (isFree>wasFree);
      rt.runFinalization();
      System.out.println("HOUSKEEPING: incremented free memory from "+
                         wasFreeBefore+" to "+isFree);
      gc = new GPMSCLIENT(args[0],sockno);
      gc.login(args[2],args[3]);
      resp = gc.gpms(msisdn);
      System.out.println("GPMS.RESP="+resp);
      resp = gc.exit();
      System.out.println("EXIT.RESP="+resp);
      resp = gc.quit();
      System.out.println("QUIT.RESP="+resp);
      gc.close();
      gc = null;
      System.out.println("Sleeping ...");
      try {
	  Thread.sleep(100); }
      catch (Exception e) {};
      System.out.println("Woke up, iteration "+i+" complete.");
      }
  }
}

