Skip to content

Commit fdbe9c7

Browse files
committed
Añadida Switching table a switches
1 parent a656069 commit fdbe9c7

File tree

1 file changed

+93
-24
lines changed

1 file changed

+93
-24
lines changed

Main.py

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def __str__(self):
651651

652652
def debug(self, *args):
653653
print("DEBUG")
654-
print("MAC:", self.macdir)
654+
print("MAC:", self.macdir, int(self.macdir))
655655

656656
def rclick(self, event):
657657
global rclick_Object
@@ -828,6 +828,13 @@ def connect(self, objeto, cable):
828828
self.update()
829829
objeto.update()
830830

831+
if objeto.__class__.__name__ == "Switch":
832+
print("Connecting {} to {}".format(objeto, self))
833+
objeto.connectport(self)
834+
if self.__class__.__name__ == "Switch":
835+
print("Connecting {} to {}".format(objeto, self))
836+
self.connectport(objeto)
837+
831838
def disconnect(self, widget, *args, de=None):
832839
print("Cables:", self.cables)
833840
#QUICKFIX
@@ -867,6 +874,11 @@ def disconnect(self, widget, *args, de=None):
867874

868875
de.update()
869876

877+
if self.__class__.__name__ == "Switch":
878+
self.disconnectport(de)
879+
elif de.__class__.__name__ == "Switch":
880+
de.disconnectport(self)
881+
870882
self.update()
871883

872884
def delete(self, *widget, conf=1):
@@ -884,7 +896,7 @@ def delete(self, *widget, conf=1):
884896
else:
885897
raise
886898

887-
def packet_received(self, pck):
899+
def packet_received(self, pck, *args, port=None):
888900
print("Hola, soy {} y he recibido un paquete, pero no sé que hacer con él".format(self.name))
889901
if config.getboolean("DEBUG", "packet-received"):
890902
print(">Pck:",pck)
@@ -924,33 +936,77 @@ def __del__(self, *args):
924936

925937
class Switch(ObjetoBase):
926938
cnt = 1
927-
def __init__(self, x, y, *args, name="Default", maxconnections=4, ip=None):
939+
#El objeto puerto
940+
class Port():
941+
def __init__(self, switch):
942+
self.id = switch.portid
943+
self.dic = switch.pdic
944+
self.all = switch.pall
945+
switch.portid += 1
946+
self.switch = switch
947+
self.connection = None
948+
self.all[self.id] = self
949+
self.dic[self.id] = self.connection
950+
def connect(self, connection):
951+
self.connection = connection
952+
self.dic[self.id] = self.connection
953+
def disconnect(self):
954+
self.connection = None
955+
self.dic[self.id] = self.connection
956+
def is_available(self):
957+
if self.connection == None:
958+
return True
959+
return False
960+
961+
def __init__(self, x, y, *args, name="Default", maxconnections=5, ip=None):
928962
self.objectype = "Switch"
963+
self.portid = 0
964+
self.pdic = {}
965+
self.pall = {}
929966

930967
push_elemento("Creado objeto Switch")
931968
self.imgdir = resdir + "Switch.*"
932-
ObjetoBase.__init__(self, x, y, self.objectype, name=name)
969+
ObjetoBase.__init__(self, x, y, self.objectype, name=name, maxconnections=maxconnections)
933970
self.x = x
934971
self.y = y
935972
self.timeout = 900 #Segundos
936973

974+
for p in range(self.max_connections):
975+
self.Port(self)
976+
print(self.pall)
977+
937978
self.table = [
938-
#[MAC, nextpoint, expiration]
979+
#[MAC, port, expiration]
939980
] #Ya se usará
940981

941982
def deleteobject(self, *jhafg):
942983
self.image.destroy()
943984
push_elemento("Eliminado objeto " + self.objectype)
944985
self.__del__()
945986

946-
def packet_received(self, pck):
987+
def connectport(self, objeto):
988+
for port in self.pall:
989+
if self.pall[port].is_available():
990+
self.pall[port].connect(objeto)
991+
break
992+
print(self.pdic)
993+
994+
def disconnectport(self, objeto):
995+
for p in self.pdic:
996+
print("i: {}, idx: {}".format(p,self.pdic[p]))
997+
if objeto == self.pdic[p]:
998+
self.pall[p].disconnect()
999+
break
1000+
print(self.pdic)
1001+
1002+
def packet_received(self, pck, port=None):
9471003
macd = "{0:0112b}".format(pck.frame)[0:6*8]
9481004
macs = "{0:0112b}".format(pck.frame)[6*8+1:6*16+1]
9491005

9501006
#LO PRIMERO: AÑADIRLO A LA TABLA
9511007

