/* * Message.java * Karl-Adam Karlsson * 06-10-12 * * This class is used for communication by the chatclients. * */ import javax.crypto.*; import java.io.*; import java.net.*; import java.util.LinkedList; public class Message implements Serializable{ public static final int MESSAGE_CONNECT = 1; public static final int MESSAGE_DISCONNECT = 2; public static final int MESSAGE_MESSAGE = 3; public static final int MESSAGE_CHALLENGE = 4; public static final int MESSAGE_USR_LIST = 5; public static final int MESSAGE_CH_REPLY = 6; //public static final int MESSAGE_CONNECT_REPLY = 6; private int myType; private String strData; private InetAddress myClient, target; private Object data; //Constructor for the message class public Message(int type){ myType = type; try{ myClient = InetAddress.getLocalHost(); }catch(UnknownHostException unho){ unho.printStackTrace(); System.exit(1); } }//end of constructor /* type() * * @return int myType, the type of this message instance, */ public int type(){return myType;} //getters & setters public InetAddress getSenderIP(){return myClient;} public InetAddress getTargetIP(){return target;} public String getText(){return strData;} public void setText(String newtext){strData = newtext;} public void setData(Object someData){data = someData;} public Object getData(){return data;} /* * Sets the data of this object, based on the Type of this instance. * @param Object data the data to be associated with this object. * connect/disconnect had no data, merely a type. * for MESSAGE_MESSAGE data is the string to send. * for MESSAGE_CHALLENGE data is the address of the one to challenge. */ public void setData(Object somedata, InetAddress me, InetAddress tar){ if(myType == MESSAGE_MESSAGE){ strData = (String)somedata;//data e meddelandet if(me!=null){myClient = me;} if(tar!=null){target = tar;} } else if(myType==MESSAGE_CONNECT||myType==MESSAGE_DISCONNECT||myType==MESSAGE_CH_REPLY){ strData = (String)somedata;//data e vårat namn myClient = me; target = tar; }else if(myType == MESSAGE_CHALLENGE){ //Data is Name of challengeer strData = (String)somedata;//data e vårat namn myClient = me; target = tar; }else if(myType==MESSAGE_USR_LIST){ data = new LinkedList(); data = (LinkedList)somedata; myClient = me; target = tar; } }//end of setData }//end of class