www.pudn.com > src.rar > buildDynamicNS.java, change:2009-04-12,size:68312b


/*************************************************** 
 *  This program revised by Chien-Ming Chou, NCKU CSIE, LENS lab. 
 *  Some part functions refer from TraNS 
 *  2009-02-17  
 ***************************************************/ 
import java.io.*; 
import java.util.*;  
 
import javax.swing.*; 
 
public class buildDynamicNS extends JFrame{ 
 
	String filename = ""; 
	String netFile = ""; 
	String nodefile = ""; 
	String namfile = ""; 
	 
	/************************************************** 
	 *   TraNS part -- start 
	 **************************************************/ 
	 
	public final static String NS2_PREFIX    = "event"; 
	public final static String ACCIDENT_NS2_PREFIX    = "rwagent";	 
	public final static String DynamicRerouteNS2_PREFIX    = "dragent"; 
	 
	private boolean namflag = false; 
	private boolean connectionEventFlag = false; 
	private boolean vehicleStopEventFlag = false; 
	private boolean LocationStopEventFlag = false; 
	private boolean AccidentEventFlag = false; 
	private boolean DynamicRerouteEventFlag = false; 
 
	private Vector<VehicleBasedEvent> vehicleEvents; 
    private LocationBasedEvent locationEvent; 
	 
    // Bind attributes between agent and tcl invocation 
    private double  jitterFactor         = 0.0; 
    private double  slowDownDistance     = 0.0; //distance to slow down 
    private double  broadcastInterval    = 0.0; 
    private double  restoreTime          = 0.0; 
     
    // for DynamicReroute part 
    private double  DynamicReroutejitterFactor        = 0.0; 
    private double  DynamicReroutedecisionPercentage  = 0.0; 
    private double  DynamicReroutebroadcastInterval   = 0.0; 
     
    private double  startBroadcastTime   = 0.0; 
     
    // TCL variable names for accident  
    public final static String jitterFactorVar         = "jitterfactor"; 
    public final static String slowDownDistanceVar     = "slowdistance"; 
    public final static String broadcastIntervalVar    = "brcinterv"; 
    public final static String restoreTimeVar          = "restoretime"; 
     
 // TCL variable names for dynamicReroute   
    public final static String DynamicReroutejitterFactorVar         = "reRoutejitterfactor"; 
    public final static String DynamicReroutedecisionPercentageVar   = "reRoutedecisionper"; 
    public final static String DynamicReroutebroadcastIntervalVar    = "reRoutebrcinterval"; 
     
	private String traciHostname_ 		= "unsetting"; 
	private String traciPort_ 		 	= "unsetting"; 
	private String traciPenetration_		= "unsetting"; 
	private String traciSimulationTime_	= "unsetting"; 
	int traciSetFlag = 0;	 
     
	/************************************************** 
	 *   TraNS part -- end 
	 **************************************************/ 
     
	PrintWriter  printer; 
	private int onlyMovement = 0; 
	private String temp = ""; 
	private String source = ""; 
	private String sink = ""; 
 
	private String chanType; 
	private String propType; 
	private String netifType; 
	private String macType; 
	private String queueType; 
	private String layerType; 
	private String antModel; 
	private String packetNo; 
	private String protocol; 
 
	private String topoLength; 
	private String topoWidth; 
	private String agentTriger; 
	private String routerTriger; 
	private String macTriger; 
	private String movementTriger; 
	private String stopTime; 
 
 
	public String temptime = ""; 
	public String start = ""; 
	private int hasstart = 0; 
 
	private int currid = 0; 
 
	private boolean idMobileIP = false; 
	 
	private int TCPFlag = 0; //This is the flag that indicate whether 
	//user sets the TCP/FTP parameters 
	private int UDPFlag = 0; //This is the flag that indicate whether 
	//user sets the UDP/CBR parameters 
	 
	private String udpStart_ = ""; 
	private String udpStop_  = ""; 
	private String udpPacketSize_=""; 
	private String udpRate_  = ""; 
	private String udpInterval_=""; 
	private String udpRandom_ = ""; 
	private String udpMaxpkts_ = ""; 
 
 
	private String tcpStart_ = ""; 
	private String tcpStop_  = ""; 
	private String tcpWindow_= ""; 
	private String tcpPacketSize_=""; 
	private String tcpMaxburst_ = ""; 
	private String tcpMaxcwnd_ = ""; 
 
	private int connSTime[] = new int [1000]; 
	private int connETime[] = new int [1000]; 
	public Vector nodeGroups = new Vector(); 
 
	//public Vector SRMGroupID = new Vector(); 
	//public HashMap SRMGroup = new HashMap(); 
 
	Vector initialNode = new Vector();	 
	Vector nodeMovement = new Vector(); 
	public Vector connections = new Vector(); 
	public Vector conneStartTime = new Vector(); 
	Vector numberedID = new Vector(); 
	double xBoundary = 0.0; 
	double yBoundary = 0.0; 
 
	public buildDynamicNS(String traceFile, String netXMLFile){ 
		filename = traceFile; 
		netFile = netXMLFile; 
		 
		vehicleEvents = new Vector<VehicleBasedEvent>(); 
        locationEvent = new LocationBasedEvent(); 
	} 
 
	/************************************************** 
	 *   TraNS part -- start 
	 **************************************************/ 
    public Vector<VehicleBasedEvent> getVehicleEvents() { 
        return vehicleEvents; 
    } 
     
    public void setVehicleEvents(Vector<VehicleBasedEvent> vehicleEvents) { 
        this.vehicleEvents = vehicleEvents; 
    } 
    
    public void addVehicleEvent(String nodeID, int startBrcTime, int stopBrcTime){ 
        VehicleBasedEvent newEvent = new VehicleBasedEvent(nodeID, startBrcTime, stopBrcTime); 
        vehicleEvents.add(newEvent); 
    } 
     
    public void clearVehicleEvent(){ 
        vehicleEvents.removeAllElements(); 
    } 
 
    public LocationBasedEvent getLocationEvent() { 
        return locationEvent; 
    } 
     
    public void setLocationEvent(LocationBasedEvent locationEvent) { 
        this.locationEvent = locationEvent; 
    } 
     
    public void setJitterFactor(double jitterFactor) { 
        this.jitterFactor = jitterFactor; 
    } 
     
    public void setSlowDistance(double slowDownDistance) { 
        this.slowDownDistance = slowDownDistance; 
    } 
     
    public void setBroadcastInterval(double broadcastInterval) { 
        this.broadcastInterval = broadcastInterval; 
    } 
 
    public void setRestoreTime(double restoreTime) { 
        this.restoreTime = restoreTime; 
    } 
     
    public void setDynamicRerouteJitterFactor(double jitterFactor) { 
        this.DynamicReroutejitterFactor = jitterFactor; 
    } 
     
    public void setDynamicRerouteDecisionPercentage(double decisionPercentage) { 
        this.DynamicReroutedecisionPercentage = decisionPercentage; 
    } 
     
    public void setDynamicRerouteBroadcastInterval(double broadcastInterval) { 
        this.DynamicReroutebroadcastInterval = broadcastInterval; 
    } 
         
    public double getStartBroadcastTime() { 
        return (startBroadcastTime + 5.0); 
    }     
     
    public void setTraCI(String Hostname, String Port, String Penetration, String SimulationTime) { 
    	traciHostname_ = Hostname; 
    	traciPort_ = Port; 
    	traciPenetration_ = Penetration; 
    	traciSimulationTime_ = SimulationTime;   	 
    } 
	/************************************************** 
	 *   TraNS part -- end 
	 **************************************************/     
     
	public void setOnlyMovement(int target){ 
		onlyMovement = target; 
	} 
 
	public void addConnections(Vector newConn){ 
		connections.addElement(newConn); 
	} 
	 
	public void connectionTime(int cSTime, int cETime, int index, int size){ 
		//connSTime = new int [size+1]; 
		//System.out.println("_" + cTime); 
		connSTime[index] = cSTime; 
		connETime[index] = cETime; 
	} 
	 
	public void addGroupNodes(Vector newGroup){ 
		String temp = (String)newGroup.elementAt(2); 
		if (temp.length() > 3){ 
			nodeGroups.add(temp); 
			nodeGroups.add((String)newGroup.elementAt(0)); 
		} 
	} 
 
	/*public void setSRM(String srmStart, String srmStop, String window, 
	String packetSize, String burst_time, String idle_time, 
	String rate){ 
		srmStart_ = srmStart; 
		srmStop_  = srmStop; 
 
		srmWindow_ = window;	       //max bound on window size 
		srmPacketSize_ = packetSize;   //constant size of packets generated 
 
		srmBurst_time_ = burst_time;   //average on time for generator 
		srmIdle_time_ = idle_time;     //average off time for generator 
 
		srmRate_ = rate;	      //sending rate during on time 
 
		SRMFlag = 1; 
	}*/ 
 
 
	public void setUDP(String udpStart, String udpStop, String window, 
	String packetSize, String rate, String interval, 
	String random, String maxpkts){ 
		udpStart_ = udpStart; 
		udpStop_  = udpStop; 
		 
		udpPacketSize_ = packetSize;  // the constant size of the packets generated 
		udpRate_ = rate;	   		  // sending rate 
		udpInterval_  = interval;     // interval between packets 
 
		udpRandom_ = random;	  	  // whether or not to introduce random noise 
		//in the scheduled departure times. defualt is off 
		udpMaxpkts_= maxpkts;		  //maximum number of packets to send 
 
		UDPFlag = 1; 
	} 
 
