1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.applications.vitp.utils; |
---|
7 | |
---|
8 | |
---|
9 | import java.util.*; |
---|
10 | |
---|
11 | import vnsim.vehicular.simulator.RouteSegment; |
---|
12 | |
---|
13 | |
---|
14 | public abstract class VITPMessage implements java.io.Serializable { |
---|
15 | |
---|
16 | /** <code>serialVersionUID</code> */ |
---|
17 | private static final long serialVersionUID = -5883719321862303634L; |
---|
18 | |
---|
19 | protected String versionNumber="1.0"; //protocol version |
---|
20 | |
---|
21 | public RouteSegment source; //message source |
---|
22 | public RouteSegment dest; //message dest |
---|
23 | |
---|
24 | protected Date initialTime; //initial issuing time of message |
---|
25 | protected Date expirationTime; //expiration time of message |
---|
26 | |
---|
27 | protected String cacheControl; //not used for now |
---|
28 | |
---|
29 | protected int msgId; //unique message identifier |
---|
30 | |
---|
31 | protected static int VITP_IDINVAL=-1; |
---|
32 | |
---|
33 | // protected int contentLength; //length of body |
---|
34 | |
---|
35 | protected String msgBody; //actual message body |
---|
36 | |
---|
37 | protected static int VITP_NOTYPE=0; |
---|
38 | protected static int VITP_REQ=1; |
---|
39 | protected static int VITP_REP=2; |
---|
40 | |
---|
41 | protected int type=VITP_NOTYPE; //VITPRequest or VITPReply |
---|
42 | |
---|
43 | public boolean isRequest(){ |
---|
44 | if(type==VITP_REQ) |
---|
45 | return true; |
---|
46 | return false; |
---|
47 | } |
---|
48 | |
---|
49 | public boolean isReply(){ |
---|
50 | if(type==VITP_REP) |
---|
51 | return true; |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | protected VITPMessage( short roadIdDst, short fromDst, short toDst, |
---|
56 | short roadIdSrc, short fromSrc, short toSrc, |
---|
57 | Date initialTime, Date expirationTime, |
---|
58 | String cacheControl, |
---|
59 | int msgId, |
---|
60 | String msgBody){ |
---|
61 | |
---|
62 | // this.roadIdDest=roadIdDest; |
---|
63 | // this.segIdDest=segIdDest; |
---|
64 | // this.roadIdSrc=roadIdSrc; |
---|
65 | // this.segIdSrc=segIdSrc; |
---|
66 | |
---|
67 | |
---|
68 | // this.dest=new SegmentID(-1,segIdDest,lat1dest,long1dest,lat2dest,long2dest); |
---|
69 | // this.source=new SegmentID(-1,segIdSrc, lat1src, long1src, lat2src, long2src); |
---|
70 | |
---|
71 | // this.dest=new RouteSegment(road1IdxDst, pt1IdxDest, road2IdxDst, pt2IdxDest); |
---|
72 | // this.source=new RouteSegment(road1IdxSrc, pt1IdxSrc, road2IdxSrc, pt2IdxSrc); |
---|
73 | |
---|
74 | this.dest=new RouteSegment(roadIdDst, fromDst, toDst); |
---|
75 | this.source=new RouteSegment(roadIdSrc, fromSrc, toSrc); |
---|
76 | |
---|
77 | this.initialTime=initialTime; |
---|
78 | this.expirationTime=expirationTime; |
---|
79 | this.cacheControl=cacheControl; |
---|
80 | this.msgId=msgId; |
---|
81 | this.msgBody=msgBody; |
---|
82 | } |
---|
83 | |
---|
84 | public String toString(){ |
---|
85 | String ret=""; |
---|
86 | // ret=ret+"Src:[ ("+source.latP1+","+source.longP1+")--("+source.latP2+","+source.longP2+")] "; |
---|
87 | // ret=ret+"Dst:[ ("+dest.latP1+","+dest.longP1+")--("+dest.latP2+","+dest.longP2+")] "; |
---|
88 | |
---|
89 | ret=ret+"Src:["+source+"] "; |
---|
90 | // ret=ret+"Dst:[FROM SEG="+dest.rdIdx1+ "; P"+dest.ptIdx1+ " To SEG"+dest.rdIdx2+ " P"+dest.ptIdx2+ "] "; |
---|
91 | ret=ret+"Dst:["+dest+"] "; |
---|
92 | |
---|
93 | if(isRequest()) ret=ret+"REQ-"; |
---|
94 | else if(isReply()) ret=ret+"REP-"; |
---|
95 | else ret=ret+"NO_TYPE-"; |
---|
96 | ret=ret+"Id:"+msgId; |
---|
97 | ret=ret+"Body:--"+msgBody+"--"; |
---|
98 | return ret; |
---|
99 | } |
---|
100 | |
---|
101 | public void setMsgId(int id){ |
---|
102 | msgId=id; |
---|
103 | } |
---|
104 | |
---|
105 | public int getId(){ |
---|
106 | return msgId; |
---|
107 | } |
---|
108 | |
---|
109 | public void setMsgIdInval(){ |
---|
110 | msgId=VITP_IDINVAL; |
---|
111 | } |
---|
112 | |
---|
113 | public boolean isIdInval(){ |
---|
114 | if (msgId==VITP_IDINVAL) |
---|
115 | return true; |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | public RouteSegment getDest(){ |
---|
120 | return dest; |
---|
121 | } |
---|
122 | |
---|
123 | public RouteSegment getSource(){ |
---|
124 | return source; |
---|
125 | } |
---|
126 | |
---|
127 | public void addToBody(String s) { |
---|
128 | if(msgBody==null) msgBody=""; |
---|
129 | if(!msgBody.endsWith("\n")) |
---|
130 | msgBody=msgBody+"\n"; |
---|
131 | msgBody=msgBody+s+"\n"; |
---|
132 | } |
---|
133 | |
---|
134 | public String getBody(){ |
---|
135 | return msgBody; |
---|
136 | } |
---|
137 | |
---|
138 | public VITPReply getReversedVITP(){ |
---|
139 | //gets a VITPReply, by inverting source and destination of this message (copies body and id) |
---|
140 | |
---|
141 | VITPReply ret=new VITPReply(source.roadIndex,source.pt2, source.pt1, dest.roadIndex, dest.pt2, dest.pt1, |
---|
142 | new Date(), new Date(), "none", msgId, msgBody, 0, "OK"); |
---|
143 | return ret; |
---|
144 | } |
---|
145 | |
---|
146 | public abstract String getUserFriendlyText(); |
---|
147 | } |
---|