/************************************************************************************
* 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.routing;
import java.util.*;
import java.io.*;
import vnsim.applications.vitp.utils.*;
import vnsim.vehicular.simulator.RouteSegment;
public class RoutingPacket implements java.io.Serializable {
/** serialVersionUID
*/
private static final long serialVersionUID = -5883719321862303634L;
public boolean toBroadcast=false;
//the center of the broadcast region (only valid
//if toBroadcast=true)
public int roadIndex;
public int pointIndex;
public double radius;
public VITPMessage msg;
private ArrayList route; //the pre-calculated trajectory on which the
//packet should travel to the destination
//an ordered collection of RouteSegment
private int indexCurrent; //the index of the current segment id;
public RouteSegment getCurrentSegment(){
try{
return ((RouteSegment)(route.get(indexCurrent)));
} catch (Exception ex) {
// System.out.println("RoutingPacket.getCurrent - exception!");
return null;
}
}
public RouteSegment getNextSegment(){
try{
return ((RouteSegment)(route.get(indexCurrent+1)));
} catch (Exception ex) {
// System.out.println("RoutingPacket.getNextSegment - exception!");
return null;
}
}
public RouteSegment getAfterNextSegment() {
try{
return ((RouteSegment)(route.get(indexCurrent+2)));
} catch(Exception ex) {
// System.out.println("RoutingPacket.getNextSegment - exception!");
return null;
}
}
public void incrementIndex(){
indexCurrent++;
}
public RoutingPacket(VITPMessage msg, ArrayList route) {
// if(route==null) {
// System.out.println("ERROR! Route should not be null!");
// }
this.msg=msg;
this.route=route;
this.indexCurrent=0;
}
public String toString(){
String ret="Broad="+toBroadcast;
ret=ret+msg+route;
return ret;
}
public byte[] getSerializedBytes() {
//returns the serialization of this object
try{
ByteArrayOutputStream byteStream=new ByteArrayOutputStream();
ObjectOutputStream os=new ObjectOutputStream(new BufferedOutputStream(byteStream));
os.flush();
os.writeObject(this);
os.close();
return byteStream.toByteArray();
} catch(Exception ex) {
System.out.println("RoutingPacket - getSerializedBytes() - exception!:"+ex.toString());
return null;
}
}
public static RoutingPacket getObjectFromSerializedBytes(byte[] bytes) {
try{
ByteArrayInputStream byteStream=new ByteArrayInputStream(bytes);
ObjectInputStream is=new ObjectInputStream(new BufferedInputStream(byteStream));
Object o=is.readObject();
is.close();
return (RoutingPacket)o;
} catch (Exception ex) {
System.out.println("RoutingPacket - getObjectFromSerializedBytes() - exception!:"+ex.toString());
return null;
}
}
}