From 985ed3a200938da54b8fd26e08c9e0a57c87961e Mon Sep 17 00:00:00 2001 From: DanielaTh Date: Sat, 28 Nov 2015 20:18:39 -0300 Subject: [PATCH] modificacion receive del router --- src/Entities/Equipment/Router.java | 17 ++--- src/Entities/Equipment/Terminal.java | 40 ++++++------ src/Main/Main.java | 93 ++++++++++++++++------------ 3 files changed, 83 insertions(+), 67 deletions(-) diff --git a/src/Entities/Equipment/Router.java b/src/Entities/Equipment/Router.java index 4e2668e..903f17d 100644 --- a/src/Entities/Equipment/Router.java +++ b/src/Entities/Equipment/Router.java @@ -41,8 +41,7 @@ public void sendPacket(Packet packet) { return; } } - } - + } } @@ -51,13 +50,14 @@ public void receivePacket(Packet packet) { ServicePacket responsePacket = new ServicePacket(packet.getDestination(),packet.getSource(),new Sendmsg(),packet.getTtl() -1,""); if(packet instanceof RoutePacket){ - if (packetReceived(packet)){ - packet.setTtl(packet.getTtl() -1); - this.sendPacket(packet); + Packet pktReceived = ((RoutePacket) packet).getPacket(); + if (packetReceived(pktReceived)){ + packet.setTtl(pktReceived.getTtl() -1); + this.sendPacket(pktReceived); } else{ RoutePacket routePacket = new RoutePacket(this.getAssociatedIp(), - packet.getDestination(), packet.getServiceType(),packet.getTtl(), "", packet); + packet.getDestination(), packet.getServiceType(),packet.getTtl(), "", pktReceived); defaultEquipment.receivePacket(routePacket); responsePacket.setText("Could not send the packet"); responsePacket.setServiceType(new Sendmsg()); @@ -113,8 +113,9 @@ private Integer getNextKey(){ private boolean packetReceived(Packet packet){ boolean received = false; - for (Equipment equipment : equipments) { - if(equipment.getAssociatedIp().equals(packet.getDestination())){ + for (Equipment equipment : equipments) { + + if(equipment.getAssociatedIp().sameNetwork(packet.getDestination())){ received = true; break; } diff --git a/src/Entities/Equipment/Terminal.java b/src/Entities/Equipment/Terminal.java index 1ec974a..4e13131 100644 --- a/src/Entities/Equipment/Terminal.java +++ b/src/Entities/Equipment/Terminal.java @@ -9,7 +9,7 @@ */ public class Terminal extends Equipment { - private TerminalOs operatingSystem; + private TerminalOs operatingSystem; private IPAddressV4 ed; public TerminalOs getOperatingSystem() { @@ -18,7 +18,7 @@ public TerminalOs getOperatingSystem() { public void setOperatingSystem(TerminalOs operatingSystem) { this.operatingSystem = operatingSystem; - } + } public IPAddressV4 getEd() { return ed; @@ -30,20 +30,22 @@ public void setEd(IPAddressV4 ed) { @Override public void receivePacket(Packet packet) { - if(packet.getDestination().equals(associatedIp)){ + if (packet.getDestination().equals(associatedIp)) { String message = packet.getText(); PacketType responseType = packet.getServiceType().getResponse(); - if(packet.getServiceType().isRequest()){ - if(responseType instanceof Who){ + if (packet.getServiceType().isRequest()) { + if (responseType instanceof Who) { message = operatingSystem.getDataVersion(); } - Packet response = new ServicePacket(packet.getDestination(),packet.getSource(),responseType,packet.getTtl(),message); + Packet response = new ServicePacket(packet.getDestination(), packet.getSource(), responseType, + packet.getTtl(), message); sendPacket(response); - }else{ - if(packet.getServiceType() instanceof ICMPResponse){ + } else { + if (packet.getServiceType() instanceof ICMPResponse) { message = packet.toString(); } - System.out.println(String.format("La ip: %s recibio el mensaje '%s' de la ip: %s",this.associatedIp.toString(),message, packet.getSource().toString())); + System.out.println(String.format("La ip: %s recibio el mensaje '%s' de la ip: %s", + this.associatedIp.toString(), message, packet.getSource().toString())); } } @@ -58,32 +60,33 @@ public void sendPacket(Packet packet) { } } - public Terminal(IPAddressV4 ip,TerminalOs operatingSystem){ + public Terminal(IPAddressV4 ip, TerminalOs operatingSystem) { super(ip); this.operatingSystem = operatingSystem; } - - public Terminal(IPAddressV4 ip,TerminalOs operatingSystem, IPAddressV4 ed) { + + public Terminal(IPAddressV4 ip, TerminalOs operatingSystem, IPAddressV4 ed) { super(ip); this.operatingSystem = operatingSystem; this.ed = ed; } - public void sendPacket(IPAddressV4 destination, PacketType packetType,String message) { + public void sendPacket(IPAddressV4 destination, PacketType packetType, String message) { boolean exists = false; Packet packet; for (Equipment equipment : equipments) { - if(equipment.associatedIp.sameNetwork(destination)){ - packet = new ServicePacket(this.associatedIp,destination,packetType,operatingSystem.getTtl(),message); + if (equipment.associatedIp.sameNetwork(destination)) { + packet = new ServicePacket(this.associatedIp, destination, packetType, operatingSystem.getTtl(), + message); equipment.receivePacket(packet); exists = true; } } - if(!exists){ - packet = new ServicePacket(this.associatedIp,this.ed,packetType,operatingSystem.getTtl(),message); + if (!exists) { + packet = new ServicePacket(this.associatedIp, destination, packetType, operatingSystem.getTtl(), message); RoutePacket routePacket = new RoutePacket(this.associatedIp, this.ed, packetType, - this.operatingSystem.getTtl(),"",packet); + this.operatingSystem.getTtl(), "", packet); this.sendPacket(routePacket); @@ -92,5 +95,4 @@ public void sendPacket(IPAddressV4 destination, PacketType packetType,String mes System.out.println(String.format("La ip: %s envio el mensaje '%s'", this.associatedIp.toString(), message)); } - } diff --git a/src/Main/Main.java b/src/Main/Main.java index 4f41dce..c99b575 100644 --- a/src/Main/Main.java +++ b/src/Main/Main.java @@ -10,81 +10,94 @@ public class Main { public static void main(String[] args) throws Exception { - try{ + try { IPAddressV4 ipHub1 = new IPAddressV4(190, 210, 2, 4); + IPAddressV4 ed = new IPAddressV4(192, 168, 100, 254); + IPAddressV4 ipRouter = new IPAddressV4(192, 168, 100, 254); + Hub hub1 = new Hub(15, ipHub1); - + TerminalOs osServerHub1 = new TerminalOs("Windows Server", "2012", 10); IPAddressV4 ipServerHub1 = new IPAddressV4(190, 210, 2, 1); - Terminal serverHub1 = new Terminal(ipServerHub1,osServerHub1); - + Terminal serverHub1 = new Terminal(ipServerHub1, osServerHub1); + serverHub1.setEd(ed); + TerminalOs osLaptopHub1 = new TerminalOs("Windows", "7", 10); IPAddressV4 ipLaptopHub1 = new IPAddressV4(190, 210, 2, 2); - Terminal LaptopHub1 = new Terminal(ipLaptopHub1,osLaptopHub1); - + Terminal laptopHub1 = new Terminal(ipLaptopHub1, osLaptopHub1); + laptopHub1.setEd(ed); + TerminalOs osDesktopHub1 = new TerminalOs("Windows", "10", 10); IPAddressV4 ipDesktopHub1 = new IPAddressV4(190, 210, 2, 3); - Terminal DesktopHub1 = new Terminal(ipDesktopHub1,osDesktopHub1); - + Terminal desktopHub1 = new Terminal(ipDesktopHub1, osDesktopHub1); + desktopHub1.setEd(ed); + hub1.associateEquipment(serverHub1); - hub1.associateEquipment(LaptopHub1); - hub1.associateEquipment(DesktopHub1); - + hub1.associateEquipment(laptopHub1); + hub1.associateEquipment(desktopHub1); + + IPAddressV4 ipHub2 = new IPAddressV4(10, 10, 5, 4); Hub hub2 = new Hub(15, ipHub2); - + TerminalOs osServerHub2 = new TerminalOs("Linux", "Ubuntu 14.05", 10); IPAddressV4 ipServerHub2 = new IPAddressV4(10, 10, 5, 1); - Terminal serverHub2 = new Terminal(ipServerHub2,osServerHub2); - + Terminal serverHub2 = new Terminal(ipServerHub2, osServerHub2); + serverHub2.setEd(ed); + TerminalOs osLaptopHub2 = new TerminalOs("Linux", "Mint", 10); IPAddressV4 ipLaptopHub2 = new IPAddressV4(10, 10, 5, 2); - Terminal LaptopHub2 = new Terminal(ipLaptopHub2,osLaptopHub2); - + Terminal laptopHub2 = new Terminal(ipLaptopHub2, osLaptopHub2); + laptopHub2.setEd(ed); + TerminalOs osDesktopHub2 = new TerminalOs("Linux", "Debian", 10); IPAddressV4 ipDesktopHub2 = new IPAddressV4(10, 10, 5, 3); - Terminal DesktopHub2 = new Terminal(ipDesktopHub2,osDesktopHub2); - + Terminal desktopHub2 = new Terminal(ipDesktopHub2, osDesktopHub2); + desktopHub2.setEd(ed); + hub2.associateEquipment(serverHub2); - hub2.associateEquipment(LaptopHub2); - hub2.associateEquipment(DesktopHub2); - + hub2.associateEquipment(laptopHub2); + hub2.associateEquipment(desktopHub2); + IPAddressV4 ipHub3 = new IPAddressV4(192, 168, 100, 4); Hub hub3 = new Hub(15, ipHub3); - + TerminalOs osServerHub3 = new TerminalOs("OS X", "Server 5", 10); IPAddressV4 ipServerHub3 = new IPAddressV4(192, 168, 100, 1); - Terminal serverHub3 = new Terminal(ipServerHub3,osServerHub3); - + Terminal serverHub3 = new Terminal(ipServerHub3, osServerHub3); + serverHub3.setEd(ed); + TerminalOs osLaptopHub3 = new TerminalOs("OS X", "10.9", 10); IPAddressV4 ipLaptopHub3 = new IPAddressV4(192, 168, 100, 2); - Terminal LaptopHub3 = new Terminal(ipLaptopHub3,osLaptopHub3); - + Terminal laptopHub3 = new Terminal(ipLaptopHub3, osLaptopHub3); + laptopHub3.setEd(ed); + TerminalOs osDesktopHub3 = new TerminalOs("OS X", "10.8", 10); IPAddressV4 ipDesktopHub3 = new IPAddressV4(192, 168, 100, 3); - Terminal DesktopHub3 = new Terminal(ipDesktopHub3,osDesktopHub3); - + Terminal desktopHub3 = new Terminal(ipDesktopHub3, osDesktopHub3); + desktopHub3.setEd(ed); + hub3.associateEquipment(serverHub3); - hub3.associateEquipment(LaptopHub3); - hub3.associateEquipment(DesktopHub3); - - + hub3.associateEquipment(laptopHub3); + hub3.associateEquipment(desktopHub3); + NetworkOs osRouter = new NetworkOs("Router SO", "1.0"); Hub defaultHub = new Hub(50, new IPAddressV4(192, 168, 100, 50)); - IPAddressV4 ipRouter = new IPAddressV4(192, 168, 100, 254); - Router router = new Router(ipRouter,defaultHub, osRouter); + + Router router = new Router(ipRouter, defaultHub, osRouter); + router.setConnectionsNumber(15); router.associateEquipment(hub1); router.associateEquipment(hub2); router.associateEquipment(hub3); - - + serverHub1.sendPacket(ipDesktopHub1, new Sendmsg(), "Vamoooo"); - serverHub1.sendPacket(ipDesktopHub1, new ICMPRequest(),"Larala"); - } - catch(Exception ex){ + serverHub1.sendPacket(ipDesktopHub1, new ICMPRequest(), "Larala"); + serverHub1.sendPacket(ipDesktopHub3, new Sendmsg(), "de una subred a otra"); - System.out.println("Error " + ex.getMessage()); + } catch (Exception ex) { + + System.out.println("Error " + ex.getMessage()); } }