/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.applications.vitp.utils; import java.util.*; import vnsim.vehicular.simulator.RouteSegment; public abstract class VITPMessage implements java.io.Serializable { /** serialVersionUID */ private static final long serialVersionUID = -5883719321862303634L; protected String versionNumber="1.0"; //protocol version public RouteSegment source; //message source public RouteSegment dest; //message dest protected Date initialTime; //initial issuing time of message protected Date expirationTime; //expiration time of message protected String cacheControl; //not used for now protected int msgId; //unique message identifier protected static int VITP_IDINVAL=-1; // protected int contentLength; //length of body protected String msgBody; //actual message body protected static int VITP_NOTYPE=0; protected static int VITP_REQ=1; protected static int VITP_REP=2; protected int type=VITP_NOTYPE; //VITPRequest or VITPReply public boolean isRequest(){ if(type==VITP_REQ) return true; return false; } public boolean isReply(){ if(type==VITP_REP) return true; return false; } protected VITPMessage( short roadIdDst, short fromDst, short toDst, short roadIdSrc, short fromSrc, short toSrc, Date initialTime, Date expirationTime, String cacheControl, int msgId, String msgBody){ // this.roadIdDest=roadIdDest; // this.segIdDest=segIdDest; // this.roadIdSrc=roadIdSrc; // this.segIdSrc=segIdSrc; // this.dest=new SegmentID(-1,segIdDest,lat1dest,long1dest,lat2dest,long2dest); // this.source=new SegmentID(-1,segIdSrc, lat1src, long1src, lat2src, long2src); // this.dest=new RouteSegment(road1IdxDst, pt1IdxDest, road2IdxDst, pt2IdxDest); // this.source=new RouteSegment(road1IdxSrc, pt1IdxSrc, road2IdxSrc, pt2IdxSrc); this.dest=new RouteSegment(roadIdDst, fromDst, toDst); this.source=new RouteSegment(roadIdSrc, fromSrc, toSrc); this.initialTime=initialTime; this.expirationTime=expirationTime; this.cacheControl=cacheControl; this.msgId=msgId; this.msgBody=msgBody; } public String toString(){ String ret=""; // ret=ret+"Src:[ ("+source.latP1+","+source.longP1+")--("+source.latP2+","+source.longP2+")] "; // ret=ret+"Dst:[ ("+dest.latP1+","+dest.longP1+")--("+dest.latP2+","+dest.longP2+")] "; ret=ret+"Src:["+source+"] "; // ret=ret+"Dst:[FROM SEG="+dest.rdIdx1+ "; P"+dest.ptIdx1+ " To SEG"+dest.rdIdx2+ " P"+dest.ptIdx2+ "] "; ret=ret+"Dst:["+dest+"] "; if(isRequest()) ret=ret+"REQ-"; else if(isReply()) ret=ret+"REP-"; else ret=ret+"NO_TYPE-"; ret=ret+"Id:"+msgId; ret=ret+"Body:--"+msgBody+"--"; return ret; } public void setMsgId(int id){ msgId=id; } public int getId(){ return msgId; } public void setMsgIdInval(){ msgId=VITP_IDINVAL; } public boolean isIdInval(){ if (msgId==VITP_IDINVAL) return true; return false; } public RouteSegment getDest(){ return dest; } public RouteSegment getSource(){ return source; } public void addToBody(String s) { if(msgBody==null) msgBody=""; if(!msgBody.endsWith("\n")) msgBody=msgBody+"\n"; msgBody=msgBody+s+"\n"; } public String getBody(){ return msgBody; } public VITPReply getReversedVITP(){ //gets a VITPReply, by inverting source and destination of this message (copies body and id) VITPReply ret=new VITPReply(source.roadIndex,source.pt2, source.pt1, dest.roadIndex, dest.pt2, dest.pt1, new Date(), new Date(), "none", msgId, msgBody, 0, "OK"); return ret; } public abstract String getUserFriendlyText(); }