	public void setTCP(String tcpStart, String tcpStop, String window, 
	String packetSize, String maxburst, String maxcwnd){ 
		tcpStart_ = tcpStart; 
		tcpStop_  = tcpStop; 
 
		tcpWindow_   = window;		  // max bound on window size 
		tcpPacketSize_ = packetSize;  // the constant size of the packets generated 
 
		tcpMaxburst_ = maxburst;      // max # packets can send back-2-back 
		tcpMaxcwnd_  = maxcwnd;       // max # cwnd can ever be 
 
		TCPFlag = 1; 
	} 
 
	public double returnXBoundary(){ 
		return xBoundary; 
	} 
	public double returnYBoundary(){ 
		return yBoundary; 
	} 
	public String returnSimTime(){ 
		return temptime; 
	} 
	public String returnStartTime(){ 
		return start; 
	} 
	public void setMobileIP(boolean flag){ 
		idMobileIP = flag; 
	} 
	public void setNamTrace(boolean flag, String tnamfile){ 
		namflag = flag; 
		namfile = tnamfile; 
	} 
	public void setOption(String chan,String prop,String netif,String   mac, 
	String queue,String	layer,String ant,String packet,String	pro){ 
		chanType = chan; 
		propType = prop; 
		netifType = netif; 
		macType = mac; 
		queueType = queue; 
		layerType = layer; 
		antModel = ant; 
		packetNo = packet; 
		protocol = pro; 
	} 
 
	public void setGodParam(String len,String width,String stop,String agent,String route,String mac,String movement){ 
		topoLength = len; 
		topoWidth = width; 
		agentTriger = agent; 
		routerTriger = route; 
		macTriger = mac; 
		movementTriger = movement; 
		stopTime = stop; 
	} 
 
	public String clearSpace(String	l){ 
		String result	=	""; 
		int	length = l.length(); 
		int	start =	0; 
 
		while(start	< length){ 
			if((l.charAt(start)=='<')||(l.charAt(start)=='\n')){ 
				break; 
			} 
			else 
			start++; 
		} 
		return (l.substring(start)); 
	} 
 
	public String getValue(String token){ 
		int	i	=	token.length(); 
		int	start=0; 
		int	end	=	0; 
		int	no=0; 
 
		for(int j=0;j<i;j++){ 
			if((token.charAt(j) == '\"')&&(no==0)){ 
				no++; 
				start	=	j; 
			} 
			else if((token.charAt(j) == '\"')&&(no==1)){ 
				end	=	j; 
				break; 
			} 
		} 
		return (token.substring(start+1,end)); 
	} 
 
	public int countToken(String s){ 
		int	result = 0; 
		for(int i	=0;	i<s.length();i++){ 
			if(s.charAt(i) ==   '=') 
			result++; 
		} 
		return (result+1); 
	} 
 
	public String getValueName(String     token){ 
		int	i	=	token.length(); 
		int	end	=	0; 
		for(int j=0;j<i;j++){ 
			if(token.charAt(j) == '='){ 
				end	= j; 
				break; 
			} 
		} 
		return (token.substring(0,end)); 
	} 
	public void setConnectionEvent(boolean flag){ 
		connectionEventFlag = flag;		 
	} 
	 
	public void setVehicleStopEvent(boolean flag){ 
		vehicleStopEventFlag = flag;		 
	} 
	public void setLocationStopEvent(boolean flag){ 
		LocationStopEventFlag  = flag;		 
	} 
	public void setAccidentEvent(boolean flag){ 
		AccidentEventFlag  = flag;		 
	} 
	public void setDynamicRerouteEvent(boolean flag){ 
		DynamicRerouteEventFlag  = flag;		 
	} 
 
