/**
* Title: GPS Simulator
* Copyright: Copyright (c) 2002
* Company: University of Oregon Computer Science Dept.
*
* @author Jason Prideaux
* @version 1.0
*
*/
package controller;
import gui.*;
import gui.tray.*;
import util.*;
import agent.*;
import transport.*;
import transport.protocol.*;
import javax.swing.JFrame;
import java.awt.*;
import java.util.*;
/* ====================================================================== */
/** This class is the core of the simulator. It initializes the interface
* components, holds the agents, and controls the sockets for sending and
* recieving data.
*
*/
public class Controller implements SocketReceiverListener{
/** The chart is the visual map. */
private ChartComp chart;
/** The main frame which displays the map ChartComp. */
private SimulatorFrame frame;
/** An interface component for viewing agent stats, etc. */
private TrayDialog tray;
/** The agent that is being moved, or has center of screen. */
private Agent current_agent;
/** The list of all access points in the simulation. */
private Vector points = new Vector();
/** The list of all users in the simulation. */
private Vector users = new Vector();
/** Components that want to listen for when information changes in controller. */
private Vector listeners = new Vector();
/* ===================================================================== */
/** This constructor initializes the controller by creating the interface
* components, so that the user can begin interacting with the simulator.
* It also starts the SocketReceiver for listening to incoming messages.
*
*/
public Controller(){
chart = new ChartComp(this, false );
chart.loadChart("uo_data.xml");
frame = new SimulatorFrame(this, chart);
tray = new TrayDialog(this, frame);
try{ Thread.sleep(500); }catch(Exception e){}
frame.setVisible(true);
SocketReceiver sr = new SocketReceiver(this, 2323);
sr.start();
} //Constructor
/* ===================================================================== */
/** This method is for adding a new user into the simulator. The user is
* created based on the given information, and is added into the map, and
* all controller listeners are notified of the new addition.
*
* @param r The range for the new user.
* @param p The port for the new user.
*
*/
public void addUser(int r, int p){
Point pt = frame.getChartPane().getScrollPosition();
Dimension d= frame.getChartPane().getViewportSize();
User u = new User( new Point((int)(pt.getX()+(d.width)/2),(int)(pt.getY()+(d.height)/2)), p );
current_agent = u;
users.add( current_agent);
current_agent.setRange(r);
current_agent.setName( "user"+users.indexOf(current_agent) );
int index = users.indexOf(u);
updateAgentPosition( current_agent );
fireControllerChange();
} //method: addUser
/* ===================================================================== */
/** This method is for removing a user from the simulator. The actual
* user is given to the method, and the remove from the simulation, and
* all controller listeners are notified of the new removal.
*
* @param u The User object to remove from the users list.
*
*/
public void removeUser(User u){
users.remove(u);
if( current_agent.getName().equals( u.getName()) && users.size() > 0){
current_agent = (User)users.elementAt(0);
}
chart.repaint();
fireControllerChange();
} //method: removeUser
/* ===================================================================== */
/** This method is for adding a new access point into the simulator. The
* accesspoint is created based on the given information, and is added
* into the map, and all controller listeners are notified of the new
* addition.
*
* @param r The range for the new Access Point
*
*/
public void addAccessPoint(int r ){
Point pt = frame.getChartPane().getScrollPosition();
Dimension d= frame.getChartPane().getViewportSize();
AccessPoint u = new AccessPoint( " ", new Point((int)(pt.getX()+(d.width)/2),(int)(pt.getY()+(d.height)/2)), r );
current_agent = u;
points.add( u);
u.setRange(r);
u.setName( "access"+points.indexOf(u) );
updateAgentPosition( u );
fireControllerChange();
} //method: addAccessPoint
/* ===================================================================== */
/** This method is for removing a access point from the simulator. The actual
* access point is given to the method, and the remove from the simulation, and
* all controller listeners are notified of the new removal.
*
* @param u The Access Point object to remove from the points list.
*
*/
public void removeAccessPoint(AccessPoint u){
points.remove(u);
chart.repaint();
fireControllerChange();
} //method: removeAccessPoint
/* ===================================================================== */
/** This method updating an Agent's gps Position. It will get the pixel
* location, and then turn this into a gps Position.
*
* @param u The agent whose Position needs to be set by this method.
*
*/
public void updateAgentPosition(Agent u){
// Get the location Point
chart.updatePos(u.getLocation(), users, points);
Point poi = u.getLocation();
// Turn pixel into gps coordinates.
Position chart_pos = chart.getChartData().transPos(poi);
Position old = u.getPosition();
//Retain some old Position information.
chart_pos.setHeading( old.getHeading() );
chart_pos.setTime( old.getTime() );
u.setPosition( chart_pos );
} //method: updateAgentPosition
/* ===================================================================== */
/** This method returns the Vector of all the current users in the simulator.
*
* @return Vector All users in the simulation.
*
*/
public Vector getUsers(){
return users;
} //method: getUsers
/* ===================================================================== */
/** This method returns the Vector of all current access points in the
* simulator.
*
* @return Vector All Access Points in the simulation.
*
*/
public Vector getPoints(){
return points;
} //method: getPoints
/* ===================================================================== */
/** This method returns the current agent in focus or being moved around
* in the simulator map.
*
* @return Agent The current Agent being moved or center on screen.
*
*/
public Agent getCurrentAgent(){
return current_agent;
} //method: getCurrentAgent
/* ===================================================================== */
/** This method sets who the current is.
*
* @param a The Agent to make the current agent.
*
*/
public void setCurrentAgent(Agent a){
current_agent = a;
} //method: setCurrentAgent
/* ===================================================================== */
/** This method is for centering the given agent on the map. Also returns
* the agent object in case it is needed once centered.
*
* @param name The name of Agent to find and center on screen.
* @return Agent The Agent object that was centered.
*
*/
public Agent centerAgent(String name){
Agent a = null;
// Find the user or access point.
for( int i=0; i -1 ){
current_agent = a;
fireControllerChange();
frame.centerScreen(a.getLocation());
}
}
for( int i=0; i -1 ){
current_agent = a;
fireControllerChange();
frame.centerScreen(a.getLocation());
}
}
return a;
} //method: centerAgent
/* ===================================================================== */
/** This method is called when a User's location has changed. A NMEA
* Message is created and then sent to the User's corresponding iPAQ via
* a port number.
*
* @param u The user who has changed location.
*
*/
public void locationChange(User u){
Position p = u.getPosition();
Message msg = new NMEAMessage(u.getPort(), 2323, p.getLatitude(), p.getLongitude(), p.getHeading(), p.getTime());
SocketSender ss = new SocketSender(msg);
ss.start();
} //method: locationChange
/* ===================================================================== */
/** This method is called to send a message to all agents.
*
* @param msg Message to send
*
*/
public void sendToAll(Sim2PMessage msg){
User a = null;
// Find the user or access point.
for( int i=0; i