//
// A Java interface for talking to the LPC logic engine
// over TCP/IP sockets.
//
//
//    Copyright (C) 2000-1 by Hugh Jack <jackh@gvsu.edu>
//
//    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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
// Last Modified: November 8, 2000
//

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

public class Message {
	Socket				rw_socket;
	ServerSocket		server_socket;
	OutputStream 		outstream = null;
	InputStream 		instream = null;

	char[]	in_line;

	int				ERROR = -1;
	int				NO_ERROR = 0;
	int				NONE = 0;
	int				WRITE = 1;
	int				READ = 2;
	int				SERVER = 4;
	int				CLIENT = 8;

	Message(){
		in_line = new char[1007];
	}

	public void startclient(String serverName, int portNumber){
        	try{
			// InetAddress			myname;
			// get the local machine name
			// myname = InetAddress.getLocalHost();
			// System.out.println("My name is :"+myname.getHostName());
			// System.out.println("My address is :"+myname.getHostAddress());
			rw_socket = new Socket(serverName, portNumber);
		} catch (UnknownHostException uhe){
			System.out.println("Could not connect to the server: "+uhe.getMessage());
		} catch (IOException ioe){
			System.out.println("Error creating client socket: "+ioe.getMessage());
		} catch (SecurityException sec){
			System.out.println("A Security Problem: "+sec.getMessage());
		} catch (Exception s){
			System.out.println("Could not do the network thing: "+s.getMessage());
		};		
	}

	public void startserver(int portNumber){
		try{
			server_socket = new ServerSocket(portNumber);
		} catch(Exception e){
			System.out.println("Error setting up server: "+e.getMessage());
		}
	}

	public int connect(int type){
		int		error = ERROR;
		if((type & SERVER) != 0){
			try{
				server_socket.setSoTimeout(1);
				rw_socket = server_socket.accept();
				error = NO_ERROR;
			} catch(Exception e){

			}
		}
		if((type & WRITE) != 0){
			try {
				outstream = rw_socket.getOutputStream();
				error = NO_ERROR;
			} catch (IOException ioe) {
				System.out.println("Error getting write socket stream: "+ioe.getMessage());
			}
		}
		if((type & READ) != 0){
			try {
				instream = rw_socket.getInputStream();
				error = NO_ERROR;
			} catch (IOException ioe) {
				System.out.println("Error getting read socket stream: "+ioe.getMessage());
			}
		}

		return error;
	}

  	public void send_string(String out){
   		try{
			byte buffer[] = new byte[out.length()];
			buffer = out.getBytes();
			// out.getBytes(0, buffer.length, buffer,0);
			outstream.write(buffer);
			// outstream.flush();
//System.out.println("Writing String: <"+out.trim()+">");
		} catch (Exception e) {
			System.out.println("Another send Error: "+e.getMessage());
		};
  	}

  	public String get_string(){
		int		nbytes;
		int		value;
		// String	in;
		int		in_count;

		String		in;

//in_line = new char[1007];
//in_count = 0;
   		// if(in != null) in.delete();
		try{
			//byte buffer[] = new byte[ 1024 ];
			//nbytes = instream.read (buffer, 0 ,1024);
			if(instream.available() > 0){
//System.out.println("get 0 ");
				in_count = 0;
				while((instream.available() > 0) && (in_count < (1007-1))){
//System.out.println("get 1 ");
					value = instream.read();
					 //System.out.println("character: "+value);
					// System.out.println("buffer==String was: ["+buffer+"]");
					//if(value != null){
						in_line[in_count] = (char)value;
						in_count++;
					//}
				}
//System.out.println("get 2 "+in_count);
				if(in_count > 0){
					in_line[in_count] = 0;
					in = new String(in_line, 0, in_count+1);
//System.out.println("in==String was: ["+in+"]");
					return in;
				} else {
//System.out.println("get 6 ");
					// in = new String("");
					// System.out.println("in==String was: [null]");
					return null; // in;
					// in = null;
				}
			}
		} catch (Exception e) {
			System.out.println("Error getting socket stream: "+e.getMessage());
			//System.out.println("Got   instream="+instream+"   in_count="+in_count+"  ["+in_line+"]");
			e.printStackTrace();
		};
//System.out.println("get 8 ");
		return null;
  	}

	public void disconnect(int type){
		if((type & WRITE) != 0){
			try {
				if(outstream != null){
					outstream.flush();
					outstream.close();
				}
			} catch (IOException io){
				System.out.println("Write disconnect problem: "+io.getMessage());
			}
		}
		if((type & READ) != 0){
			try {
				if(instream != null) instream.close();
			} catch (IOException io){
				System.out.println("Read disconnect problem: "+io.getMessage());
			}
		}
		if((type & SERVER) != 0){
			try{
				server_socket.close();
			} catch(Exception e){

			}
		}
		if((type & CLIENT) != 0){
			try{
				rw_socket.close();
			} catch (IOException io){
				System.out.println("Could not shut down client socket"+io.getMessage());
			}
		}
	}

      	public void endsocket(){
        	try{   
			rw_socket.close();
		} catch (Exception s) {
			System.out.println("Could not quit the network thing: "+s.getMessage());
		};		
	}

};

