[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip ProtocolStack.java,1.9,1.10 Udp.java,1.9,1.10
Status: Beta
Brought to you by:
darkkey
From: gift <gi...@us...> - 2005-11-20 13:44:06
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28544/core/protocolsuite/tcp_ip Modified Files: ProtocolStack.java Udp.java Log Message: Now it's working pretty good! Index: ProtocolStack.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ProtocolStack.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** ProtocolStack.java 20 Nov 2005 10:25:26 -0000 1.9 --- ProtocolStack.java 20 Nov 2005 13:43:57 -0000 1.10 *************** *** 748,752 **** UDP_packet temp = (UDP_packet)inPacket; ! System.out.println("UDP packet here trying to pass up!!!!"); try{ mUDPprotocol.receiveUDPPacket(temp); --- 748,752 ---- UDP_packet temp = (UDP_packet)inPacket; ! System.out.println("UDP packet here Receiving!!!! Message from ProtocolStack."); try{ mUDPprotocol.receiveUDPPacket(temp); *************** *** 1298,1331 **** * @exception CommunicationException * @exception LowLinkException ! * @exception InvalidNetworkLayerDeviceException ! * @exception TransportLayerException ! * @version v0.30 * @see CommunicationException * @see LowLinkException - * @see InvalidNetworkLayerDeviceException - * @see TransportLayerException */ public void sendUDP(Object application, String inUDPMessage) throws LowLinkException, CommunicationException ! { ! ! //lets get first network interface name ! //will be used when resolving source IP of UDP sender String FirstInterfaceName; String DestIPAddress; int destPort; Node temp = (Node)mParentNode; FirstInterfaceName = temp.getFirstInterfaceName(); ! try{ ! System.out.println("Trying to send!!!!!!!"); ! DestIPAddress=mUDPprotocol.getApplicationDestIP(application); ! destPort=mUDPprotocol.getApplicationDestPortNumber(application); if (FirstInterfaceName!=null) { if(IPV4Address.validateDecIP(DestIPAddress)) ! { ! UDP_packet tosend = mUDPprotocol.sendUDPPacket(application, DestIPAddress,FirstInterfaceName,destPort,inUDPMessage); sendPacket(tosend); }else --- 1298,1334 ---- * @exception CommunicationException * @exception LowLinkException ! * @version v0.40 * @see CommunicationException * @see LowLinkException */ public void sendUDP(Object application, String inUDPMessage) throws LowLinkException, CommunicationException ! { String FirstInterfaceName; String DestIPAddress; + String SourceIPAddress; + int srcPort; int destPort; + + //lets get first network interface name Node temp = (Node)mParentNode; FirstInterfaceName = temp.getFirstInterfaceName(); ! try{ ! //let's get a reserved local port number for this application ! srcPort=mUDPprotocol.getApplicationLocalPortNumber(application); ! //and destination IP of the other host ! DestIPAddress=mUDPprotocol.getApplicationDestIP(srcPort); ! //and destination port of the other host ! destPort=mUDPprotocol.getApplicationDestPortNumber(srcPort); if (FirstInterfaceName!=null) { if(IPV4Address.validateDecIP(DestIPAddress)) ! { ! //let's get first interface IP address ! SourceIPAddress=getIPAddress(FirstInterfaceName); ! ! UDP_packet tosend = mUDPprotocol.sendUDPPacket(DestIPAddress,SourceIPAddress,destPort,srcPort,inUDPMessage); sendPacket(tosend); }else Index: Udp.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Udp.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Udp.java 20 Nov 2005 10:25:26 -0000 1.9 --- Udp.java 20 Nov 2005 13:43:57 -0000 1.10 *************** *** 149,153 **** --- 149,158 ---- public Udp(ProtocolStack inParentStack) { + //String i; //String is port number :( such a pity but we use hash tables + //int j; //port number int i; + + for(i=0;i<PORT_QUANT;i++) + { UDP_HashTableElement Elm = new UDP_HashTableElement(); *************** *** 158,163 **** mParentStack = inParentStack; ! for(i=0;i<PORT_QUANT;i++) ! { PortTable.put(new Integer((PORT_START_NUMBER+i)),Elm); // CHECK HERE } --- 163,167 ---- mParentStack = inParentStack; ! PortTable.put(new Integer((PORT_START_NUMBER+i)),Elm); // CHECK HERE } *************** *** 194,198 **** //let's check all the things dealing with destination port number ! UDP_HashTableElement Elm = new UDP_HashTableElement(); Elm=(UDP_HashTableElement)PortTable.get(inPacket.get_destPort()); --- 198,202 ---- //let's check all the things dealing with destination port number ! UDP_HashTableElement Elm = null; Elm=(UDP_HashTableElement)PortTable.get(inPacket.get_destPort()); *************** *** 240,244 **** case 2: //busy port, let's check whether it's busy by us (sender) ;) ! if ((Elm.connectedtoIP.equals(inPacket.getSourceIPAddress())) && (Elm.connectedtoPort==inPacket.get_srcPort())) //temporary { //now we decompose UDP datagram --- 244,248 ---- case 2: //busy port, let's check whether it's busy by us (sender) ;) ! if ((Elm.connectedtoIP.equals(inPacket.getSourceIPAddress())) && (Elm.connectedtoPort==inPacket.get_srcPort())) { //now we decompose UDP datagram *************** *** 287,295 **** * @author gift (sourceforge.net user) * @param inDestIPAddress destination IP address ! * @param FirstInterfaceName name of the first network intarface of the node that sends UDP e.g. "eth0" * @param indestPort destination port number ! * @param application application that uses port * @param inMessage UDP message to send ! * @return UDP_packet * @exception TransportLayerException * @version v0.30 --- 291,299 ---- * @author gift (sourceforge.net user) * @param inDestIPAddress destination IP address ! * @param inSourceIPAddress our IP address * @param indestPort destination port number ! * @param insrcPort our port number * @param inMessage UDP message to send ! * @return UDP_packet to send in Network Layer * @exception TransportLayerException * @version v0.30 *************** *** 297,328 **** */ ! public UDP_packet sendUDPPacket(Object application,String inDestIPAddress,String inFirstInterfaceName, int indestPort, String inMessage) throws TransportLayerException, LowLinkException { ! int srcPort; ! String SrcIP; ! ! //let's get first interface IP address ! SrcIP=mParentStack.getIPAddress(inFirstInterfaceName); ! ! //let's get a reserved local port number for this application ! srcPort=getApplicationLocalPortNumber(application); ! ! ! UDP_packet tosend = new UDP_packet(inDestIPAddress,SrcIP,indestPort,srcPort); tosend.setUDP_message(inMessage); //Create Layer info - LayerInfo UDP_Info = new LayerInfo(getClass().getName()); - UDP_Info.setObjectName(mParentStack.getParentNodeName()); - UDP_Info.setDataType("UDP Packet"); - UDP_Info.setLayer("Transport"); ! ! UDP_Info.setDescription("Created UDP packet to " + inDestIPAddress + ":" + srcPort +"."); ! Simulation.addLayerInfo(UDP_Info); --- 301,317 ---- */ ! public UDP_packet sendUDPPacket(String inDestIPAddress,String inSourceIPAddress, int indestPort, int insrcPort, String inMessage) throws TransportLayerException, LowLinkException { ! ! UDP_packet tosend = new UDP_packet(inDestIPAddress,inSourceIPAddress,indestPort,insrcPort); tosend.setUDP_message(inMessage); //Create Layer info LayerInfo UDP_Info = new LayerInfo(getClass().getName()); UDP_Info.setObjectName(mParentStack.getParentNodeName()); UDP_Info.setDataType("UDP Packet"); UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Created UDP packet for " + inDestIPAddress + ":" + insrcPort +"."); Simulation.addLayerInfo(UDP_Info); *************** *** 344,348 **** * @see TransportLayerException */ ! public int getDefinitePortNumber(Object application,String inDestIPAddress, int indestPort) throws TransportLayerException { Integer number = new Integer(0); --- 333,337 ---- * @see TransportLayerException */ ! public int ReservePort(Object application,String inDestIPAddress, int indestPort) throws TransportLayerException { Integer number = new Integer(0); *************** *** 394,401 **** { //lets try to reserve any free port ! number=0; ! while ( (number<PORT_QUANT) && !(Reserved) ) { ! Elm = (UDP_HashTableElement) PortTable.get(number+PORT_START_NUMBER); if (Elm.PortStatus == 0) //free port --- 383,390 ---- { //lets try to reserve any free port ! number=PORT_START_NUMBER; ! while ( (number<PORT_START_NUMBER+PORT_QUANT) && !(Reserved) ) { ! Elm = (UDP_HashTableElement) PortTable.get((Integer)number); if (Elm.PortStatus == 0) //free port *************** *** 407,411 **** Elm.application = application; } ! number++; } --- 396,400 ---- Elm.application = application; } ! if (!Reserved) number++; } *************** *** 414,483 **** throw new TransportLayerException("UDP Error: all ports are busy! Can not send UDP datagram to host " + inDestIPAddress + ":" + indestPort +"."); } ! number+=PORT_START_NUMBER; //to get the real number ! } return (number); //do not change here! if you want to change here scan again the code } ! ! /** ! * This method reserves LOCAL UDP port ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @param inDestIPAddress destination IP address ! * @param indestPort destination port number ! * @return int port number that has been reserved for application ! * @exception TransportLayerException ! * @version v0.30 ! * @see TransportLayerException ! */ ! public int ReservePort(Object application, String inDestIPAddress, int indestPort) throws TransportLayerException ! { ! //let's get a proper port number for this application ! return getDefinitePortNumber(application,inDestIPAddress,indestPort); ! } ! /** * This method finds port number for an application. Port number range: [PORT_START_NUMBER; PORT_START_NUMBER+PORT_QUANT] * @author gift (sourceforge.net user) ! * @param application ! * @return int port number that has been reserved for application * @exception TransportLayerException ! * @version v0.10 * @see TransportLayerException */ ! public int getApplicationDestPortNumber(Object application) throws TransportLayerException { int number=0; ! boolean Found=false; ! Set set = PortTable.keySet(); ! Iterator itr = set.iterator(); ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! ! while ( (itr.hasNext()) && !(Found) ) ! { ! Elm = (UDP_HashTableElement) itr.next(); ! ! int UID1 = -2; ! int UID2 = -1; ! ! if(Elm.application!=null && application!=null){ ! UID1 = ((Application)Elm.application).getUID(); ! UID2 = ((Application)application).getUID(); ! } ! ! if ( UID1 == UID2 ) ! { ! Found=true; ! //we have found our application ! number=Elm.connectedtoPort; ! } ! } ! //in case we have not found our application in hash table..... ! if (!Found) ! { ! throw new TransportLayerException("UDP Error: no local port reserved for the application: \"" + application.toString() + "\"! Can not find destination port."); ! } ! return new Integer(number); } --- 403,436 ---- throw new TransportLayerException("UDP Error: all ports are busy! Can not send UDP datagram to host " + inDestIPAddress + ":" + indestPort +"."); } ! ! } ! ! //For debugging ! UDP_HashTableElement Elm2 = new UDP_HashTableElement();//For debugging ! Elm2 = (UDP_HashTableElement) PortTable.get((Integer)number+1);//For debugging return (number); //do not change here! if you want to change here scan again the code + + } ! /** * This method finds port number for an application. Port number range: [PORT_START_NUMBER; PORT_START_NUMBER+PORT_QUANT] * @author gift (sourceforge.net user) ! * @param inSrcPort port number that has been reserved for the applicaion ! * @return int port number of the other host (that the application is connected to) * @exception TransportLayerException ! * @version v0.20 * @see TransportLayerException */ ! public int getApplicationDestPortNumber(int inSrcPort) throws TransportLayerException { int number=0; ! UDP_HashTableElement Elm = null; ! Elm=(UDP_HashTableElement)PortTable.get((Integer)inSrcPort); ! number=Elm.connectedtoPort; ! ! return number; } *************** *** 493,509 **** public void ClosePort(Object application) throws TransportLayerException //changes port status from 1 (listen) to 0 (free) { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); Integer PortToClose=getApplicationLocalPortNumber(application); ! if (Elm.PortStatus==1) { ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! //PortTable.remove(PortToClose); //delete old element ! //PortTable.put(PortToClose,Elm); //hash table update ! } else throw new TransportLayerException("UDP Error: port "+ PortToClose +" is not being LISTENED to by the application: \"" + application.toString() + "\"."); } --- 446,465 ---- public void ClosePort(Object application) throws TransportLayerException //changes port status from 1 (listen) to 0 (free) { ! UDP_HashTableElement oldElm=null; Integer PortToClose=getApplicationLocalPortNumber(application); ! oldElm=(UDP_HashTableElement) PortTable.get((Integer)PortToClose); ! ! if (oldElm.PortStatus==1) { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! PortTable.remove(PortToClose); //delete old element ! PortTable.put(PortToClose,Elm); //hash table update ! } else throw new TransportLayerException("UDP Error: port "+ PortToClose +" is not being LISTENED to by the application: \"" + application.toString() + "\"."); } *************** *** 519,534 **** public void FreePort(Object application) throws TransportLayerException //changes port status from 3 (listen) to 0 (free) { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); Integer PortToFree=getApplicationLocalPortNumber(application); ! if (Elm.PortStatus==2) ! { ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! //PortTable.remove(PortToFree); //delete old element ! //PortTable.put(PortToFree,Elm); //hash table update } else throw new TransportLayerException("UDP Error: port "+ PortToFree +" is not BUSY by the application: \"" + application.toString() + "\"."); } --- 475,493 ---- public void FreePort(Object application) throws TransportLayerException //changes port status from 3 (listen) to 0 (free) { ! UDP_HashTableElement oldElm=null; Integer PortToFree=getApplicationLocalPortNumber(application); ! oldElm=(UDP_HashTableElement) PortTable.get((Integer)PortToFree); ! ! if (oldElm.PortStatus==2) ! { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! PortTable.remove(PortToFree); //delete old element ! PortTable.put(PortToFree,Elm); //hash table update } else throw new TransportLayerException("UDP Error: port "+ PortToFree +" is not BUSY by the application: \"" + application.toString() + "\"."); } *************** *** 554,567 **** if (inPort>=0 && inPort<=10000) { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); if ( (UDP_HashTableElement)PortTable.get(inPort) !=null ) { ! Elm = (UDP_HashTableElement)PortTable.get(inPort); ! switch(Elm.PortStatus) { case 0: //port is free => update such a record in hashtable ! Elm.PortStatus=1; Elm.application=application; --- 513,527 ---- if (inPort>=0 && inPort<=10000) { ! UDP_HashTableElement oldElm = null; if ( (UDP_HashTableElement)PortTable.get(inPort) !=null ) { ! oldElm = (UDP_HashTableElement)PortTable.get(inPort); ! switch(oldElm.PortStatus) { case 0: //port is free => update such a record in hashtable ! ! UDP_HashTableElement Elm = new UDP_HashTableElement(); Elm.PortStatus=1; Elm.application=application; *************** *** 569,577 **** Elm.connectedtoPort=0; ! //PortTable.remove(inPort); //delete old element ! //PortTable.put(inPort,Elm); //hash table update break; case 1: // port is being listened to by whom? ! if (Elm.application==application) { throw new TransportLayerException("UDP Error: already listening to port "+ inPort +"!"); --- 529,537 ---- Elm.connectedtoPort=0; ! PortTable.remove(inPort); //delete old element ! PortTable.put(inPort,Elm); //hash table update break; case 1: // port is being listened to by whom? ! if (oldElm.application==application) //no UID required here :) check before update! { throw new TransportLayerException("UDP Error: already listening to port "+ inPort +"!"); *************** *** 587,590 **** --- 547,551 ---- { //create such a record in hashtable + UDP_HashTableElement Elm = new UDP_HashTableElement(); Elm.PortStatus=1; Elm.application=application; *************** *** 601,653 **** * This method finds destination IP (host) for an application. * @author gift (sourceforge.net user) ! * @param application ! * @return String destination IP that has been reserved for application * @exception TransportLayerException ! * @version v0.10 * @see TransportLayerException */ ! public String getApplicationDestIP(Object application) throws TransportLayerException { String dIP=null; - boolean Found=false; - Set set = PortTable.keySet(); - Iterator itr = set.iterator(); - //Enumeration keys = PortTable.elements(); - //Integer key = null; UDP_HashTableElement Elm = null; - while ( (itr.hasNext()) && !(Found) ) //keys.hasMoreElements() - { - try{ - //key = new Integer((Integer) keys.nextElement()); - //Elm = (UDP_HashTableElement) PortTable.get(key); - System.out.println("Getting next itr!!!!!!!"); - Elm=(UDP_HashTableElement)itr.next(); - System.out.println("GOT ITR!!!!!!!"); - }catch(Exception ex){ System.out.println(ex.toString()); } - - int UID1 = -2; - int UID2 = -1; - - if(Elm!=null && application!=null){ - if(Elm.application!=null) UID1 = ((Application)Elm.application).getUID(); - UID2 = ((Application)application).getUID(); - } - - if ( UID1 == UID2 ) - { - Found=true; - //we have found our application - dIP=Elm.connectedtoIP; - } - - - } - - //in case we have not found our application in hash table..... - if (!Found) - { - throw new TransportLayerException("UDP Error: no local port reserved for the application: \"" + application.toString() + "\"! Can not find destination IP."); - } return dIP; } --- 562,579 ---- * This method finds destination IP (host) for an application. * @author gift (sourceforge.net user) ! * @param inSrcPort port number that has been reserved for the applicaion ! * @return String destination IP (other host IP that the application is connected to) * @exception TransportLayerException ! * @version v0.20 * @see TransportLayerException */ ! public String getApplicationDestIP(int inSrcPort) throws TransportLayerException { String dIP=null; UDP_HashTableElement Elm = null; + + Elm=(UDP_HashTableElement)PortTable.get((Integer)inSrcPort); + dIP=Elm.connectedtoIP; return dIP; } *************** *** 665,677 **** public int getApplicationLocalPortNumber(Object application) throws TransportLayerException { ! int number=0; boolean Found=false; boolean Reserved=false; Enumeration LocalPorts = PortTable.keys(); ! UDP_HashTableElement Elm = new UDP_HashTableElement(); while ( (LocalPorts.hasMoreElements()) && !(Found) ) { ! number = Integer.valueOf( (String)LocalPorts.nextElement() ); Elm = (UDP_HashTableElement) PortTable.get(number); --- 591,603 ---- public int getApplicationLocalPortNumber(Object application) throws TransportLayerException { ! Integer number= new Integer(0); boolean Found=false; boolean Reserved=false; Enumeration LocalPorts = PortTable.keys(); ! UDP_HashTableElement Elm = null; while ( (LocalPorts.hasMoreElements()) && !(Found) ) { ! number = (Integer)LocalPorts.nextElement(); Elm = (UDP_HashTableElement) PortTable.get(number); |