[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip Application.java, 1.16, 1.17 Echo.java, 1.21,
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2007-09-16 19:38:08
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv31679/core/protocolsuite/tcp_ip Modified Files: Application.java Echo.java ProtocolStack.java SNMP.java Udp.java Log Message: First attempt to create simple socket layer: modfied UDP protocol... Index: ProtocolStack.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ProtocolStack.java,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** ProtocolStack.java 22 Nov 2006 21:39:45 -0000 1.54 --- ProtocolStack.java 16 Sep 2007 19:38:04 -0000 1.55 *************** *** 124,127 **** --- 124,129 ---- private Node mParentNode = null; + private socketLayer mSL = null; + private int packetinputIPCounter; // counter for input IP Packets *************** *** 163,168 **** mTCPprotocol = new Tcp(this); //gift (sourceforge.net user) 25 Nov 2005 ! mUDPprotocol = new Udp(this); //gift (sourceforge.net user) 17 Nov 2005 } --- 165,174 ---- mTCPprotocol = new Tcp(this); //gift (sourceforge.net user) 25 Nov 2005 + + mSL = new socketLayer(this); ! mUDPprotocol = new Udp(this, mSL); //gift (sourceforge.net user) 17 Nov 2005 ! ! } *************** *** 179,184 **** public void initNAT(){ mTCPprotocol = new Tcp(this); //gift (sourceforge.net user) 25 Nov 2005 ! ! mUDPprotocol = new Udp(this); //gift (sourceforge.net user) 17 Nov 2005 } --- 185,190 ---- public void initNAT(){ mTCPprotocol = new Tcp(this); //gift (sourceforge.net user) 25 Nov 2005 ! mSL = new socketLayer(this); ! mUDPprotocol = new Udp(this, mSL); //gift (sourceforge.net user) 17 Nov 2005 } *************** *** 207,210 **** --- 213,218 ---- */ + + public void sendPacket(IP_packet inPacket) throws LowLinkException, CommunicationException{ *************** *** 214,217 **** --- 222,229 ---- String outInterface[] = new String[2]; + + if(!IPV4Address.validateDecIP(inPacket.mDestIPAddress)){ + throw new CommunicationException("Packet dropped host unreachable: " + inPacket.mDestIPAddress); + } try{ *************** *** 1435,1439 **** { ! if(IPV4Address.validateDecIP(inDestIPAddress)) { --- 1447,1451 ---- { ! /*if(IPV4Address.validateDecIP(inDestIPAddress)) { *************** *** 1450,1456 **** --- 1462,1481 ---- }else throw new CommunicationException("Packet dropped host unreachable: " + inDestIPAddress); + */ + return 0; } + public Udp UDP(){ + return mUDPprotocol; + } + + public Tcp TCP(){ + return mTCPprotocol; + } + + public socketLayer SL(){ + return mSL; + } /** *************** *** 1466,1470 **** public void CloseUDP(Object application) throws TransportLayerException { ! try { mUDPprotocol.ClosePort(application); } catch (TransportLayerException te) --- 1491,1495 ---- public void CloseUDP(Object application) throws TransportLayerException { ! /*try { mUDPprotocol.ClosePort(application); } catch (TransportLayerException te) *************** *** 1476,1480 **** UDP_Info.setDescription("UDP port closing: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! } } --- 1501,1505 ---- UDP_Info.setDescription("UDP port closing: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! }*/ } *************** *** 1491,1495 **** public void freeUDPPort(Object application) throws TransportLayerException { ! try { mUDPprotocol.FreePort(application); } catch (TransportLayerException te) --- 1516,1520 ---- public void freeUDPPort(Object application) throws TransportLayerException { ! /*try { mUDPprotocol.FreePort(application); } catch (TransportLayerException te) *************** *** 1501,1505 **** UDP_Info.setDescription("UDP port freeing: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! } } --- 1526,1530 ---- UDP_Info.setDescription("UDP port freeing: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! }*/ } *************** *** 1515,1519 **** public void FreeUDPApplication(Object application) throws TransportLayerException { ! mUDPprotocol.FreeApplication(application); } --- 1540,1544 ---- public void FreeUDPApplication(Object application) throws TransportLayerException { ! //mUDPprotocol.FreeApplication(application); } *************** *** 1530,1534 **** public void ListenUDP(Object application, int inPort) throws TransportLayerException { ! try{ mUDPprotocol.ListenPort(application,inPort); --- 1555,1559 ---- public void ListenUDP(Object application, int inPort) throws TransportLayerException { ! /* try{ mUDPprotocol.ListenPort(application,inPort); *************** *** 1548,1554 **** UDP_Info.setDescription("Error: UDP port listening: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! } } ! /** * This method sends the UDP packet --- 1573,1593 ---- UDP_Info.setDescription("Error: UDP port listening: \""+ te.toString() + "\"."); Simulation.addLayerInfo(UDP_Info); ! }*/ } ! ! public String getSrcIP(){ ! Node temp = (Node)mParentNode; ! String FirstInterfaceName = temp.getFirstInterfaceName(); ! ! if (FirstInterfaceName!=null) ! { ! return getIPAddress(FirstInterfaceName); ! }else{ ! return ""; ! } ! } ! ! ! /** * This method sends the UDP packet *************** *** 1566,1570 **** public void sendUDP(Object application, String inUDPMessage) throws LowLinkException, CommunicationException { ! String FirstInterfaceName; String DestIPAddress; String SourceIPAddress; --- 1605,1609 ---- public void sendUDP(Object application, String inUDPMessage) throws LowLinkException, CommunicationException { ! /*String FirstInterfaceName; String DestIPAddress; String SourceIPAddress; *************** *** 1620,1624 **** } ! } } --- 1659,1663 ---- } ! }*/ } Index: Application.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Application.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** Application.java 24 Feb 2006 10:20:11 -0000 1.16 --- Application.java 16 Sep 2007 19:38:04 -0000 1.17 *************** *** 15,18 **** --- 15,22 ---- import core.LowLinkException; + import core.LayerInfo; + + import core.Simulation; + /** * *************** *** 129,131 **** --- 133,144 ---- } + protected void printLayerInfo(String DataType, String s) { + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType(DataType); + protInfo.setLayer("Application "); + protInfo.setDescription(s); + Simulation.addLayerInfo(protInfo); + } + } Index: Udp.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Udp.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** Udp.java 5 Feb 2006 14:01:51 -0000 1.34 --- Udp.java 16 Sep 2007 19:38:04 -0000 1.35 *************** *** 128,137 **** { public byte PortStatus=0; // 0 - free port; 1 - port is being listened to; 2 - busy. ! public Object application=null; //points to application that listens to this port | provided PortStatus==1 public String connectedtoIP=""; //contains IP of the other connected computer | provided PortStatus==2 public int connectedtoPort=0; //contains port number of the other connected computer | provided PortStatus==2 } ! private Hashtable PortTable = new Hashtable(); private ProtocolStack mParentStack; --- 128,142 ---- { public byte PortStatus=0; // 0 - free port; 1 - port is being listened to; 2 - busy. ! public Object application=null; //points to application that listens to this port | provided PortStatus==1 public String connectedtoIP=""; //contains IP of the other connected computer | provided PortStatus==2 public int connectedtoPort=0; //contains port number of the other connected computer | provided PortStatus==2 } ! public class UDP_session{ ! public byte PortStatus=0; // 0 - free port; 1 - port is being listened to; 2 - busy. ! public int sock=-1; // socket... ! } ! ! private Hashtable PortTable = new Hashtable(); private ProtocolStack mParentStack; *************** *** 145,148 **** --- 150,155 ---- private static final int PORT_INIT = -1; //used in ListenPort && AlreadyListens + private socketLayer mSL; + /** * This method assigns the ParentStack *************** *** 151,168 **** * @version v0.20 */ ! public Udp(ProtocolStack inParentStack) { ! int i; mParentStack = inParentStack; ! for(i=0;i<PORT_QUANT;i++) ! { ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! PortTable.put(new Integer((PORT_START_NUMBER+i)),Elm); // DANGEROUS PLACE! Check before change. ! } } --- 158,166 ---- * @version v0.20 */ ! public Udp(ProtocolStack inParentStack, socketLayer inSL) { ! //int i; mParentStack = inParentStack; ! mSL = inSL; } *************** *** 194,218 **** // test if destination UDP port exists on this NeworkLayerDevice ! if (PortTable.get(inPacket.get_destPort()) !=null) { //let's check all the things dealing with destination port number ! UDP_HashTableElement Elm = null; ! Elm=(UDP_HashTableElement)PortTable.get(inPacket.get_destPort()); switch(Elm.PortStatus) { case 0: //port is free => Error: no application to receive UDP //have to disconnect listener from port ! throw new TransportLayerPortException("UDP Error: no server application listening to port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"!"); //no break needed ;) ! case 1: // port is being listened to by a new application. Have to "establish" (in UDP no connections are being established) a new connection by updating hashtable element information //now we decompose UDP datagram ! ! // everything is OK, one more ! Elm.PortStatus=2; //port will be busy from now ! Elm.connectedtoIP=inPacket.getSourceIPAddress(); ! Elm.connectedtoPort=inPacket.get_srcPort(); ! IncReceivedDatagrammsNumber(); --- 192,214 ---- // test if destination UDP port exists on this NeworkLayerDevice ! if (PortTable.get((Integer)inPacket.get_destPort()) !=null) { //let's check all the things dealing with destination port number ! //UDP_HashTableElement Elm = null; ! UDP_session Elm = null; ! Elm=(UDP_session)PortTable.get(inPacket.get_destPort()); + int sock = Elm.sock; + switch(Elm.PortStatus) { case 0: //port is free => Error: no application to receive UDP //have to disconnect listener from port ! throw new TransportLayerPortException("UDP Error: no application binded port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"!"); //no break needed ;) ! case 1: // port is binded //now we decompose UDP datagram ! IncReceivedDatagrammsNumber(); *************** *** 226,270 **** UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + ":" + inPacket.get_srcPort() + " message: \"" +inPacket.getUDP_message() + "\"." + " UDP Port " + inPacket.get_destPort() + " has status \"busy\" from now."); Simulation.addLayerInfo(UDP_Info); ! Application listener; ! listener = (Application)Elm.application; ! listener.RecvData(inPacket.getUDP_message()); ! break; ! ! case 2: //busy port, let's check whether it's busy by sender ;) ! if ((Elm.connectedtoIP.equals(inPacket.getSourceIPAddress())) && (Elm.connectedtoPort==inPacket.get_srcPort())) ! { ! //now we decompose UDP datagram ! // everything is OK ! //Create Layer info ! ! UDP_Info = new LayerInfo(getClass().getName()); ! ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! ! UDP_Info.setDataType("UDP Protocol"); ! ! UDP_Info.setLayer("Transport"); ! ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + ":" + inPacket.get_srcPort() + " message: \"" +inPacket.getUDP_message() + "\"."); ! ! Simulation.addLayerInfo(UDP_Info); ! ! IncReceivedDatagrammsNumber(); ! ! listener = (Application)Elm.application; ! listener.RecvIP(inPacket.getSourceIPAddress()); ! listener.RecvPrt(inPacket.get_srcPort()); ! listener.RecvData(inPacket.getUDP_message()); ! ! } else { ! //have to disconnect listener from port ! throw new TransportLayerPortException("UDP Error: port is busy! Port is connected to another host or/and port and is busy by another application." + "Port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"."); ! } ! break; ! default: //UDP Error: unknown port status :( //have to disconnect listener from port --- 222,231 ---- UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + ":" + inPacket.get_srcPort() + " message: \"" +inPacket.getUDP_message() + "\"."); // + " UDP Port " + inPacket.get_destPort() + " has status \"busy\" from now."); Simulation.addLayerInfo(UDP_Info); ! mSL.recvFrom(sock, inPacket.getSourceIPAddress(), inPacket.get_srcPort(), inPacket.getUDP_message()); ! break; default: //UDP Error: unknown port status :( //have to disconnect listener from port *************** *** 295,332 **** */ ! 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 Protocol"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Created UDP packet for " + inDestIPAddress + ":" + indestPort +"."); ! Simulation.addLayerInfo(UDP_Info); ! ! IncSentDatagrammsNumber(); ! return tosend; } ! ! /** ! * This method reserves LOCAL port number for an application in case reserved. Port number range: [PORT_START_NUMBER; PORT_START_NUMBER+PORT_QUANT] ! * if not found then we reserve a new number ! * @author gift (sourceforge.net user) ! * @param application that will: take free port or take already occupied port (only by itself) or tell that all ports are busy :( ! * @param inDestIPAddress destination IP address ! * @param indestPort destination port number ! * @return int port number that has been reserved for application ! * @exception TransportLayerException in several cases ! * @version v0.10 ! * @see TransportLayerException ! */ ! public int ReservePort(Object application,String inDestIPAddress, int indestPort) throws TransportLayerException { Integer number = new Integer(0); --- 256,320 ---- */ ! public void sendUDPPacket(int sock, String inDestIPAddress, int indestPort, String inMessage) throws TransportLayerException, LowLinkException { ! String inSourceIPAddress; ! int insrcPort; ! ! insrcPort = 0; + try{ + if(mSL.get_socket(sock).src_port == 0){ + // we should reserve port for it + ReserveFreePort(sock); + mSL.get_socket(sock).src_IP = mParentStack.getSrcIP(); + } + + inSourceIPAddress = mSL.get_socket(sock).src_IP; + insrcPort = mSL.get_socket(sock).src_port; + + 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 Protocol"); + UDP_Info.setLayer("Transport"); + UDP_Info.setDescription("Created UDP packet for " + inDestIPAddress + ":" + indestPort +". (Local is " + inSourceIPAddress + ":" + insrcPort + ", socket " + sock + " )."); + Simulation.addLayerInfo(UDP_Info); + + IncSentDatagrammsNumber(); + + mParentStack.sendPacket(tosend); + + }catch(Exception e){ + + try { + + LayerInfo UDP_Info = new LayerInfo(getClass().getName()); + UDP_Info.setObjectName(mParentStack.getParentNodeName()); + UDP_Info.setDataType("UDP Packet"); + UDP_Info.setLayer("Transport"); + UDP_Info.setDescription("UDP sending error: \""+ e.toString() + "\"."); + Simulation.addLayerInfo(UDP_Info); + ClosePort(sock); + //*TODO*: here we shall put some layerinfo for TransportException + } catch (TransportLayerException te) + { + LayerInfo UDP_Info = new LayerInfo(getClass().getName()); + UDP_Info.setObjectName(mParentStack.getParentNodeName()); + UDP_Info.setDataType("UDP Packet"); + UDP_Info.setLayer("Transport"); + UDP_Info.setDescription("UDP port freeing: \""+ te.toString() + "\"."); + Simulation.addLayerInfo(UDP_Info); + } + + } } ! // if application tries to send, but no port binded, do it now.. ! public void ReserveFreePort(int sock) throws TransportLayerException { Integer number = new Integer(0); *************** *** 336,341 **** ! UDP_HashTableElement Elm = new UDP_HashTableElement(); while ( (LocalPorts.hasMoreElements()) && !(Found) ) { --- 324,330 ---- ! UDP_session Elm = new UDP_session(); + // check if socket already busy while ( (LocalPorts.hasMoreElements()) && !(Found) ) { *************** *** 343,378 **** ! Elm = (UDP_HashTableElement) PortTable.get(number); ! ! 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; let's check whom we are connected to ! if (Elm.connectedtoIP.equals(inDestIPAddress)) ! { ! //now goes port number ! if (Elm.connectedtoPort==indestPort) ! { ! //everything is OK, need to return "number" ! ! } else { ! throw new TransportLayerException("UDP Error: application is already connected to another port number! Connected to host: " + Elm.connectedtoIP + ":" + Elm.connectedtoPort + "."); ! } ! } else { ! throw new TransportLayerException("UDP Error: application is already connected to another IP! Connected to host: " + Elm.connectedtoIP + "."); ! } ! } } ! //in case we have not found our application in hash table..... if (!Found) { --- 332,346 ---- ! Elm = (UDP_session) PortTable.get(number); ! if ( sock == Elm.sock && Elm.sock >=0 ) { Found=true; ! throw new TransportLayerException("UDP Error: socket is already busy (have port)!"); } } ! //in case we have not found socket in hash table..... if (!Found) { *************** *** 381,554 **** while ( (number<PORT_START_NUMBER+PORT_QUANT) && !(Reserved) ) { ! Elm = (UDP_HashTableElement) PortTable.get((Integer)number); ! if (Elm.PortStatus == 0) //free port { ! Reserved=true; ! Elm.PortStatus=2; //port will be busy from now ! Elm.connectedtoIP=inDestIPAddress; ! Elm.connectedtoPort=indestPort; ! Elm.application = application; ! } ! if (!Reserved) number++; } if (!Reserved) //all ports are busy :( { ! 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; ! } ! ! /** ! * This method closes the UDP port for an application ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @return Nothing ! * @exception TransportLayerException ! * @version v0.10 ! * @see TransportLayerException ! */ ! public void ClosePort(Object application) throws TransportLayerException //changes port status from 1 (listen) or (see proc ;) to 0 (free) ! { ! UDP_HashTableElement Elm=null; ! Integer PortToClose=getApplicationLocalPortNumber(application); ! ! Elm=(UDP_HashTableElement) PortTable.get((Integer)PortToClose); ! ! if (Elm.PortStatus==1 || (Elm.PortStatus==2 && PortToClose<PORT_START_NUMBER)) ! { ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! } else throw new TransportLayerException("UDP Error: port "+ PortToClose +" is not being LISTENED to by the application: \"" + application.toString() + "\"."); ! } ! ! /** ! * This method sets the UDP port free ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @return Nothing ! * @exception TransportLayerException ! * @version v0.10 ! * @see TransportLayerException ! */ ! public void FreePort(Object application) throws TransportLayerException //changes port status from 2 (busy) to 0 (free) ! { ! UDP_HashTableElement Elm=null; ! Integer PortToFree=getApplicationLocalPortNumber(application); ! ! Elm=(UDP_HashTableElement) PortTable.get((Integer)PortToFree); ! ! if (Elm.PortStatus==2) ! { ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! } else throw new TransportLayerException("UDP Error: port "+ PortToFree +" is not BUSY by the application: \"" + application.toString() + "\"."); ! } ! ! /** ! * This method sets the UDP port totally free ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @return Nothing ! * @exception TransportLayerException ! * @version v0.10 ! * @see TransportLayerException ! */ ! public void FreeApplication(Object application) throws TransportLayerException //changes any port status to 0 (free) ! { ! UDP_HashTableElement Elm=null; ! ! try{ ! Integer PortToFree=getApplicationLocalPortNumber(application); ! Elm=(UDP_HashTableElement) PortTable.get((Integer)PortToFree); ! ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! ! }catch(Exception e){} ! } ! ! /** ! * This method checks whether server is already listening to port ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @return int - port number if listens or PORT_INIT if not listens ! * @version v0.10 ! */ ! public int AlreadyListens(Object application) ! { ! boolean bo=false; ! int listensto=PORT_INIT; ! int localPort; ! ! try { ! localPort=getApplicationLocalPortNumber(application); ! listensto=localPort; ! } catch (TransportLayerException e) ! {} ! ! return listensto; ! } ! ! ! /** ! * This method sets the UDP port status to 1 (is being listened to) ! * NOTE: server must listen to UDP port numbers from 0 to 10000 ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @param in_Port port that the application want to listen to ! * @return Nothing ! * @exception TransportLayerException ! * @version v0.10 ! * @see TransportLayerException ! */ ! public void ListenPort(Object application, int in_Port) throws TransportLayerException { int lsPort=PORT_INIT; Integer inPort = new Integer(in_Port); ! if ( (lsPort=AlreadyListens(application)) == PORT_INIT ) { if (inPort>=0 && inPort<=10000) { ! UDP_HashTableElement Elm = null; ! if ( (Elm =(UDP_HashTableElement)PortTable.get(inPort)) !=null ) { switch(Elm.PortStatus) --- 349,393 ---- while ( (number<PORT_START_NUMBER+PORT_QUANT) && !(Reserved) ) { ! Elm = (UDP_session) PortTable.get((Integer)number); ! if (Elm == null) //free port { ! Reserved=true; ! Elm = new UDP_session(); ! Elm.PortStatus=1; //port will be busy from now ! Elm.sock = sock; ! mSL.get_socket(sock).src_port = number; ! PortTable.put(new Integer(number),Elm); ! LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Protocol"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Local port " + number + " reserved for client app."); ! Simulation.addLayerInfo(UDP_Info); ! } ! ! if (!Reserved) number++; } if (!Reserved) //all ports are busy :( { ! throw new TransportLayerException("UDP Error: all ports are busy! Cannot reserve port for socket!"); } ! } ! } ! ! // bind port to socket ! public void BindPort(int sock_num, int in_Port) throws TransportLayerException { int lsPort=PORT_INIT; Integer inPort = new Integer(in_Port); ! if ( mSL.get_socket(sock_num).src_port == 0 ) { if (inPort>=0 && inPort<=10000) { ! UDP_session Elm = null; ! if ( (Elm =(UDP_session)PortTable.get(inPort)) !=null ) { switch(Elm.PortStatus) *************** *** 556,576 **** case 0: //port is free => update such a record in hashtable Elm.PortStatus=1; ! Elm.application=application; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; break; case 1: // port is being listened to by whom? ! int UID1=-2; ! int UID2=-1; ! UID1=((Application)Elm.application).getUID(); ! UID2=((Application)application).getUID(); ! ! if (UID1==UID2) { ! throw new TransportLayerException("error: already listening to port "+ inPort +"!"); ! } else throw new TransportLayerException("error: port "+ inPort +" is being listened to by another application! Can listen only to free ports." ); ! case 2: //port is busy => error ! throw new TransportLayerException("error: port "+ inPort +" is busy! Can listen only to free ports." ); ! // no "break" needed ;) default: //unknown port status throw new TransportLayerException("error: unknown port status! Port "+ inPort +" on host \""+ mParentStack.getParentNodeName()+"\"."); --- 395,406 ---- case 0: //port is free => update such a record in hashtable Elm.PortStatus=1; ! Elm.sock = sock_num; ! mSL.get_socket(sock_num).src_port = in_Port; break; case 1: // port is being listened to by whom? ! if (sock_num==Elm.sock) { ! throw new TransportLayerException("error: already binded to port "+ inPort +"!"); ! } else throw new TransportLayerException("error: port "+ inPort +" is binded by another application! Can bind only to free ports." ); default: //unknown port status throw new TransportLayerException("error: unknown port status! Port "+ inPort +" on host \""+ mParentStack.getParentNodeName()+"\"."); *************** *** 580,692 **** { //create such a record in hashtable ! UDP_HashTableElement newElm = new UDP_HashTableElement(); ! newElm.PortStatus=1; ! newElm.application=application; ! newElm.connectedtoIP=""; ! newElm.connectedtoPort=0; ! PortTable.put(inPort,newElm); //hash table update } } else // { - /* LayerInfo UDP_Info = new LayerInfo(getClass().getName()); - UDP_Info.setObjectName(mParentStack.getParentNodeName()); - UDP_Info.setDataType("UDP Applicaion"); - UDP_Info.setLayer("Transport"); - UDP_Info.setDescription("UDP port listening: \"UDP Error: can not listen to port "+ inPort +"! Use port range from 0 to 10000 to listen to."); - Simulation.addLayerInfo(UDP_Info); */ throw new TransportLayerException("error: can not listen to port "+ inPort +"! Use port range from 0 to 10000 to listen to."); } } else // { ! /* LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Applicaion"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP port listening: \"UDP Error: can not listen to port "+ inPort +"! Already listening to port " + lsPort + ".\"."); ! Simulation.addLayerInfo(UDP_Info); */ ! if (lsPort==inPort) throw new TransportLayerException("error: can not double listen to port "+ inPort +"! Server is already listening to this port"); throw new TransportLayerException("error: can not listen to port "+ inPort +"! Already listening to port " + lsPort); } ! ! /* LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Applicaion"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP application is now listening on UDP port "+ inPort +"."); ! Simulation.addLayerInfo(UDP_Info); */ ! } ! ! ! ! /** ! * 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; ! } ! ! ! /** ! * This method finds LOCAL port number for an application. Port number range: [PORT_START_NUMBER; PORT_START_NUMBER+PORT_QUANT] ! * @author gift (sourceforge.net user) ! * @param application which local port will be returned ! * @return int port number that has been reserved for application ! * @exception TransportLayerException in several cases ! * @version v0.10 ! * @see TransportLayerException ! */ ! 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); ! ! 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 ! //everything is OK, need to return "number" ! } ! } ! ! //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 get local reserved port number."); ! } ! ! return (number); //do not change here! if you want to change here scan again the code } - /*STATISTIC PART */ --- 410,467 ---- { //create such a record in hashtable ! UDP_session newElm = new UDP_session(); ! newElm.PortStatus=1; ! newElm.sock = sock_num; ! mSL.get_socket(sock_num).src_port = in_Port; ! PortTable.put(inPort,newElm); //hash table update } + LayerInfo UDP_Info = new LayerInfo(getClass().getName()); + UDP_Info.setObjectName(mParentStack.getParentNodeName()); + UDP_Info.setDataType("UDP Protocol"); + UDP_Info.setLayer("Transport"); + UDP_Info.setDescription("Local port " + in_Port + " binded."); + Simulation.addLayerInfo(UDP_Info); } else // { throw new TransportLayerException("error: can not listen to port "+ inPort +"! Use port range from 0 to 10000 to listen to."); } } else // { ! if (mSL.get_socket(sock_num).src_port==inPort) throw new TransportLayerException("error: can not double listen to port "+ inPort +"! Server is already listening to this port"); throw new TransportLayerException("error: can not listen to port "+ inPort +"! Already listening to port " + lsPort); } ! } ! /** ! * This method closes the UDP port for an application ! * @author gift (sourceforge.net user) ! * @param application application that uses port ! * @return Nothing ! * @exception TransportLayerException ! * @version v0.10 ! * @see TransportLayerException ! */ ! public void ClosePort(int sock) throws TransportLayerException //changes port status from 1 (listen) or (see proc ;) to 0 (free) ! { ! UDP_session Elm=null; ! Integer PortToClose=mSL.get_socket(sock).src_port; ! Elm=(UDP_session) PortTable.get((Integer)PortToClose); ! ! if(PortToClose!=0 && Elm!=null){ ! if (Elm.PortStatus==1) ! { ! Elm.PortStatus=0; ! Elm.sock = -1; ! LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Protocol"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Local port " + PortToClose + " closed and set to free."); ! Simulation.addLayerInfo(UDP_Info); ! PortTable.remove((Integer)PortToClose); ! } else throw new TransportLayerException("UDP Error: port "+ PortToClose +" is not being LISTENED to by the application: \"" + mSL.get_socket(sock).app.toString() + "\"."); ! } else throw new TransportLayerException("UDP Error: port "+ PortToClose +" is not being LISTENED to by the application: \"" + mSL.get_socket(sock).app.toString() + "\"."); } /*STATISTIC PART */ Index: Echo.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Echo.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** Echo.java 9 Sep 2006 13:43:58 -0000 1.21 --- Echo.java 16 Sep 2007 19:38:04 -0000 1.22 *************** *** 30,37 **** --- 30,40 ---- long utc1; public int recieved; + int sock; /** Creates a new instance of Echo */ public Echo(ProtocolStack inParentStack, int listenPort, int appType, int UID) { super(inParentStack, listenPort, appType, UID); + sock = mParentStack.SL().socket(jnSocket.UDP_socket, this); + int fake = 0; } *************** *** 42,51 **** */ public void Listen() throws TransportLayerException{ ! //throw new TransportLayerException("Cannot bind port " + listenPort + "."); ! try{ ! mParentStack.ListenUDP(this, listenPort); ! } catch (TransportLayerException e) { ! } } --- 45,56 ---- */ public void Listen() throws TransportLayerException{ ! // ! try{ ! mParentStack.SL().bind(sock, mParentStack.getSrcIP(), listenPort); ! printLayerInfo("Echo app", "Echo server starts listening in port " + listenPort + "."); ! } catch (Exception e) { ! printLayerInfo("Echo app", "Error: cannot bind port " + listenPort + "."); ! throw new TransportLayerException("Cannot bind port " + listenPort + "."); } } *************** *** 60,68 **** public void Close() throws TransportLayerException { ! mParentStack.CloseUDP(this); } /** * This method connects to server on the other side (imaginations from the other side.... :+) * @author key * @param Host - hostname or ip of server. --- 65,75 ---- public void Close() throws TransportLayerException { ! printLayerInfo("Echo app", "Echo app closed socket."); ! mParentStack.SL().close(sock); } /** * This method connects to server on the other side (imaginations from the other side.... :+) + * This is "fake" method cos of UDP * @author key * @param Host - hostname or ip of server. *************** *** 74,79 **** sdHost = Host; sdPort = port; ! clientPort = mParentStack.reserveUDPPort(this, sdHost, sdPort); ! if (clientPort>0) return true; else return false; } --- 81,87 ---- sdHost = Host; sdPort = port; ! //clientPort = mParentStack.SL().reserveUDPPort(this, sdHost, sdPort); ! //if (clientPort>0) return true; else return false; ! return true; } *************** *** 85,90 **** */ public void Connected(String Host, int port){ ! sdHost = Host; ! sdPort = port; } --- 93,98 ---- */ public void Connected(String Host, int port){ ! /*sdHost = Host; ! sdPort = port;*/ } *************** *** 104,108 **** public void Disconnect() throws TransportLayerException { ! mParentStack.freeUDPPort(this); } --- 112,116 ---- public void Disconnect() throws TransportLayerException { ! mParentStack.SL().close(sock); } *************** *** 117,122 **** public void SendData(String Data) throws LowLinkException, TransportLayerException, CommunicationException { ! ! mParentStack.sendUDP(this, Data); //processing the protocol doings. --- 125,129 ---- public void SendData(String Data) throws LowLinkException, TransportLayerException, CommunicationException { ! mParentStack.SL().writeTo(sock, Data, sdHost, sdPort); //processing the protocol doings. *************** *** 143,153 **** protInfo.setDescription("Recieving echo message '" + Data + "' from server."); Simulation.addLayerInfo(protInfo); ! /* ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("Echo Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving echo message '" + Data + "' from server."); ! Simulation.addLayerInfo(protInfo);*/ Disconnect(); --- 150,154 ---- protInfo.setDescription("Recieving echo message '" + Data + "' from server."); Simulation.addLayerInfo(protInfo); ! Disconnect(); *************** *** 159,163 **** protInfo.setDataType("Echo Protocol Data"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving echo message '" + Data + "' from client. Total recieved messages: " + recieved); Simulation.addLayerInfo(protInfo); --- 160,164 ---- protInfo.setDataType("Echo Protocol Data"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving echo message '" + Data + "' from client " + sdHost + ":" + sdPort + ". Total recieved messages by server: " + recieved); Simulation.addLayerInfo(protInfo); *************** *** 171,184 **** SendData(Data); ! LayerInfo protInfo3 = new LayerInfo(getClass().getName()); protInfo3.setObjectName(mParentStack.getParentNodeName()); protInfo3.setDataType("Echo Protocol Data"); protInfo3.setLayer("Application "); protInfo3.setDescription("Server closing connection. Now listening on " + listenPort + "."); ! Simulation.addLayerInfo(protInfo3); ! Close(); ! Listen(); }catch(Exception e){ ///*TODO*: here to catch } --- 172,186 ---- SendData(Data); ! /*LayerInfo protInfo3 = new LayerInfo(getClass().getName()); protInfo3.setObjectName(mParentStack.getParentNodeName()); protInfo3.setDataType("Echo Protocol Data"); protInfo3.setLayer("Application "); protInfo3.setDescription("Server closing connection. Now listening on " + listenPort + "."); ! Simulation.addLayerInfo(protInfo3);*/ ! //Close(); ! //Listen(); }catch(Exception e){ + System.out.println(e.toString()); ///*TODO*: here to catch } *************** *** 195,199 **** */ public void SendEcho(String Data, String Host, int port, int counts) throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException, TransportLayerException{ ! mParentStack.FreeUDPApplication(this); utc1 = System.currentTimeMillis () ; --- 197,201 ---- */ public void SendEcho(String Data, String Host, int port, int counts) throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException, TransportLayerException{ ! //mParentStack.FreeUDPApplication(this); utc1 = System.currentTimeMillis () ; *************** *** 201,231 **** for(int c=0; c<counts; c++){ ! if (ClientConnect(Host, port)) ! { ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("Echo Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Start sending echo message '" + Data + "' to " + Host + ":" + port); ! Simulation.addLayerInfo(protInfo); ! SendData(Data); ! } else ! { ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("Echo Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Error: can not connect to " + Host + ":" + port + "!"); ! Simulation.addLayerInfo(protInfo); ! c=counts; ! } } ! LayerInfo protInfo2 = new LayerInfo(getClass().getName()); ! protInfo2.setObjectName(mParentStack.getParentNodeName()); ! protInfo2.setDataType("Echo Protocol Data"); ! protInfo2.setLayer("Application "); ! protInfo2.setDescription("Connection time: " + (long)(System.currentTimeMillis () - utc1) + " ms. Sent messages: " + counts + " Recieved messages: " + recieved); ! Simulation.addLayerInfo(protInfo2); } --- 203,232 ---- for(int c=0; c<counts; c++){ ! if (ClientConnect(Host, port)) ! { ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("Echo Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Start sending echo message '" + Data + "' to " + Host + ":" + port); ! Simulation.addLayerInfo(protInfo); ! SendData(Data); ! } else { ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("Echo Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Error: can not connect to " + Host + ":" + port + "!"); ! Simulation.addLayerInfo(protInfo); ! c=counts; ! } } ! LayerInfo protInfo2 = new LayerInfo(getClass().getName()); ! protInfo2.setObjectName(mParentStack.getParentNodeName()); ! protInfo2.setDataType("Echo Protocol Data"); ! protInfo2.setLayer("Application "); ! protInfo2.setDescription("Connection time: " + (long)(System.currentTimeMillis () - utc1) + " ms. Sent messages: " + counts + " Recieved messages: " + recieved); ! Simulation.addLayerInfo(protInfo2); } *************** *** 235,242 **** * @author gift (sourceforge.net user) * @version v0.01 */ public void RecvIP(String IP) throws LowLinkException, TransportLayerException { ! } --- 236,244 ---- * @author gift (sourceforge.net user) * @version v0.01 + * WARNING: Soon this 'll be DEPRECATED! */ public void RecvIP(String IP) throws LowLinkException, Trans... [truncated message content] |