	public void build() throws Exception { 
 
		long StartTime = System.currentTimeMillis(); // calculate program 
														// execution time 
 
		ReadDumpInfo r = new ReadDumpInfo(filename); 
		r.LoadInfo(); 
		// ReadNode newnodeInfo = new ReadNode(nodefile); 
		// newnodeInfo.LoadInfo(); 
		dumpData d = r.getData(); 
 
		ArrayList allSteps = d.returnData(); 
 
		String timeID; 
		String edgeID; 
		String laneID; 
		String vehID; 
		String speed = " "; 
 
		String temx1 = " ", temy1 = " ", temx2 = " ", temy2 = " "; 
		String temlength = " "; 
		String temposition = " "; 
		String fromTemp = ""; 
		String toTemp = ""; 
 
		// This part parse XML.NET file. 
		// List<String> variable uses same index in arraryList 
		// Here are for edge information 
		List<String> listEdge = new ArrayList<String>(); 
		List<String> listFrom = new ArrayList<String>(); 
		List<String> listTo = new ArrayList<String>(); 
		List<String> listLength = new ArrayList<String>(); 
 
		// Here are for node information 
		List<String> listNode = new ArrayList<String>(); 
		List<String> listX = new ArrayList<String>(); 
		List<String> listY = new ArrayList<String>(); 
 
		// here are for current edge information 
		String existVID[] = new String[3000]; 
		String Current_source[] = new String[3000]; 
		String Current_destination[] = new String[3000];		 
		String Current_length[] = new String[3000]; 
		 
		for (int initial = 0; initial < 3000; initial++ ) { 
			existVID[initial] = ""; 
			Current_source[initial] = ""; 
			Current_destination[initial] = ""; 
			Current_length[initial] = ""; 
		} 
		 
		boolean parseFlag = false; 
 
		File roadMap = new File(netFile); 
		BufferedReader roadMapContent = new BufferedReader(new FileReader( 
				roadMap), 65535); 
		StringBuffer tempContent = new StringBuffer(); 
		String roadMapLine = roadMapContent.readLine(); 
 
		while (roadMapLine != null) { 
			// System.out.println(roadMapLine); 
			// road-map-line parse 
 
			roadMapLine = clearSpace(roadMapLine); 
 
			if (roadMapLine.equals("<net>")) 
				parseFlag = true; 
 
			// System.out.println(roadMapLine.length()); 
			// System.out.println(roadMapLine); 
 
			if (roadMapLine.length() >= 7 && parseFlag == true) { 
				StringTokenizer stringParse = new StringTokenizer(roadMapLine); 
 
				if (stringParse.countTokens() > 3) { 
					String temp = stringParse.nextToken(); 
					// System.out.println(stringParse.countTokens()); 
					// System.out.println(temp); 
 
					if (temp.compareTo("<edge") == 0 && stringParse.countTokens() == 6) { 
					//	 System.out.println(" - > "+ temp); 
 
						temp = stringParse.nextToken(); 
					//	 System.out.print(" target_1 " + getValue(temp)); 
						listEdge.add(getValue(temp)); 
 
						temp = stringParse.nextToken(); 
					//	 System.out.print(" target_2 " + getValue(temp)); 
						listFrom.add(getValue(temp)); 
 
						temp = stringParse.nextToken(); 
					//	 System.out.print(" target_3 " + getValue(temp) + "\n"); 
						listTo.add(getValue(temp)); 
 
						// read the edge length 
						roadMapLine = roadMapContent.readLine(); 
						roadMapLine = roadMapContent.readLine(); 
						roadMapLine = clearSpace(roadMapLine); 
						StringTokenizer stringParse2 = new StringTokenizer( 
								roadMapLine); 
						String temp2 = stringParse2.nextToken(); 
						if (temp2.compareTo("<lane") == 0) { 
							temp2 = stringParse2.nextToken(); 
					//		 System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
					//		 System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
					//		 System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
					//		 System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
					//		 System.out.println(getValue(temp2)); 
							listLength.add(getValue(temp2)); 
						} 
						// System.in.read(); 
					} 
 
					if (temp.compareTo("<junction") == 0 && stringParse.countTokens() == 4) { 
						temp = stringParse.nextToken(); 
						 //System.out.println(getValue(temp)); 
						listNode.add(getValue(temp)); 
 
						temp = stringParse.nextToken(); 
						//System.out.println(getValue(temp)); 
						temp = stringParse.nextToken(); 
						//System.out.println(getValue(temp)); 
						listX.add(getValue(temp)); 
 
						temp = stringParse.nextToken(); 
						//System.out.println(getValue(temp)); 
						listY.add(getValue(temp)); 
						//System.in.read(); 
					} 
				} 
				else if(stringParse.countTokens() == 3) {		 
					String temp = stringParse.nextToken(); 
					// System.out.println(stringParse.countTokens()); 
					// System.out.println(temp); 
 
					if (temp.compareTo("<edge") == 0) { 
						 //System.out.println(" - > "+ temp); 
 
						temp = stringParse.nextToken(); 
						 //System.out.print(" target_1 " + getValue(temp)); 
						listEdge.add(getValue(temp)); 
						listFrom.add(null); 
						listTo.add(null); 
						 
						// read the edge length 
						roadMapLine = roadMapContent.readLine(); 
						roadMapLine = roadMapContent.readLine(); 
						roadMapLine = clearSpace(roadMapLine); 
						//System.out.println(roadMapLine); 
						StringTokenizer stringParse2 = new StringTokenizer(roadMapLine); 
						String temp2 = stringParse2.nextToken(); 
						if (temp2.compareTo("<lane") == 0) { 
							temp2 = stringParse2.nextToken(); 
							 //System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
							 //System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken(); 
							 //System.out.println(getValue(temp2)); 
							temp2 = stringParse2.nextToken();							  
							 //System.out.println("++ " + getValue(temp2)); 
							listLength.add(getValue(temp2)); 
						} 
						//System.in.read(); 
					} 
				} 
			} 
			// StringTokenizer rMLParse= new StringTokenizer(roadMapLine); 
			// String tt = rMLParse.nextToken(); 
			roadMapLine = roadMapContent.readLine(); 
		} 
 
		// test function 
		// for(String listOut : listLength) { 
		// System.out.print(listOut + " "); 
		// } 
 
		HashMap map = new HashMap(); 
		int vehicle_counter = 0; 
 
		for (int i = 0; i < allSteps.size(); i++) { 
			// System.out.println("AllSteps = "+allSteps.size()); 
			stepInfo s = (stepInfo) allSteps.get(i); 
			ArrayList allEdges = s.returnData(); 
			timeID = s.getID(); 
 
			 //System.out.println("timeID-> " + timeID); 
 
			for (int j = 0; j < allEdges.size(); j++) { 
				// System.out.println("AllEdges = "+allEdges.size()); 
				edgeInfo e = (edgeInfo) allEdges.get(j); 
				ArrayList allLanes = e.returnData(); 
				edgeID = e.getID(); 
				//System.out.println("edgeID-> " + edgeID); 
 
				for (int m = 0; m < allLanes.size(); m++) { 
					// System.out.println("AllLanes = "+allLanes.size()); 
					laneInfo l = (laneInfo) allLanes.get(m); 
					ArrayList allVeh = l.returnData(); 
					laneID = l.getID(); 
 
					//System.out.println("laneID-> " + laneID); 
 
					for (int n = 0; n < allVeh.size(); n++) { 
						// System.out.println("AllVeh = "+allVeh.size()); 
						vehInfo v = (vehInfo) allVeh.get(n); 
						vehID = v.getID(); 
						temposition = v.getPosition(); 
						speed = v.getSpeed(); 
						 
 
						 //System.out.println("vehID-> " + vehID); 
						 //System.out.println("speed-> " + speed); 
						 //System.out.println("temposition-> " + temposition); 
						  
						String tempID = "";						 
	 
						 
						// This part start process XML.NET file to find current 
						// location of the vehicle 
 
						int findEdge_index = 0; 
						int findNode_index = 0; 
						int findLocation_index = 0; 
						boolean findNode_flag = false; 
						boolean intersection_flag = false;  
						int searchCounter = 0; 
						String findTemp; 
						 
						temx1 = ""; 
						temy1 = ""; 
						temx2 = ""; 
						temy2 = "";						 
						 
						// update current vehicle information 
						for (int foundnode = 0 ; vehicle_counter != 0 && foundnode < 3000 ; foundnode ++ ) { 
							if (existVID[foundnode].compareTo(vehID) == 0) 
							{ 
								findLocation_index = foundnode; 
								findNode_flag = true; 
								//System.out.println("!! find exist vehicle ->" + vehID + "  index -> " + findLocation_index); 
								break; 
							}							 
						} 
													 
						if(findNode_flag == false) 
						{							 
							existVID[vehicle_counter] = vehID; 
							//System.out.println("cannot find exist vehicle ->" + vehID + "  index -> " + vehicle_counter); 
						} 
					 
	 
						for (String findEdge : listEdge) { 
							findTemp = listEdge.get(findEdge_index); 
							if (findTemp.compareTo(edgeID) == 0) { 
								fromTemp = listFrom.get(findEdge_index); 
								toTemp = listTo.get(findEdge_index); 
								temlength = listLength.get(findEdge_index); 
 
								 // the vehicle is moving on intersection 
								 if(fromTemp != null) { 
									 if (findNode_flag == true) { 
										 Current_source[findLocation_index] = fromTemp; 
										 Current_destination[findLocation_index] = toTemp; 
										 Current_length[findLocation_index] = temlength; 
										 //System.out.println(" --> index " + findLocation_index); 
									 } 
									 else 
									 {										  
										 Current_source[vehicle_counter] = fromTemp; 
										 Current_destination[vehicle_counter] = toTemp; 
										 Current_length[vehicle_counter] = temlength; 
										 //System.out.println(" --> index " + vehicle_counter); 
										 vehicle_counter ++; 
									 }									 	 
								 } 
								 else 
								 { 
									 intersection_flag = true;									  
									 fromTemp = Current_source[findLocation_index]; 
									 toTemp = Current_destination[findLocation_index];									  
								 } 
 
								 //System.out.print("edgeID -> " + findTemp + " "); 
								 //System.out.print(fromTemp + " " + toTemp + " ");								  
								 //System.out.println(" L-> " + temlength + "\n"); 
								  
								for (String findNode : listNode) { 
									findTemp = listNode.get(findNode_index); 
									if (findTemp.compareTo(fromTemp) == 0) { 
										 
										temx1 = listX.get(findNode_index); 
										temy1 = listY.get(findNode_index); 
										searchCounter++; 
									} 
 
									if (findTemp.compareTo(toTemp) == 0) { 
										 
										temx2 = listX.get(findNode_index); 
										temy2 = listY.get(findNode_index); 
										searchCounter++; 
									} 
 
									if (searchCounter >= 2) 
										break; 
									findNode_index++; 
								} 
								 //System.out.print(" " + temx1 + " " + temy1); 
								 //System.out.print(" " + temx2 + " " + temy2 + "\n\n");								 
							} 
							findEdge_index++; 
						} 
						 
						Double xfrom, yfrom, xto, yto, position, length; 
						Double extra_pos, extra_len; 
						double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0; 
						double ratio = 0.0; 
 
						double a = 0.0, b = 0.0; 
 
						xfrom = new Double(temx1); 
						xto = new Double(temx2); 
 
						yfrom = new Double(temy1); 
						yto = new Double(temy2); 
 
						// System.out.println("x from = " + xfrom + " x to =" + 
						// xto + " y from = " + yfrom + " y to = " + yto); 
 
						if (xfrom.doubleValue() > xBoundary) 
							xBoundary = xfrom.doubleValue(); 
						if (xto.doubleValue() > xBoundary) 
							xBoundary = xto.doubleValue(); 
						if (yfrom.doubleValue() > yBoundary) 
							yBoundary = yfrom.doubleValue(); 
						if (yto.doubleValue() > yBoundary) 
							yBoundary = yto.doubleValue(); 
 
						position = new Double(temposition); 
						length = new Double(temlength); 
						 
						if (intersection_flag == true) { 
							extra_pos = new Double(Current_length[findLocation_index]); 
							extra_len = new Double(Current_length[findLocation_index]);							 
							position += extra_pos; 
							length += extra_len; 
						} 
						 
						//System.out.println("pos =" + position + " length =" + length); 
						 
						//if (vehID.compareTo("flow10A_0") == 0) 
						//	System.in.read(); 
						 
						if ((xfrom.doubleValue() < xto.doubleValue()) 
								&& (yfrom.doubleValue() != yto.doubleValue())) { 
							// System.out.println("1"); 
							x1 = xfrom.doubleValue(); 
							y1 = yfrom.doubleValue(); 
 
							x2 = xto.doubleValue(); 
							y2 = yto.doubleValue(); 
 
							ratio = position.doubleValue() 
									/ length.doubleValue(); 
 
							a = x2 - ratio * (x2 - x1); 
 
							if (y2 > y1) 
								b = y2 - ratio * (y2 - y1); 
							else 
								b = y1 - ratio * (y1 - y2); 
 
							// if(y1<y2) 
							// ratio = 
							// (length.doubleValue()-position.doubleValue())/length.doubleValue(); 
							// else 
							// ratio = 
							// position.doubleValue()/length.doubleValue(); 
						} else if ((xfrom.doubleValue() > xto.doubleValue()) 
								&& (yfrom.doubleValue() != yto.doubleValue())) { 
							// System.out.println("2 pos = " + position + " x 
							// from = " + xfrom + " x to = " + xto + " y from = 
							// " + yfrom + " y to = " + yto); 
							x1 = xto.doubleValue(); 
							y1 = yto.doubleValue(); 
 
							x2 = xfrom.doubleValue(); 
							y2 = yfrom.doubleValue(); 
 
							ratio = position.doubleValue() 
									/ length.doubleValue(); 
 
							a = x2 - ratio * (x2 - x1); 
 
							if (y2 > y1) 
								b = y2 - ratio * (y2 - y1); 
							else 
								b = y1 - ratio * (y1 - y2); 
 
							// if(y1<y2) 
							// ratio = 
							// (length.doubleValue()-position.doubleValue())/length.doubleValue(); 
							// else 
							// ratio = 
							// position.doubleValue()/length.doubleValue(); 
						} 
 
						if ((xfrom.doubleValue() == xto.doubleValue()) 
								&& (yfrom.doubleValue() < yto.doubleValue())) { 
							// System.out.println("3"); 
							x1 = xfrom.doubleValue(); 
							y1 = yfrom.doubleValue(); 
 
							x2 = xto.doubleValue(); 
							y2 = yto.doubleValue(); 
 
							a = x1; 
							b = y1 + position.doubleValue(); 
						} else if ((xfrom.doubleValue() == xto.doubleValue()) 
								&& (yfrom.doubleValue() > yto.doubleValue())) { 
							// System.out.println("4"); 
							x1 = xto.doubleValue(); 
							y1 = yto.doubleValue(); 
 
							x2 = xfrom.doubleValue(); 
							y2 = yfrom.doubleValue(); 
 
							a = x1; 
							b = y2 - position.doubleValue(); 
							// b = 
							// y1+length.doubleValue()-position.doubleValue(); 
						} else if ((yfrom.doubleValue() == yto.doubleValue()) 
								&& (xfrom.doubleValue() < xto.doubleValue())) { 
							// System.out.println("5"); 
							x1 = xfrom.doubleValue(); 
							y1 = yfrom.doubleValue(); 
 
							x2 = xto.doubleValue(); 
							y2 = yto.doubleValue(); 
 
							a = x1 + position.doubleValue(); 
							b = y1; 
						} else if ((yfrom.doubleValue() == yto.doubleValue()) 
								&& (xfrom.doubleValue() > xto.doubleValue())) { 
							// System.out.println("6"); 
							x1 = xto.doubleValue(); 
							y1 = yto.doubleValue(); 
 
							x2 = xfrom.doubleValue(); 
							y2 = yfrom.doubleValue(); 
 
							// a = 
							// x1+length.doubleValue()-position.doubleValue(); 
							a = x2 - position.doubleValue(); 
							b = y1; 
						} 
						/* 
						 * else if(y1<y2){ System.out.println(" 7 ratio=" + 
						 * ratio); a = x2-ratio*(x2-x1); b = y2-ratio*(y2-y1); } 
						 * else if(y1>y2){ System.out.println(" 8 ratio=" + 
						 * ratio); a = x1+ratio*(x2-x1); b = y1-ratio*(y1-y2); } 
						 */ 
 
						if (map.isEmpty()) { 
							// System.out.println(a+ " " + b); 
							NSNode tempNode = new NSNode(timeID, vehID, a, b); 
							map.put(vehID, tempNode); 
							initialNode.add(tempNode); 
							// System.out.println(timeID); 
 
							Vector idnode = new Vector(); 
							idnode.addElement(new Integer(currid)); 
							idnode.addElement(vehID); 
							idnode.addElement(timeID); 
							numberedID.addElement(idnode); 
							currid++; 
							temptime = timeID; 
 
							if (hasstart == 0) { 
								start = timeID; 
								hasstart = 1; 
							} 
 
						} else { 
							if (map.containsKey(vehID)) { 
								// System.out.println(a+ " " + b); 
								NSNode tempNode = new NSNode(timeID, vehID, a, 
										b); 
								tempNode.setSpeed(speed); 
								tempNode.setTimestep(timeID); 
								nodeMovement.add(tempNode); 
								temptime = timeID; 
								// System.out.println(timeID); 
							} else { 
								NSNode tempNode = new NSNode(timeID, vehID, a, 
										b); 
								map.put(vehID, tempNode); 
								initialNode.add(tempNode); 
 
								Vector idnode = new Vector(); 
								idnode.addElement(new Integer(currid)); 
								idnode.addElement(vehID); 
								numberedID.addElement(idnode); 
								currid++; 
								temptime = timeID; 
								// System.out.println(timeID); 
								if (hasstart == 0) { 
									start = timeID; 
									hasstart = 1; 
								} 
							} 
						} 
					} 
				} 
			} 
		} 
		// System.out.println("over"); 
		long ProcessTime = System.currentTimeMillis() - StartTime; 
		System.out.println("Total Tcl translation time (ms) = " + ProcessTime); 
		d = null; 
		r = null; 
		allSteps = null; 
		existVID = null; 
		Current_source = null; 
		Current_destination = null; 
	} 
 
