[31] | 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.routing; |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | import java.util.*; |
---|
| 10 | import java.io.*; |
---|
| 11 | |
---|
| 12 | import vnsim.applications.vitp.utils.*; |
---|
| 13 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | public class RoutingPacket implements java.io.Serializable { |
---|
| 17 | |
---|
| 18 | /** <code>serialVersionUID</code> */ |
---|
| 19 | private static final long serialVersionUID = -5883719321862303634L; |
---|
| 20 | |
---|
| 21 | public boolean toBroadcast=false; |
---|
| 22 | |
---|
| 23 | //the center of the broadcast region (only valid |
---|
| 24 | //if toBroadcast=true) |
---|
| 25 | public int roadIndex; |
---|
| 26 | public int pointIndex; |
---|
| 27 | public double radius; |
---|
| 28 | |
---|
| 29 | public VITPMessage msg; |
---|
| 30 | private ArrayList<RouteSegment> route; //the pre-calculated trajectory on which the |
---|
| 31 | //packet should travel to the destination |
---|
| 32 | //an ordered collection of RouteSegment |
---|
| 33 | private int indexCurrent; //the index of the current segment id; |
---|
| 34 | |
---|
| 35 | public RouteSegment getCurrentSegment(){ |
---|
| 36 | try{ |
---|
| 37 | return ((RouteSegment)(route.get(indexCurrent))); |
---|
| 38 | } catch (Exception ex) { |
---|
| 39 | // System.out.println("RoutingPacket.getCurrent - exception!"); |
---|
| 40 | return null; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | public RouteSegment getNextSegment(){ |
---|
| 45 | try{ |
---|
| 46 | return ((RouteSegment)(route.get(indexCurrent+1))); |
---|
| 47 | } catch (Exception ex) { |
---|
| 48 | // System.out.println("RoutingPacket.getNextSegment - exception!"); |
---|
| 49 | return null; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public RouteSegment getAfterNextSegment() { |
---|
| 54 | try{ |
---|
| 55 | return ((RouteSegment)(route.get(indexCurrent+2))); |
---|
| 56 | } catch(Exception ex) { |
---|
| 57 | // System.out.println("RoutingPacket.getNextSegment - exception!"); |
---|
| 58 | return null; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public void incrementIndex(){ |
---|
| 63 | indexCurrent++; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public RoutingPacket(VITPMessage msg, ArrayList<RouteSegment> route) { |
---|
| 67 | |
---|
| 68 | // if(route==null) { |
---|
| 69 | // System.out.println("ERROR! Route should not be null!"); |
---|
| 70 | // } |
---|
| 71 | this.msg=msg; |
---|
| 72 | this.route=route; |
---|
| 73 | this.indexCurrent=0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public String toString(){ |
---|
| 77 | String ret="Broad="+toBroadcast; |
---|
| 78 | ret=ret+msg+route; |
---|
| 79 | return ret; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public byte[] getSerializedBytes() { |
---|
| 83 | //returns the serialization of this object |
---|
| 84 | try{ |
---|
| 85 | ByteArrayOutputStream byteStream=new ByteArrayOutputStream(); |
---|
| 86 | ObjectOutputStream os=new ObjectOutputStream(new BufferedOutputStream(byteStream)); |
---|
| 87 | os.flush(); |
---|
| 88 | os.writeObject(this); |
---|
| 89 | os.close(); |
---|
| 90 | return byteStream.toByteArray(); |
---|
| 91 | } catch(Exception ex) { |
---|
| 92 | System.out.println("RoutingPacket - getSerializedBytes() - exception!:"+ex.toString()); |
---|
| 93 | return null; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public static RoutingPacket getObjectFromSerializedBytes(byte[] bytes) { |
---|
| 98 | try{ |
---|
| 99 | ByteArrayInputStream byteStream=new ByteArrayInputStream(bytes); |
---|
| 100 | ObjectInputStream is=new ObjectInputStream(new BufferedInputStream(byteStream)); |
---|
| 101 | Object o=is.readObject(); |
---|
| 102 | is.close(); |
---|
| 103 | return (RoutingPacket)o; |
---|
| 104 | } catch (Exception ex) { |
---|
| 105 | System.out.println("RoutingPacket - getObjectFromSerializedBytes() - exception!:"+ex.toString()); |
---|
| 106 | return null; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|