952-
if int(macd,2) not in [x[0] for x in self.table]:
953-
self.table.append([int(macd,2), int(macs,2), int(time.time()+self.timeout)])
1008+
if int(macs,2) not in [x[0] for x in self.table]:
1009+
self.table.append([int(macs,2), port, int(time.time()+self.timeout)])
9541010
for tab in self.table:
9551011
if tab[2] <= time.time():
9561012
print("Ha llegado tu hora")
@@ -967,7 +1023,8 @@ def packet_received(self, pck):
9671023
print("self.macdir",int(self.macdir), int("{0:0112b}".format(pck.frame)[6*8+1:6*16+1],2))
9681024
print("TTL:", int(pck.str[64:72],2), pck.str[64:72])
9691025

970-
print("Soy un switch y mi deber es entregar el paquete a {}".format(int(macd,2)))
1026+
print("Soy {} y mi deber es entregar el paquete a {}".format(self.name,int(macd,2)))
1027+
print("El paquete llegó por el puerto {}".format(port))
9711028
dic = {}
9721029
for i in self.connections:
9731030
dic[int(i.macdir)] = i
@@ -977,11 +1034,14 @@ def packet_received(self, pck):
9771034
#Si macd en conn, enviarle el paquete
9781035
#Si existe una tabla de enrutamiento que contiene una ruta para macd, enviar por ahi
9791036
#Si no, enviar al siguiente, y así
1037+
print(">MAAAC:",int(macd,2), "DIIIC:")
9801038
if int(macd,2) in dic and ttl > 0:
9811039
pck.animate(self, dic[int(macd,2)])
9821040

983-
elif int(macd,2) in []:
984-
pass
1041+
elif int(macd,2) in [x[0] for x in self.table]:
1042+
for x in self.table:
1043+
if x[0] == int(macd,2):
1044+
pck.animate(self, self.pdic[x[1]])
9851045

9861046
elif "Switch" in [x.objectype for x in self.connections] and ttl >= 0:
9871047
print("Ahora lo enviamos al siguiente router")
@@ -999,14 +1059,16 @@ def packet_received(self, pck):
9991059
print("Tmplst:", tmplst)
10001060
obj = choice(tmplst)
10011061
print("Sending to:", obj)
1002-
#pck.frame = int("".join(( pck.str[:6*8+2], macsnew ,pck.str[6*16+1:] )),2)
10031062
pck.animate(self, obj)
10041063

10051064
def debug(self, *args):
1065+
print(self.pdic)
10061066
print("MyMac:", self.macdir)
10071067
row_format ="{:>20}" * 3
10081068
print(row_format.format("MAC", "NXT", "EXP s"))
10091069
for row in self.table:
1070+
if row[1] == None:
1071+
row[1] = "None"
10101072
print(row_format.format(row[0], row[1], int(row[2]-int(time.time()))))
10111073

10121074
#¿Tengo permisos de escritura?, no se si tendré permisos
@@ -1021,7 +1083,7 @@ def __init__(self, x, y, *args, name="Default", maxconnections=4, ip=None):
10211083
self.x = x
10221084
self.y = y
10231085

1024-
def packet_received(self,pck):
1086+
def packet_received(self,pck,port=None):
10251087
ttl = int(pck.str[64:72],2)
10261088
macs = "{0:0112b}".format(pck.frame)[6*8+1:6*16+1]
10271089
ttlnew = "{0:08b}".format(ttl-1)
@@ -1162,7 +1224,7 @@ def send_pck(self, *widget, to=None):
11621224
ping.animate(self, self.connections[0])
11631225

11641226
#Ver routing: https://en.wikipedia.org/wiki/IP_forwarding
1165-
def packet_received(self, pck):
1227+
def packet_received(self, pck, *args, port=None):
11661228
print("Hola, soy {} y he recibido un paquete, tal vez tenga que responder".format(self.name))
11671229
#Si el tipo de ping es x, responder, si es y imprimir info
11681230
if config.getboolean("DEBUG", "packet-received"):
@@ -1195,7 +1257,7 @@ def packet_received(self, pck):
11951257
if ty == 8:
11961258
print("El paquete era para mí, voy a responder un gracias :D")
11971259
ping = Ping.create(1, self.IP, int(pck.str[96:128],2))
1198-
frame = eth(int("{0:011b}".format(pck.frame)[6*8+1:6*16+1],2), int(self.macdir), ping)
1260+
frame = eth(int("{0:0112b}".format(pck.frame)[6*8+1:6*16+1],2), int(self.macdir), ping)
11991261
frame.applytopack(ping)
12001262

