/** * Title: GPS Simulator * Copyright: Copyright (c) 2002 * Company: University of Oregon Computer Science Dept. * * @author Jason Prideaux * @version 1.0 * */ package transport; import transport.protocol.*; import java.io.*; import java.net.*; import java.util.*; /* ====================================================================== */ /** This class is used by the controller to send messages to agents with * a port number. It simply opens a socket to the receiver's port and * sends the message object. * */ public class SocketSender extends Thread { /** The message to send. */ private Message message; /** The host that the server (ipaq) is on. */ private String host = "localhost"; /* ===================================================================== */ /** This constructor initializes the SocketSender so that it can send a * message. * * @param message The message to send. * */ public SocketSender(Message message){ this.message = message; } //Constructor /* ===================================================================== */ /** This constructor initializes the SocketSender so that it can send a * message. * * @param message The message to send. * @param host The host that the server (ipaq) is on. * */ public SocketSender(Message message, String host){ this.message = message; this.host = host; } //Constructor /* ===================================================================== */ /** This method opens a Socket and trys to send the message stored in this * class. It will only try to send the Message once. * */ public void run(){ try{ // Get a Socket Socket s = new Socket( host, message.getTo() ); // Get the output stream System.out.println ("Simulator: sending message on port "+message.getTo() ); BufferedOutputStream bufout = new BufferedOutputStream(s.getOutputStream()); ObjectOutputStream objout = new ObjectOutputStream(bufout); // Send the object and close objout.writeObject(message); objout.flush(); objout.close(); s.close(); }catch(Exception e){System.out.println("Not connected to the user.");} } //method: run } //class: SocketSender