Skip to content

Commit

Permalink
modificacion receive del router
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaTh committed Nov 28, 2015
1 parent fe42863 commit 985ed3a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 67 deletions.
17 changes: 9 additions & 8 deletions src/Entities/Equipment/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public void sendPacket(Packet packet) {
return;
}
}
}

}
}


Expand All @@ -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());
Expand Down Expand Up @@ -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;
}
Expand Down
40 changes: 21 additions & 19 deletions src/Entities/Equipment/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Terminal extends Equipment {

private TerminalOs operatingSystem;
private TerminalOs operatingSystem;
private IPAddressV4 ed;

public TerminalOs getOperatingSystem() {
Expand All @@ -18,7 +18,7 @@ public TerminalOs getOperatingSystem() {

public void setOperatingSystem(TerminalOs operatingSystem) {
this.operatingSystem = operatingSystem;
}
}

public IPAddressV4 getEd() {
return ed;
Expand All @@ -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()));

}
}
Expand All @@ -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);

Expand All @@ -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));
}


}
93 changes: 53 additions & 40 deletions src/Main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Expand Down

0 comments on commit 985ed3a

Please sign in to comment.