/*
* planeSender.java
*
* Created on October 19, 2007, 10:50 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package PlaneSim;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import messages.planesim.PlaneObject;
import messages.planesim.SimDataObject;
/**
*
* @author Matthias Roth
*/
public class planeSender {
private List<PlaneObject> pl = new LinkedList<PlaneObject>();
private String serverIp;
private Socket clientSocket;
private ObjectOutputStream out;
private Timer timer;
private boolean started;
private int sendFreq = 1000; //milisekunden
/**
* Creates a new planeSender instance.
*/
public planeSender() {
startTimer();
}
/**
* Stops the timer.
*
* The data will not be sent to the server anymore.
*/
public final void stopTimer() {
timer.cancel();
started = false;
}
/**
* Starts the timer.
*
* The data will now be send to the server frequently.
*/
public final void startTimer() {
timer = new Timer();
timer.schedule(new Task(), 1000, sendFreq);
started = true;
}
/**
* Adds a simulated plane.
*
* @param po The planeobject which consists of the required values longitude,
* latitude, altitude and the name
*/
public final void addPlane(final PlaneObject po) {
pl.add(po);
}
/**
* Removes a simulated plane.
*
* @param po The planeobject which consists of the required values longitude,
* latitude, altitude and the name
*/
public final void removePlane(final PlaneObject po) {
pl.remove(po);
}
/**
* Opens a socket to the server.
*
* @param ip the ip of the server to connect to
* @throws java.lang.Exception exceptions will be caught by the GUI and
* handled appropriatly
*/
public final void openSocket(final String ip) throws Exception {
this.serverIp = ip;
try {
clientSocket = new Socket(serverIp, 48000);
out = new ObjectOutputStream(clientSocket.getOutputStream());
} finally {
System.out.println("");
}
}
/**
* Closes the socket to the server.
*/
public final void closeSocket() {
try {
out.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("IOException");
}
}
/**
* Used to get the simulated planes.
*
* @return a collection of the simulated planes
*/
public final Collection<PlaneObject> getPlanes() {
return pl;
}
/**
* Erases all planes of the list.
*/
public final void clear() {
pl.clear();
}
/**
* The task which periodically sends the update planedata to the server and
* removes arrived planes.
*/
class Task extends TimerTask {
public void run() {
Iterator it = pl.listIterator();
List<PlaneObject> temp = new LinkedList<PlaneObject>();
while (it.hasNext()) {
PlaneObject next = (PlaneObject) it.next();
if (next.hasArrived()) {
temp.add(next);
}
}
pl.removeAll(temp);
it = pl.listIterator();
while (it.hasNext()) {
try {
out.writeObject(new SimDataObject((PlaneObject) it.next()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}