	public void write(String tclFile, String traceFile){ 
		 
		//For creat node move pattern 
		int NodeCheckFlag[] = new int [numberedID.size()+1]; 
		int NodeCountFlag[] = new int [numberedID.size()+1];		 
		 
		//System.out.println(numberedID.size()); 
		 
		try{ 
			printer = new PrintWriter(new BufferedWriter(new FileWriter(tclFile), 32768)); 
		} 
		catch(IOException e){ 
			System.out.println("print error!"); 
		}; 
 
		float localNodeTime; 
		if(idMobileIP == true){ 
			printer.print("# ======================================================================\n"); 
			printer.print("# Define options\n"); 
			printer.print("# ======================================================================\n"); 
 
			printer.print("set opt(chan)   "+chanType+"		      \t;# channel type\n"); 
			printer.print("set opt(prop)   "+propType+"		      \t;# radio-propagation model\n"); 
			printer.print("set opt(netif)  "+netifType+"		     \t;# network interface type\n"); 
			printer.print("set opt(mac)    "+macType+"		      ;# MAC type\n"); 
			printer.print("set opt(ifq)    "+queueType+"		      ;# interface queue type\n"); 
			printer.print("set opt(ll)     "+layerType+"		      ;# link layer type\n"); 
			printer.print("set opt(ant)    "+antModel+"		      ;# antenna model\n"); 
			printer.print("set opt(ifqlen) "+packetNo+"		      ;# max packet in ifq\n"); 
			printer.print("set opt(nn)     "+(initialNode.size()-4)+"	  ;# number of mobilenodes\n"); 
			printer.print("set opt(adhocRouting)	 "+protocol+"	      ;# routing protocol\n\n"); 
 
			printer.print("set opt(cp)     \"\"			      ;# cp file not used\n"); 
			printer.print("set opt(sc)     \"\"			      ;# node movement file\n\n"); 
 
			int xp = (int) returnXBoundary()+1; 
			int yp = (int) returnYBoundary()+1; 
 
			printer.print("set opt(x)      "+ xp+"			      ;# x coordinate of topology\n"); 
			printer.print("set opt(y)      "+ yp+"			      ;# y coordinate of topology\n"); 
 
			printer.print("set opt(seed)   0.0			      ;# random seed\n"); 
			printer.print("set opt(stop)   "+stopTime+"		      ;# time to stop simulation\n\n"); 
 
			printer.print("set opt(ftp1-start)	12.0\n"); 
			printer.print("set num_wired_nodes	2\n"); 
 
			printer.print("# ======================================================================\n"); 
			printer.print("# Main Program\n"); 
			printer.print("# ======================================================================\n"); 
 
			printer.print("# create simulator instance\n"); 
			printer.print("set ns_	 [new Simulator]\n\n"); 
 
			printer.print("# set up for hierarchical routing\n"); 
			printer.print("$ns_ node-config -addressType hierarchical\n\n"); 
 
			printer.print("AddrParams set domain_num_ 3	      ;# number of domains\n"); 
			printer.print("lappend cluster_num 2 1 1	      ;# number of clusters in each domain\n"); 
			printer.print("AddrParams set cluster_num_ $cluster_num\n"); 
			printer.print("lappend eilastlevel 1 1 2 1	      ;# number of nodes in each cluster\n"); 
			printer.print("AddrParams set nodes_num_ $eilastlevel ;# of each domain\n"); 
 
			printer.print("set tracefd  [open "+traceFile+ " w]\n"); 
			printer.print("$ns_ trace-all $tracefd\n"); 
 
			if (namflag == true){ 
				printer.print("set namtrace [open "+namfile+" w]\n"); 
				printer.print("$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)\n\n"); 
			} 
			printer.print("# Create topography object\n"); 
			printer.print("set topo   [new Topography]\n\n"); 
 
			printer.print("# define topology\n"); 
			printer.print("$topo load_flatgrid $opt(x) $opt(y)\n\n"); 
 
			printer.print("# create God\n"); 
			printer.print("create-god [expr $opt(nn) + 4]\n"); //one for HA and 3 for FA 
			printer.print("#create wired nodes\n"); 
			printer.print("set temp {0.0.0 0.1.0}		;# hierarchical addresses\n"); 
			printer.print("for {set i 0} {$i < $num_wired_nodes} {incr i} {\n"); 
			printer.print("    set W($i) [$ns_ node [lindex $temp $i]]\n"); 
			printer.print("}\n\n"); 
 
			printer.print("# Configure for ForeignAgent and HomeAgent nodes\n"); 
			printer.print("$ns_ node-config -mobileIP ON \\\n"); 
			printer.print(" 		-adhocRouting $opt(adhocRouting) \\\n"); 
			printer.print(" 		-llType $opt(ll) \\\n"); 
			printer.print(" 		-macType $opt(mac) \\\n"); 
			printer.print(" 		-ifqType $opt(ifq) \\\n"); 
			printer.print(" 		-ifqLen $opt(ifqlen) \\\n"); 
			printer.print(" 		-antType $opt(ant) \\\n"); 
			printer.print(" 		-propType $opt(prop) \\\n"); 
			printer.print(" 		-phyType $opt(netif) \\\n"); 
			printer.print(" 		-channelType $opt(chan) \\\n"); 
			printer.print(" 		-topoInstance $topo \\\n"); 
			printer.print(" 		-wiredRouting ON \\\n"); 
			printer.print(" 		-agentTrace ON \\\n"); 
			printer.print(" 		-routerTrace OFF \\\n"); 
			printer.print(" 		-macTrace OFF\n\n"); 
 
			printer.print("# Create HA and FA\n"); 
			printer.print("set HA [$ns_ node 1.0.0]\n"); 
			printer.print("set FA [$ns_ node 2.0.0]\n"); 
			printer.print("$HA random-motion 0\n"); 
			printer.print("$FA random-motion 0\n\n"); 
 
			printer.print("# Position (fixed) for base-station nodes (HA & FA).\n"); 
 
			int	listLength = initialNode.size(); 
			for(int index=0; index<listLength; index++){ 
				NSNode temNode = (NSNode) initialNode.get(index); 
				String tempData = temNode.getID(); 
				if(tempData.compareTo("HA")==0){ 
				   printer.print("$HA set X_ "+temNode.getX()+"\n"); 
				   printer.print("$HA set Y_ "+temNode.getY()+"\n"); 
				   printer.print("$HA set Z_ 0.0\n"); 
				   printer.print("\n"); 
				   break; 
				} 
			} 
			//printer.print("$HA set X_ 0.000000000000\n"); 
			//printer.print("$HA set Y_ 5.000000000000\n"); 
			//printer.print("$HA set Z_ 0.000000000000\n\n"); 
 
			listLength = initialNode.size(); 
			for(int index=0; index<listLength; index++){ 
				NSNode temNode = (NSNode) initialNode.get(index); 
				String tempData = temNode.getID(); 
				if(tempData.compareTo("FA")==0){ 
				   printer.print("$FA set X_ "+temNode.getX()+"\n"); 
				   printer.print("$FA set Y_ "+temNode.getY()+"\n"); 
				   printer.print("$FA set Z_ 0.0\n"); 
				   printer.print("\n"); 
				   break; 
				} 
			} 
			//printer.print("$FA set X_ 190.000000000000\n"); 
			//printer.print("$FA set Y_ 190.000000000000\n"); 
			//printer.print("$FA set Z_ 0.000000000000\n\n"); 
 
			printer.print("# create a mobilenode\n"); 
			printer.print("$ns_ node-config -wiredRouting OFF\n\n"); 
 
			printer.print("set MH [$ns_ node 1.0.1]\n"); 
			printer.print("set node_(0) $MH\n"); 
			printer.print("set HAaddress [AddrParams addr2id [$HA node-addr]]\n"); 
			printer.print("[$MH set regagent_] set home_agent_ $HAaddress\n\n"); 
 
			printer.print("# Initialize of the MH\n"); 
 
			listLength = initialNode.size(); 
			for(int index=0; index<listLength; index++){ 
				NSNode temNode = (NSNode) initialNode.get(index); 
				String tempData = temNode.getID(); 
 
				if(tempData.compareTo("MobileHost") == 0){ 
					double tempXPos = temNode.getX() + 0.005; 
					double tempYPos = temNode.getY() + 0.005; 
 
					printer.print("$MH set Z_ 0.000000000000\n"); 
					printer.print("$MH set X_ "+tempXPos+"\n"); 
					printer.print("$MH set Y_ "+tempYPos+"\n\n"); 
 
				} 
			} 
			printer.print("# Now define the movement of the MH\n"); 
			listLength = nodeMovement.size(); 
			//System.out.println(nodeMovement.size()); 
			for(int index=0; index<listLength; index++){ 
				NSNode temNode = (NSNode) nodeMovement.get(index); 
				//System.out.println(temNode.getTimestep()); 
				//System.out.println(temNode.getID()); 
				String tempData = temNode.getID(); 
 
				String tempx = ""+temNode.getX(); 
				String tempy = ""+temNode.getY(); 
 
				if (tempx.equals("0.0")){ 
					tempx = "0.005"; 
				} 
				else if (tempx.equals(topoWidth)){ 
					tempx = "" + (temNode.getX() -1.0); 
				} 
				if (tempy.equals("0.0")){ 
					tempy = "0.005"; 
				} 
				else if (tempy.equals(topoLength)){ 
					tempy = "" + (temNode.getY() -1.0); 
				} 
				if(tempData.compareTo("MobileHost") == 0){ 
					localNodeTime = Integer.parseInt(temNode.getTimestep()); 
					localNodeTime -= 1;		//for synchronous  
					printer.print("$ns_ at "+ localNodeTime +" \"$MH setdest "+ 
					tempx+" "+tempy+" "+ temNode.getSpeed()+"\"\n"); 
				} 
			} 
			printer.print("$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail\n"); 
			printer.print("$ns_ duplex-link $W(1) $HA 5Mb 30ms DropTail\n"); 
			printer.print("$ns_ duplex-link $W(1) $FA 5Mb 200ms DropTail\n\n"); 
 
			printer.print("$ns_ duplex-link-op $W(0) $W(1) orient down\n"); 
			printer.print("$ns_ duplex-link-op $W(1) $HA orient left\n"); 
			printer.print("$ns_ duplex-link-op $W(1) $FA orient right-up\n\n"); 
 
			printer.print("# setup TCP connections between a wired node and the MobileHost\n\n"); 
			printer.print("set tcp1 [new Agent/TCP]\n"); 
			printer.print("$tcp1 set class_ 2\n"); 
			printer.print("set sink1 [new Agent/TCPSink]\n"); 
			printer.print("$ns_ attach-agent $W(0) $tcp1\n"); 
			printer.print("$ns_ attach-agent $MH $sink1\n"); 
			printer.print("$ns_ connect $tcp1 $sink1\n"); 
			printer.print("set ftp1 [new Application/FTP]\n"); 
			printer.print("$ftp1 attach-agent $tcp1\n"); 
			printer.print("$ns_ at $opt(ftp1-start) \"$ftp1 start\"\n\n"); 
 
			printer.print("# source connection-pattern and node-movement scripts\n"); 
 
			printer.print("set opt(cp) \"none\"\n"); 
			printer.print("set opt(sc) \"none\"\n"); 
 
			printer.print("# Define initial node position in nam\n"); 
			printer.print("for {set i 0} {$i < $opt(nn)} {incr i} {\n"); 
			printer.print("    $ns_ initial_node_pos $node_($i) 20\n"); 
			printer.print("}\n\n"); 
 
			printer.print("# Tell all nodes when the siulation ends\n"); 
			printer.print("for {set i 0} {$i < $opt(nn) } {incr i} {\n"); 
			printer.print("    $ns_ at $opt(stop).0 \"$node_($i) reset\";\n"); 
			printer.print("}\n\n"); 
 
			printer.print("$ns_ at $opt(stop).0 \"$HA reset\";\n$ns_ at $opt(stop).0 \"$FA reset\";\n\n"); 
 
			printer.print("$ns_ at $opt(stop).0002 \"puts \\\"NS EXITING...\\\" ; $ns_ halt\"\n\n"); 
			printer.print("$ns_ at $opt(stop).0001 \"stop\"\n\n"); 
			printer.print("proc stop {} {\n"); 
			printer.print("    global ns_ tracefd"); 
			if (namflag == true) 
				printer.print(" namtrace\n"); 
			else 
				printer.print("\n"); 
			printer.print("    $ns_ flush-trace\n"); 
			printer.print("    close $tracefd\n"); 
			if (namflag == true) 
			printer.print("    close $namtrace\n"); 
			printer.print("}\n\n"); 
 
			printer.print("# some useful headers for tracefile\n"); 
			printer.print("puts $tracefd \"M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp \\\n"); 
			printer.print("        $opt(adhocRouting)\"\n"); 
			printer.print("puts $tracefd \"M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)\"\n"); 
			printer.print("puts $tracefd \"M 0.0 prop $opt(prop) ant $opt(ant)\"\n\n"); 
			printer.print("puts \"Starting Simulation...\"\n"); 
			printer.print("$ns_ run\n"); 
			printer.close(); 
			return; 
		} 
		else{ 
			//no mobile node part  
			printer.print("# ======================================================================\n"); 
			printer.print("# Define options\n"); 
			printer.print("# ======================================================================\n"); 
 
			printer.print("set val(chan)   "+chanType+"		      ;# channel type\n"); 
			printer.print("set val(prop)   "+propType+"		      ;# radio-propagation model\n"); 
			printer.print("set val(netif)  "+netifType+"		      ;# network interface type\n"); 
			printer.print("set val(mac)    "+macType+"		      ;# MAC type\n"); 
			printer.print("set val(ifq)    "+queueType+"		      ;# interface queue type\n"); 
			printer.print("set val(ll)     "+layerType+"		      ;# link layer type\n"); 
			printer.print("set val(ant)    "+antModel+"		      ;# antenna model\n"); 
			printer.print("set val(ifqlen) "+packetNo+"		      ;# max packet in ifq\n"); 
			printer.print("set val(nn)     "+initialNode.size()+"	      ;# number of mobilenodes\n"); 
			printer.print("set val(rp)     "+protocol+"		      ;# routing protocol\n\n"); 
 
			printer.print("set opt(x)      "+ topoLength+"			      ;# x coordinate of topology\n"); 
			printer.print("set opt(y)      "+ topoWidth+"			     ;# y coordinate of topology\n"); 
 
			// Trace Directories and Files 
			printer.println("# Trace Variables"); 
			//printer.println("set opt(tracedir)   ./files/"); 
			//printer.println("set opt(filename)   $opt(tracedir)out           ;# base filename for traces"); 
			printer.println("set opt(vnfilen)    trace-vanet.tr     ;# vanet tracelog"); 
			//printer.println("set opt(tr)         $opt(tracedir)output.tr     ;# trace file"); 
			printer.println(); 
			 
			if ( AccidentEventFlag  == true) { 
				printer.print("\n"); 
				printer.println("set opt(" + jitterFactorVar + ")\t\t"      + jitterFactor        + "\t\t;# jitter for the message broadcast"); 
				printer.println("set opt(" + slowDownDistanceVar + ")\t\t"  + slowDownDistance       + "\t\t;# decision distance from the node triggering an event"); 
				printer.println("set opt(" + broadcastIntervalVar + ")\t\t" + broadcastInterval   + "\t\t;# interval for sending Road Warning Messages"); 
				printer.println("set opt(" + restoreTimeVar + ")\t\t"       + restoreTime        + "\t\t;# time after the last packet received to restore node"); 
				printer.print("\n"); 
			} 
			 
			if ( DynamicRerouteEventFlag == true) {				 
		        printer.println( ); 
		        printer.println("set opt(" + DynamicReroutejitterFactorVar          + ")\t\t"       + DynamicReroutejitterFactor        + "\t\t;# jitter for the message broadcast"); 
		        printer.println("set opt(" + DynamicReroutedecisionPercentageVar    + ")\t\t" + DynamicReroutedecisionPercentage  + "\t\t;# traveled distance percentage to reroute"); 
		        printer.println("set opt(" + DynamicReroutebroadcastIntervalVar     + ")\t\t"  + DynamicReroutebroadcastInterval   + "\t\t;# time interval for the message broadcast"); 
		         
			} 
						 
			printer.print("# ======================================================================\n"); 
			printer.print("# Main Program\n"); 
			printer.print("# ======================================================================\n"); 
 
			printer.print("# \n"); 
			printer.print("# Initialize Global Variables\n"); 
			printer.print("# \n"); 
 
			printer.print("set ns_ [new Simulator]\n"); 
			printer.print("set tracefd [open "+ traceFile+" w]\n"); 
			printer.print("$ns_ trace-all $tracefd\n\n"); 
 
			if (namflag == true){ 
				printer.print("set namtrace [open "+namfile+" w]\n"); 
				printer.print("$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)\n\n"); 
			} 
 
			printer.println("set vanettracefd [open $opt(vnfilen) w]"); 
            printer.println("set VanetTrace   [new Trace/Generic]"); 
            printer.println("$VanetTrace attach $vanettracefd"); 
             
			printer.print("# set up topography object\n"); 
			printer.print("set topo    [new Topography]\n"); 
			printer.print("$topo load_flatgrid $opt(x) $opt(y)\n\n"); 
 
			printer.print("# \n"); 
			printer.print("# Create God\n"); 
			printer.print("# \n"); 
 
			printer.print("create-god $val(nn)\n\n"); 
			printer.println("set chan_1_ [new $val(chan)]"); 
 
			 
            // Mobility Interface Client (TraCI) 
			printer.println("# ======================================================================"); 
			printer.println("#       TraCI Connection Setup"); 
			printer.println("# ======================================================================"); 
			printer.println("set mobilityInterfaceClient [new TraCIClient]"); 
			printer.println("$mobilityInterfaceClient set-remoteHost " + traciHostname_); 
			printer.println("$mobilityInterfaceClient set-remotePort " + traciPort_); 
			printer.println("$mobilityInterfaceClient set-timeInterval 1.0"); 
            printer.println("puts \"Connect to TraCI server\""); 
            printer.println("$mobilityInterfaceClient connect"); 
            printer.println("$mobilityInterfaceClient startSimStepHandler"); 
            printer.println(); 
			 
			printer.print("# ======================================================================\n"); 
			printer.print("# Configure node\n"); 
			printer.print("# ======================================================================\n"); 
			printer.print("$ns_ node-config  -adhocRouting $val(rp) \\\n"); 
			printer.print(" 		 -llType $val(ll) \\\n"); 
			printer.print(" 		 -macType $val(mac) \\\n"); 
			printer.print(" 		 -ifqType $val(ifq) \\\n"); 
			printer.print(" 		 -ifqLen $val(ifqlen) \\\n"); 
			printer.print(" 		 -antType $val(ant) \\\n"); 
			printer.print(" 		 -propType $val(prop) \\\n"); 
			printer.print(" 		 -phyType $val(netif) \\\n"); 
			printer.print(" 		 -channelType $val(chan) \\\n"); 
			printer.print(" 		 -topoInstance $topo \\\n"); 
			printer.print(" 		 -agentTrace "+agentTriger+" \\\n"); 
			printer.print(" 		 -routerTrace "+routerTriger+" \\\n"); 
			printer.print(" 		 -macTrace "+macTriger+" \\\n"); 
			printer.print(" 		 -movementTrace "+movementTriger+"\n\n"); 
			 
			printer.println("Agent set debug_ true"); 
 
			printer.println("# define color index"); 
			printer.println("$ns_ color 0 red"); 
			printer.println("$ns_ color 1 green"); 
			printer.println("$ns_ color 2 chocolate"); 
			printer.println("$ns_ color 3 red"); 
			printer.println("$ns_ color 4 brown"); 
			printer.println("$ns_ color 5 tan"); 
			printer.println("$ns_ color 6 gold"); 
			printer.println("$ns_ color 7 black"); 
			printer.println(); 
			 
			printer.print("# ======================================================================\n"); 
			printer.print("# Created nodes\n"); 
			printer.print("# ======================================================================\n"); 
			 
			printer.print("for {set i 0} {$i < $val(nn)} {incr i} {\n"); 
			printer.print("  set node_($i) [$ns_ node]\n"); 
			printer.print("  $node_($i) random-motion 0 ;# disable random motion\n"); 
			printer.println("  $node_($i) color \"black\""); 
			printer.println("  $node_($i) shape \"circle\"");             
			printer.println("  $mobilityInterfaceClient add-node $node_($i)"); 
			printer.print("}\n");			 
 
			if ( vehicleStopEventFlag == true || LocationStopEventFlag == true ) { 
				printer.println("# ======================================================================"); 
	        	printer.println("#            Agent for VANET Event Handling"); 
	        	printer.println("# ======================================================================"); 
	        	printer.println("for {set i 0} {$i < [expr $val(nn)]} {incr i} {"); 
	        	printer.println("   set " + NS2_PREFIX + "($i) [new Agent/VanetEventHandler $i]"); 
	        	printer.println("   $node_($i) attach $" + NS2_PREFIX + "($i) 250"); 
	        	printer.println("   $ns_ attach-agent $node_($i) $" + NS2_PREFIX + "($i)"); 
	         
	        	printer.println("   $" + NS2_PREFIX + "($i) log-target $VanetTrace"); 
	        	printer.println("   set ll($i) [$node_($i) set ll_(0)]"); 
	        	printer.println("   set ifq($i) [$node_($i) set ifq_(0)]"); 
	        	printer.println("   $" + NS2_PREFIX + "($i) add-ll $ll($i)"); 
	        	printer.println("   $mobilityInterfaceClient start-on-move $node_($i) $" + NS2_PREFIX + "($i)"); 
	        	printer.println("   $mobilityInterfaceClient stop-on-halt $node_($i) $" + NS2_PREFIX + "($i)"); 
	         
	        	printer.println("}"); 
	        	printer.println(); 
			} 
			 
			if ( AccidentEventFlag == true ) { 
				printer.println("# ======================================================================"); 
				printer.println("#            Agent for Road Warning Application"); 
				printer.println("# ======================================================================"); 
				printer.println("for {set i 0} {$i < [expr $val(nn)]} {incr i} {"); 
				printer.println("   set " + ACCIDENT_NS2_PREFIX + "($i) [new Agent/VanetRoadWarning $i]"); 
				printer.println("   $node_($i) attach $" + ACCIDENT_NS2_PREFIX + "($i) 250"); 
				printer.println("   $ns_ attach-agent $node_($i) $" + ACCIDENT_NS2_PREFIX + "($i)"); 
		         
		        // Link the event handler agent to the application agent 
				printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) attach-eventHandler $" + NS2_PREFIX + "($i)"); 
		         
				printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) log-target $VanetTrace"); 
				printer.println("   set ll($i) [$node_($i) set ll_(0)]"); 
		        printer.println("   set ifq($i) [$node_($i) set ifq_(0)]"); 
		        printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) add-ll $ll($i)"); 
		        printer.println("   $mobilityInterfaceClient start-on-move $node_($i) $" + ACCIDENT_NS2_PREFIX + "($i)"); 
		        printer.println("   $mobilityInterfaceClient stop-on-halt $node_($i) $" + ACCIDENT_NS2_PREFIX + "($i)"); 
		        printer.println(); 
		        printer.println("# ================ Agent Parameters ==================="); 
		        printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) set jitterFactor_    $opt(" + jitterFactorVar       + ")"); 
		        printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) set slowdown_dist_   $opt(" + slowDownDistanceVar   + ")"); 
		        printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) set rwm_interval_    $opt(" + broadcastIntervalVar  + ")"); 
		        printer.println("   $" + ACCIDENT_NS2_PREFIX + "($i) set restoring_time_  $opt(" + restoreTimeVar        + ")"); 
		         
		        printer.println("}"); 
		        printer.println(); 
			} 
			 
			if ( DynamicRerouteEventFlag == true) { 
				printer.println("# ======================================================================"); 
				printer.println("#           Agent for Dynamic Reroute Application"); 
				printer.println("# ======================================================================"); 
				printer.println("for {set i 0} {$i < [expr $val(nn)]} {incr i} {"); 
				printer.println("   set " + DynamicRerouteNS2_PREFIX + "($i) [new Agent/DynamicReroute $i]"); 
				printer.println("   $node_($i) attach $" + DynamicRerouteNS2_PREFIX + "($i) 250"); 
				printer.println("   $ns_ attach-agent $node_($i) $" + DynamicRerouteNS2_PREFIX + "($i)"); 
		         
				printer.println("   $" + DynamicRerouteNS2_PREFIX + "($i) log-target $VanetTrace"); 
				printer.println("   set ll($i) [$node_($i) set ll_(0)]"); 
				printer.println("   set ifq($i) [$node_($i) set ifq_(0)]"); 
		        printer.println("   $" + DynamicRerouteNS2_PREFIX + "($i) add-ll $ll($i)"); 
		        printer.println("   $mobilityInterfaceClient start-on-move $node_($i) $" + DynamicRerouteNS2_PREFIX + "($i)"); 
		        printer.println("   $mobilityInterfaceClient stop-on-halt $node_($i) $" + DynamicRerouteNS2_PREFIX + "($i)"); 
		        printer.println(); 
		        printer.println("#  ================================== Agent Parameters =================================="); 
		        printer.println("   $" + DynamicRerouteNS2_PREFIX + "($i) set jitterFactor_    $opt(" + DynamicReroutejitterFactorVar           + ")"); 
		        printer.println("   $" + DynamicRerouteNS2_PREFIX + "($i) set relativeDistanceToDecisionPoint_  $opt(" + DynamicReroutedecisionPercentageVar + ")"); 
		        printer.println("   $" + DynamicRerouteNS2_PREFIX + "($i) set rwm_interval_    $opt(" + DynamicReroutebroadcastIntervalVar      + ")"); 
		         
		        printer.println("}"); 
		        printer.println(); 
			} 
			 
			if ( vehicleStopEventFlag == true || LocationStopEventFlag == true ) { 
		        printer.println("Agent/VanetEventHandler instproc setColor {timestep color msg} {"); 
		        printer.println("   global ns_"); 
		        printer.println("   $self instvar node_"); 
		        printer.println("   $ns_ at $timestep \"$node_ color $color\""); 
		        printer.println("   $ns_ at $timestep \"$ns_ trace-annotate \\\"Node [$node_ id] $msg\\\"\""); 
		        printer.println("}"); 
		        printer.println(); 
			} 
			 
			if ( AccidentEventFlag == true ) { 
				printer.println("Agent/VanetRoadWarning instproc setColor {timestep color msg} {"); 
		        printer.println("   global ns_"); 
		        printer.println("   $self instvar node_"); 
		        printer.println("   $ns_ at $timestep \"$node_ color $color\""); 
		        printer.println("   $ns_ at $timestep \"$ns_ trace-annotate \\\"Node [$node_ id] $msg\\\"\""); 
		        printer.println("}"); 
		        printer.println();				 
			} 
			 
	        //initial flag 
			for (int ind = 0; ind < numberedID.size(); ind++) { 
				NodeCheckFlag[ind] = 0; 
				NodeCountFlag[ind] = -1;				 
			}				 
						 
			int	listLength = initialNode.size(); 
			 
			printer.print("# ======================================================================\n"); 
			printer.print("# predefine node in NAM \n"); 
			printer.print("# ======================================================================\n");			 
			 
			int Forint =0; 
			double LocX = 0; 
			double LocY = 0; 
			 
			for(int index=0; index<listLength; index++){ 
				NSNode temNode = (NSNode) initialNode.get(index); 
				temp = ""; 
				for (int ind = 0; ind < numberedID.size(); ind++){ 
					if (temNode.getID().equalsIgnoreCase((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))){ 
						temp = temp + ((Vector)(numberedID.elementAt(ind))).elementAt(0); 
						break; 
					} 
				} 
				Forint = (int)temNode.getX(); 
				LocX = Forint + (index * 0.000001);  
				 
				Forint = (int)temNode.getY(); 
				LocY = Forint + (index * 0.000001); 
				 
				if (LocX == 0.0){ 
					LocX = 0.005; 
				} 
				if (LocY == 0.0){ 
					LocY = 0.005; 
				} 
				 
			    //System.out.println(Integer.parseInt(topoWidth)); 
				printer.print("$node_("+temp+") set X_ "+LocX+"\n"); 
				printer.print("$node_("+temp+") set Y_ "+LocY+"\n"); 
				printer.print("$node_("+temp+") set Z_ 0.0\n"); 
								 
				printer.print("$node_("+temp+") setdest "+LocX+" "+LocY+" 1"); 
				printer.print("\n"); 
			} 
			 
			/*Only the movements are required*/ 
			if (onlyMovement == 1){ 
				listLength = nodeMovement.size(); 
				//System.out.println(listLength);	 
				if ( listLength > 0) 
					printer.print("# Now produce node movements\n"); 
				for(int index=0; index<listLength; index++){ 
					NSNode temNode = (NSNode) nodeMovement.get(index); 
					temp = ""; 
					for (int ind = 0; ind < numberedID.size(); ind++){ 
						if (temNode.getID().equalsIgnoreCase((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))){ 
							temp = temp + ((Vector)(numberedID.elementAt(ind))).elementAt(0); 
							break; 
						} 
					} 
					//System.out.println(temNode.getTimestep()); 
					//System.out.println(temNode.getID()); 
					//System.out.println(temNode.getX()); 
					//System.out.println(temNode.getY()); 
					 
					Forint = (int)temNode.getX(); 
					LocX = Forint + (index * 0.000001);  
					 
					Forint = (int)temNode.getY(); 
					LocY = Forint + (index * 0.000001); 
					 
					if (LocX == 0.0){ 
						LocX = 0.005; 
					} 
					else if (LocX >= Integer.parseInt(topoLength)){ 
						LocX = Integer.parseInt(topoLength) - 1; 
					} 
					if (LocY == 0.0){ 
						LocY = 0.005; 
					} 
					else if (LocY >= Integer.parseInt(topoWidth)){ 
						LocY = Integer.parseInt(topoWidth) - 1; 
					} 
			 
					localNodeTime = Integer.parseInt(temNode.getTimestep()); 
					localNodeTime -= 1;		//for synchronous 
					 
					NodeCountFlag[Integer.parseInt(temp)] = 0; 
				 
					printer.print("$ns_ at "+ localNodeTime +" \"$node_("+temp+") setdest "+ 
							LocX+" "+LocY+" "+ temNode.getSpeed()+"\"\n");				 
				} 
				printer.close(); 
				return; 
			} 
			/*if(nodeGroups.isEmpty() == false){ 
				for(int index=0; index<nodeGroups.size(); index=index+2){ 
					String groupID = (String) nodeGroups.get(index); 
					String nodeID = (String) nodeGroups.get(index+1); 
 
					if(SRMGroup.containsKey(groupID)==false){ 
						multiCastGroup m = new multiCastGroup(groupID); 
						m.addMember(nodeID); 
						SRMGroup.put(groupID,m); 
						SRMGroupID.add(groupID); 
					} 
					else{ 
						multiCastGroup m = (multiCastGroup)SRMGroup.get(groupID); 
						m.addMember(nodeID); 
						SRMGroup.remove(groupID); 
						SRMGroup.put(groupID,m); 
					} 
				} 
 
 
			}  */ 
			 
			//System.out.println(connections.size()); 
			if (connections.size() > 0 && connectionEventFlag == true){ 
				printer.print("\n# Setup traffic flow between nodes\n"); 
				Vector vtemp; 
				int agentNO = 0; 
				int applicationNO = 0; 
				int firstSCTP = 0; 
 
				if(TCPFlag == 1){ 
					printer.print("Agent/TCP set window_ "+tcpWindow_+"\n"); 
					printer.print("Agent/TCP set packetSize_ "+tcpPacketSize_+"\n"); 
					printer.print("Agent/TCP set maxburst_ "+tcpMaxburst_+"\n"); 
					printer.print("Agent/TCP set maxcwnd_ "+tcpMaxcwnd_+"\n"); 
				}				 
				if(UDPFlag == 1){ 
					printer.print("Agent/UDP set packetSize_ "+udpPacketSize_+"\n"); 
					printer.print("Application/Traffic/CBR set rate_ "+udpRate_+"\n"); 
					printer.print("Application/Traffic/CBR set interval_ "+udpInterval_+"\n"); 
					printer.print("Application/Traffic/CBR set random_ "+udpRandom_+"\n"); 
					printer.print("Application/Traffic/CBR set maxpkts_ "+udpMaxpkts_+"\n"); 
				} 
 
				/* 
				if(SRMFlag == 1){ 
					printer.print("$ns_ at "+srmStop_+" \"$ns_ clear-mcast\"\n"); 
 
					printer.print("Agent/SRM set window_ "+srmWindow_+"\n"); 
					printer.print("Agent/SRM set packetSize_ "+srmPacketSize_+"\n"); 
					printer.print("Agent/SRM set burst_time_ "+srmBurst_time_+"\n"); 
					printer.print("Agent/SRM set idle_time_ "+srmIdle_time_+"\n"); 
					printer.print("Agent/SRM set rate_ "+srmRate_+"\n"); 
				}    
				*/ 
 
				for (int i = 0 ; i < connections.size(); i++){ 
					//System.out.println(connSTime[i]); 
					 
					vtemp = (Vector)connections.elementAt(i);					 
					String temp2 = (String)vtemp.elementAt(0); 
					String temp3 = (String)vtemp.elementAt(2); 
 
					source = ""; 
					for (int ind = 0; ind < numberedID.size(); ind++){ 
						if (temp2.equalsIgnoreCase((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))){ 
							source = source + ((Vector)(numberedID.elementAt(ind))).elementAt(0); 
							break; 
						} 
					} 
 
					sink = ""; 
					for (int ind = 0; ind < numberedID.size(); ind++){ 
						if (temp3.equalsIgnoreCase((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))){ 
							sink = sink + ((Vector)(numberedID.elementAt(ind))).elementAt(0); 
							break; 
						} 
					} 
 
					String agentName = (String)vtemp.elementAt(4); 
					if(agentName.compareTo("tcp")==0){ 
 
						printer.print("\nset " + "tcp" + agentNO + " [new Agent/TCP]\n"); 
						printer.print("$tcp" + agentNO + " set class_ 2\n"); 
						printer.print("set sink" + agentNO+ " [new Agent/TCPSink]\n"); 
						printer.print("$ns_ attach-agent $node_(" + source + ") $tcp" + agentNO + "\n"); 
						printer.print("$ns_ attach-agent $node_(" + sink + ") $sink" + agentNO + "\n"); 
						printer.print("$ns_ connect $tcp"+ agentNO + " $sink" + agentNO + "\n"); 
						printer.print("set ftp" + applicationNO + " [new Application/FTP]\n"); 
						printer.print("$ftp" + applicationNO + " attach-agent $tcp" + agentNO + "\n"); 
						if(TCPFlag == 1){ 
							printer.print("$ns_ at "+ connSTime[i] + " \"$ftp" + applicationNO + " start\"\n\n"); 
							printer.print("$ns_ at "+ connETime[i] + " \"$ftp" + applicationNO + " stop\"\n\n"); 
						}else 
						printer.print("$ns_ at "+tcpStart_+" \"$ftp" + applicationNO + " start\"\n\n"); 
 
 
					} 
					else if(agentName.compareTo("udp")==0){ 
						printer.print("\nset udp" + agentNO + " [new Agent/UDP]\n"); 
						printer.print("$ns_ attach-agent $node_(" + source + ") $udp" + agentNO +"\n"); 
						printer.print("set cbr" + applicationNO + " [new Application/Traffic/CBR]\n"); 
						printer.print("$cbr" + applicationNO + " attach-agent $udp" + agentNO + "\n"); 
						printer.print("set null" + agentNO + " [new Agent/Null]\n"); 
						printer.print("$ns_ attach-agent $node_(" + sink +") $null" + agentNO + "\n"); 
						printer.print("$ns_ connect $udp" + agentNO + " $null" + agentNO + "\n"); 
						if(UDPFlag == 1){							 
							if (Integer.parseInt(udpStart_) < connSTime[i] ) { 
								printer.print("$ns_ at "+ connSTime[i] + " \"$cbr" + applicationNO + " start\"\n\n"); 
							} 
							else 
								printer.print("$ns_ at "+ udpStart_ + " \"$cbr" + applicationNO + " start\"\n\n"); 
							printer.print("$ns_ at "+ connETime[i] + " \"$cbr" + applicationNO + " stop\"\n\n"); 
						}else 
						printer.print("$ns_ at " + udpStart_ +" \"$cbr" + applicationNO + " start\"\n\n"); 
 
					} 
					agentNO++; 
					applicationNO++; 
				} 
				 
			       /* if(nodeGroups.isEmpty() == false){ 
					int SRMNumber=0; 
					applicationNO = 0; 
					//printer.print("$ns_ enableMcast\n"); 
					//printer.print("$ns_ multicast\n"); 
 
					for(int s = 0; s < SRMGroupID.size();s++){ 
						String groupID = (String) SRMGroupID.get(s); 
						printer.print("set "+groupID+" [$ns_ allocaddr]\n"); 
						printer.print("set srm"+SRMNumber+" [new Agent/SRM]\n"); 
						printer.print("$srm"+SRMNumber+" set dst_ $"+groupID+"\n"); 
 
 
						multiCastGroup gEle = (multiCastGroup)SRMGroup.get(groupID); 
						Vector mlist = gEle.getMember(); 
						for(int k=0;k<mlist.size();k++){ 
						 
							String nodeID = (String) mlist.get(k); 
 
						String temp = ""; 
						for (int ind = 0; ind < numberedID.size(); ind++){ 
							if (nodeID.equals((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))){ 
								temp = temp + ((Vector)(numberedID.elementAt(ind))).elementAt(0); 
								break; 
							} 
						} 
 
							printer.print("$ns_ attach-agent $node_("+temp+") $srm"+SRMNumber+"\n"); 
							//printer.print("$node_("+temp+") join-group $srm"+SRMNumber+" $"+groupID+"\n"); 
						} 
						printer.print("set exp"+applicationNO+" [new Application/Traffic/Exponential]\n"); 
						printer.print("$exp"+applicationNO+" attach-agent $srm"+SRMNumber+"\n"); 
						printer.print("$srm"+SRMNumber+" log [open srmStats.tr w]\n"); 
						printer.print("$srm"+SRMNumber+" trace [open srmEvents.tr w]\n"); 
 
						if(SRMFlag == 1){ 
							printer.print("$ns_ at "+ srmStart_ +" \"$srm"+SRMNumber+"start; $exp"+applicationNO+" start\"\n"); 
 
						}else 
						printer.print("$ns_ at "+ srmStart_ +" \"$srm"+SRMNumber+"start; $exp"+applicationNO+" start\"\n"); 
 
 
 
						SRMNumber++; 
						applicationNO++; 
					} 
				} */ 
 
			} 
 
			if ( vehicleStopEventFlag == true) {				 
				printer.println("# ======================================================================"); 
				printer.println("#       Vehicle Based Event"); 
				printer.println("# ======================================================================"); 
		        for (VehicleBasedEvent event : vehicleEvents) {		 
		        	String temp2 = event.getNodeID(); 
		        	for (int ind = 0; ind < numberedID.size(); ind++){ 
		        		if (temp2.equalsIgnoreCase((String)((Vector)(numberedID.elementAt(ind))).elementAt(1))) { 
				        	printer.println("$ns_ at " + event.getStartBrcTime() + " \"$" + NS2_PREFIX + "(" + ind + ") disable-node\""); 
				        	printer.println("$ns_ at " + event.getStopBrcTime()  + " \"$" + NS2_PREFIX + "(" + ind + ") enable-node\""); 
				        	break; 
		        		}				       			        		 
		        	}		        	 
		        } 
			} 
			 
			if ( LocationStopEventFlag == true) { 
				printer.println("# ======================================================================"); 
				printer.println("#       Location Based Event"); 
				printer.println("# ======================================================================"); 
				printer.println("for {set i 0} {$i < $val(nn)} {incr i} {"); 
				printer.println("   $ns_ at " + locationEvent.getDisableRoadTime() 
		                                   + " \"$" + NS2_PREFIX + "($i) disable-road " 
		                                   + locationEvent.getXFrom() + " " 
		                                   + locationEvent.getYFrom() + " " 
		                                   + locationEvent.getXTo()   + " " 
		                                   + locationEvent.getYTo()   + " " 
		                                   + "\""); 
		         
				printer.println("   $ns_ at " + locationEvent.getEnableRoadTime() + " \"$" + NS2_PREFIX + "($i) enable-road\""); 
				printer.println("}");  
			} 
 
			if ( DynamicRerouteEventFlag == true) { 
				printer.println("# ======================================================================"); 
				printer.println("# Traffic Control Broadcast from the start of simulation"); 
				printer.println("# ======================================================================"); 
				printer.println("for {set i 0} {$i < $val(nn)} {incr i} {");                 
				printer.println("   $ns_ at " + getStartBroadcastTime() + " \"$" + DynamicRerouteNS2_PREFIX + "($i) start-getRoadPosition\""); 
		        printer.println("   $ns_ at " + getStartBroadcastTime() + " \"$" + DynamicRerouteNS2_PREFIX + "($i) start-reroutingVerification\""); 
		        printer.println("   $ns_ at " + getStartBroadcastTime() + " \"$" + DynamicRerouteNS2_PREFIX + "($i) start-dr\""); 
		        printer.println("}"); 
 
		        printer.println("# at the end of the simulation nodes should stop"); 
		        printer.println("for {set i 0} {$i < $val(nn)} {incr i} {"); 
		        printer.println("   $ns_ at [expr "+ stopTime +" 0.000000001] \"$" + DynamicRerouteNS2_PREFIX + "($i) stop-getRoadPosition\""); 
		        printer.println("   $ns_ at [expr "+ stopTime +" + 0.000000001] \"$" + DynamicRerouteNS2_PREFIX + "($i) stop-reroutingVerification\""); 
		        printer.println("   $ns_ at " + stopTime +" \"$" + DynamicRerouteNS2_PREFIX + "($i) stop-dr\""); 
		        printer.println("}"); 
			} 
			 
			printer.print("# ======================================================================\n"); 
			printer.print("# Tell nodes when the simulation ends\n"); 
			printer.print("# ======================================================================\n"); 
			printer.print("for {set i 0} {$i < $val(nn) } {incr i} {\n"); 
			printer.print("    $ns_ at "+ stopTime + " \"$node_($i) reset\";\n"); 
			printer.print("}\n"); 
			printer.print("$ns_ at " + stopTime + " \"stop\"\n"); 
			printer.print("$ns_ at " + stopTime + "1 \"puts \\\"NS EXITING...\\\" ; $ns_ halt\"\n"); 
			printer.print("proc stop {} {\n"); 
			printer.print("    global ns_ tracefd vanettracefd\n"); 
			printer.print("    $ns_ flush-trace"); 
			if (namflag == true) 
				printer.print(" namtrace\n"); 
			else 
				printer.print("\n"); 
			//printer.print("    $ns_ flush-trace\n"); 
			printer.print("    close $tracefd\n"); 
			if (namflag == true) 
			printer.print("    close $namtrace\n"); 
			printer.print("    close $vanettracefd\n"); 
             
			printer.print("}\n\n"); 
			printer.print("puts \"Starting Simulation...\"\n"); 
			printer.print("$ns_ run\n\n"); 
			printer.close(); 
			return; 
		} 
	} 
}