12011263
ping.animate(self, self.connections[0])
@@ -1356,13 +1418,12 @@ def send(self, de):
13561418

13571419
#Composición de movimientos lineales en eje x e y
13581420
#Siendo t=fps/s, v=px/s, v default = 84
1359-
def animate(self, start, end, fps=120, v=84, color=None):
1421+
def animate(self, start, end, fps=120, v=200, color=None, port=None):
13601422
if color == None:
13611423
if self.color != None:
13621424
color = self.color
13631425
else:
13641426
color = "#673AB7"
1365-
print("Animation started")
13661427
from math import sqrt, pi
13671428
#Long del cable
13681429
try:
@@ -1372,7 +1433,6 @@ def animate(self, start, end, fps=120, v=84, color=None):
13721433
w, h = cable.w + TheGrid.sqres, cable.h + TheGrid.sqres
13731434
x, y = cable.x*TheGrid.sqres-TheGrid.sqres/2, cable.y*TheGrid.sqres-TheGrid.sqres/2
13741435
xi, yi = (start.x-0.5)*TheGrid.sqres-x, (start.y-0.5)*TheGrid.sqres-y
1375-
print("xi {}, yi {}".format(xi,yi))
13761436
xf, yf = end.x, end.y
13771437
r = sqrt(cable.w**2+cable.h**2) #Pixeles totales
13781438
t=r/v #Tiempo en segundos que durara la animacion
@@ -1393,7 +1453,7 @@ def animate(self, start, end, fps=120, v=84, color=None):
13931453
TheGrid.animat_lay.put(image,x,y)
13941454
TheGrid.animat_lay.show_all()
13951455

1396-
print("x: {}, y: {}, tf:{}, spf*m:{}, t: {}".format(x/TheGrid.sqres,y/TheGrid.sqres,tf,int(spf*1000), t))
1456+
#print("x: {}, y: {}, tf:{}, spf*m:{}, t: {}".format(x/TheGrid.sqres,y/TheGrid.sqres,tf,int(spf*1000), t))
13971457
f = 0
13981458
x,y = xi,yi
13991459
sx,sy = (w-TheGrid.sqres)/tf,(h-TheGrid.sqres)/tf
@@ -1408,6 +1468,7 @@ def iteration():
14081468
nonlocal y
14091469
nonlocal ctx
14101470
nonlocal surface
1471+
nonlocal port
14111472
if f <= tf:
14121473
#Do things
14131474
#print("Current f: {}; x,y: {}, {}".format(f, x,y))
@@ -1429,10 +1490,18 @@ def iteration():
14291490
image.destroy()
14301491
del surface
14311492
#print("Paquete enviado a {}".format(end))
1432-
end.packet_received(self)
1493+
if end.__class__.__name__ == "Switch":
1494+
for p in end.pall:
1495+
if end.pall[p].connection == start:
1496+
port = p
1497+
break
1498+
print("PORT:", port)
1499+
end.packet_received(self,port=port)
1500+
return False
1501+
end.packet_received(self, port=port)
14331502
return False
14341503

1435-
print("GOB:",GObject.timeout_add(spf*1000, iteration))
1504+
GObject.timeout_add(spf*1000, iteration)
14361505

14371506

14381507
return True
@@ -1504,7 +1573,7 @@ def __init__(self):
15041573
pass
15051574

15061575
def create(r, sourceip, desti_ip, *n, payload=int( 4.3*10**19 ) << 6 | 42, \
1507-
flags=0b010, ttl=32):
1576+
flags=0b010, ttl=10):
15081577
self = Ping()
15091578
if r == 0:
15101579
Type = 8
@@ -1745,8 +1814,8 @@ def apply(self, *npi):
17451814
lprint(npi)
17461815

17471816
self.link.name = self.name_entry.get_text()
1748-
lprint([ self.link.builder.get_object(y).get_text() for y in ["chg_MAC-entry" + str(x) for x in range(0,5)] ])
1749-
self.link.macdir.str = ":".join( [ self.link.builder.get_object(y).get_text() for y in ["chg_MAC-entry" + str(x) for x in range(5)] ])
1817+
lprint([ self.link.builder.get_object(y).get_text() for y in ["chg_MAC-entry" + str(x) for x in range(0,6)] ])
1818+
self.link.macdir.str = ":".join( [ self.link.builder.get_object(y).get_text() for y in ["chg_MAC-entry" + str(x) for x in range(6)] ])
17501819
self.link.macdir.int = int(self.link.macdir.str.replace(":",""), 16)
17511820
self.link.macdir.bin = "{0:048b}".format(self.link.macdir.int)
17521821
if self.link.objectype == "Computer":

0 commit comments

Comments
 (0)