/************************************************************************
  		Network - Copyright 2003 - L. Hirschy, M. Ihde, C. Weis

For licensing information, see LICENSE.TXT

This file was generated on Tue Apr 1 2003 at 22:08:44
**************************************************************************/


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


/**
  * class Network
  * This is the networking class that gets the data to/from the network from/to the
  * the Buffer.
  */

public class Network extends Thread
{

/**Attributes: */

	private int debug_level = 0;
	private Socket client_socket;
	private ServerSocket server_socket;
	private ObjectOutputStream network_output;
	private ObjectInputStream network_input;
	private Buffer data_buffer;
	private int tcp_port = 0;
	private String remote_ip_addr; // IP address of speaker
	private String devtype; // either "speaker" of "microphone"
	private ObjectInputStream input;
	private ObjectOutputStream output;
	private Mp3Data data;


/** Public methods: */
	public void Network( String dev_type )
	{
		devtype = dev_type;
		data_buffer = new Buffer();
	}

	public void setTcpPort( int port )
	{
		tcp_port = port;
	}

	public void setRemoteIpAddr( String ipaddr )
	{
		remote_ip_addr = ipaddr;
	}

	public void setDebugLevel( int level )
	{
		debug_level = level;
	}

	public int getDebugLevel(  )
	{
		return debug_level;
	}

	public void run(  )
	{
		/* If we're a speaker */
		if ( devtype.equals("speaker") ) { 
			try {
				server_socket = new ServerSocket(tcp_port);

				/* This will block until a client request comes in */
				client_socket = server_socket.accept();	
				input = new ObjectInputStream( client_socket.getInputStream() );
			} catch (IOException ioe) {
			}

			/* We loop forever here */
			/* We block if the network goes idle */
			while ( true ) {
				getNetworkData();	
			}

		/* If we're a microphone */
		} else if ( devtype.equals("microphone") ) {
			try {
				client_socket = new Socket(remote_ip_addr, tcp_port);
				output = new ObjectOutputStream( client_socket.getOutputStream() );
			} catch (IOException ioe) {

			}


			/* We loop forever here */
			/* We block if HAL goes empty or */
			/* the "speaker" gets congested */
			while ( true ) {
				putNetworkData();
			}
		}

	}


/** Private methods: */
	/* Move data from network to HAL */
	private void getNetworkData(  )
	{
		data = new Mp3Data();
		try {
			/* Read in an MP3 data object from the network */
			data = (Mp3Data) input.readObject();	
		} catch ( IOException ioe ) {
		} catch ( ClassNotFoundException cnfe ) {
		}

		/* Put the mp3data into the buffer */
		/* Warning: this could (should) block if buffer is busy or full */
		/* See the synchronized methods in Buffer.java for more info */
		data_buffer.putElement(data);	

		/* 'The trash collector we must trust, yeeess!' --Yoda */
	}

	/* Move data from HAL to network */
	private void putNetworkData( )
	{
		/* Where we'll store the data */
		Mp3Data data;
		/* get the data from the buffer */
		data = data_buffer.getElement();	

		try {	
			/* Send the data object to the speaker */	
			output.writeObject( data );
		} catch ( IOException ioe ) {
		}

		/* 'The trash collector we must trust, yeeesss!' --Yoda */
	}
}
