<html>
<xmp>
/**
 * Title: iSIM
 * Copyright: Copyright (c) 2002
 * Company:   University of Oregon Computer Science Dept.
 *
 * @author Jason Prideaux
 * @version 1.0
 *
 */

package agent.autonomous;

import agent.*;
import controller.*;
import gui.*;
import transport.*;
import transport.protocol.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.io.*;


/* ====================================================================== */
/** This abstract class extends the regular user-controlled agent in iSIM.
 *  It provides a set of methods for sensing and actuating.  This class will
 *  get data from iSIM and pass it to the autonomous agents via sense methods.
 *  It then provides actuation for autonomous agents with actuate methods,
 *  which pass the data into iSIM.
 *
 */
public abstract class Autonomous extends User implements SocketReceiverListener{

	/** The simulation controller. */
	private Controller controller;

	/* ====================================================================== */
	/** This constructor sets up the information needed for this class to
	 *  successfully act as mediator bewteen iSIM and autonomous agents.
	 *
	 *  @param  c   The iSIM controller.
	 *  @param  loc  The location of the agent.
	 *  @param  port  The communication port need to speak with iSIM.
	 *  @param  range   The wireless max range.
	 *  @param  s   The name of the autonomous agent.
	 *
	 */
	public Autonomous(Controller c, Point loc, int port, int range, String s){

		// Super our agent/user information.
		super(s, loc, port, range);

		// As an Autonomous robot, we listen/sense for environment information.
		SocketReceiver sr = new SocketReceiver(this, port);
		sr.start();

		//simulation controller
		controller = c;

		controller.locationChange(this);
		controller.fireControllerChange();
		controller.getChartComp().repaint();

	} //Constructor



	// This is how autonomous agents get location information.
	public abstract void senseLocation(Point loc, double hea);

	// This is how autonomous agents get networking message information.
	public abstract void senseMessage(HelpMessage msg);


	// This is how autonomous agents change their location.
	public void actuateLocation(Point loc, double hea){

		try{ Thread.sleep(1000); }catch(Exception e){}

		this.getPosition().setHeading(hea);
		controller.getChartComp().repaint();

		try{ Thread.sleep(1000); }catch(Exception e){}

		this.setLocation(loc);

		controller.locationChange(this);
		controller.fireControllerChange();
		controller.getChartComp().repaint();

	}

	// This is how autonomous agents send a networking message.
	public void actuateMessage(HelpMessage msg){

		SocketSender ss = new SocketSender(msg);
		ss.start();

	}




	/* ====================================================================== */
	/** iSIM sends messages to all agent devices.  This is how agents get input
	 *  from the simulator.  The input could be many types of messages from iSIM,
	 *  so this method gets the iSIM message, checks for the type of message,
	 *  and then passes the data to the autonomous agent.
	 *
	 *  @param  msg   The incoming message from iSIM.
	 *
	 */
	public void incomingMessage(Message msg){

		// Location information
		if( msg instanceof NMEAMessage ){

			senseLocation( this.getLocation(), this.getPosition().getHeading() );

		// A message that allows robots to communicate with one another.
		}else if( msg instanceof HelpMessage ){

			senseMessage((HelpMessage)msg);

		}

	} //method: incomingMessage


} //class: Autonomous
</xmp>
</html>
