view src/MPipeline2/src/mpipeline/geo/RawClient.java @ 13:e05504b18072 database-connection-manager

Move more functions to utils.repository
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline.geo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hewitson
 */
public class RawClient {

  private Socket sock = null;
  private DataInputStream input = null;
  private DataOutputStream output = null;
  private String hostname;
  private int port = 8000;

  public RawClient(String hostname) {

    this.hostname = hostname;

  }

  public void connect() {
    try {
      sock = new Socket(hostname, port);

      input = new DataInputStream(sock.getInputStream());
      output = new DataOutputStream(sock.getOutputStream());

    } catch (UnknownHostException ex) {
      Logger.getLogger(FrameClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(FrameClient.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  public void closeConnection() {
    try {
      input.close();
      output.close();
      sock.close();
    } catch (IOException ex) {
      Logger.getLogger(FrameClient.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public int getLatest() {
    MNet.fsendString(output, "GET LATEST");
    MNet.fsendInt(output, 1);
    int latest = MNet.freadInt(input);
    return latest;
  }

  public ArrayList<String> getChannelList(int gpstime) {
    ArrayList<String> channels = new ArrayList<String>();

    MNet.fsendString(output, "CREATE JOB");
    MNet.fsendString(output, "SET STARTTIME");
    MNet.fsendInt(output, gpstime);
    MNet.fsendString(output, "SET STOPTIME");
    MNet.fsendInt(output, gpstime);
    MNet.fsendString(output, "GET CHANNEL LIST");

    // check server response
    String resp = MNet.freadString(input);
//    System.out.println("server responded: " + resp);

    if (resp.equals("FILE NOT FOUND")) {
      System.err.println("Frame file not found at time " + gpstime);
    } else {

      if (resp.equals("JOB WRONG")) {
        System.err.println("Server job set up wrongly.");
      } else {

        int nchans = MNet.freadInt(input);
        System.out.println("number of channels: " + nchans);

        for (int kk = 0; kk < nchans; kk++) {
          String s = MNet.freadString(input);
//          System.out.println("Channel " + kk + "  : " + s);
          channels.add(s);
        }
      }
    }

    return channels;
  }

}