更新代码

master
bettleChen 1 year ago
parent 54e3a4864b
commit 86d9a33716

@ -8,5 +8,17 @@
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/../网络仿真/NetworkAnalog/network.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="network" uuid="366f0f05-fea8-48a5-b951-96924aa39605">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:D:\头哥\NetworkAnalog\datas\network.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.40.1/org/xerial/sqlite-jdbc/3.40.1.0/sqlite-jdbc-3.40.1.0.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

@ -467,6 +467,15 @@ class NetWorkAnalog(Canvas):
conn_obj.ConfigCorrect = key[1]
self.tag_bind_event()
def show_obj(self, AllSimObj, conns):
self.AllSimObjs = AllSimObj
self.conns = conns
for key, sim_obj in self.AllSimObjs.items():
print(key)
sim_obj.create_img()
for conn in self.conns:
conn.draw_line()
def delete_obj(self):
# todo: 删除对象
"""
@ -919,32 +928,7 @@ class NetWorkAnalog(Canvas):
self.AllSimObjs.clear()
self.conns.clear()
def create_widget(self):
# todo: 创建页面
"""
创建整体页面布局
:return:
"""
self.create_rectangle(0, 0, self.width * 0.8, self.height * 0.85, outline="#7f6000", width=3) # 矩形框,左上角坐标,右下角坐标
round_rectangle(self, self.width * 0.82, 30, self.width * 0.98, self.height * 0.85 / 2 - 30, outline="#ffff00", width=3, fill="#f4b88e") # 矩形框,左上角坐标,右下角坐标
self.create_text(self.width * 0.82 + 120, 30 + 15, text="网络配置信息", anchor="n", font=('微软雅黑', 18, 'bold'))
round_rectangle(self, self.width * 0.82, self.height * 0.85 / 2, self.width * 0.98, self.height * 0.85 - 30, outline="#ffff00", width=2, fill="#f4b88e") # 矩形框,左上角坐标,右下角坐标
self.create_text(self.width * 0.82 + 120, self.height * 0.85 / 2 + 15, text="路由/交换表信息", anchor="n", font=('微软雅黑', 18, 'bold'))
# 显示左边的固定图片
img_height, text_height, split_width = self.height - 120, self.height - 60, 120
self.create_text(split_width, text_height, text="路由器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
router = self.create_image(split_width, img_height, image=self.router_img, anchor="nw")
self.create_text(split_width * 2, text_height, text="交换机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
switch = self.create_image(split_width * 2, img_height, image=self.switch_img, anchor="nw")
self.create_text(split_width * 3, text_height, text="集线器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
hub = self.create_image(split_width * 3, img_height, image=self.hub_img, anchor="nw")
self.create_text(split_width * 4, text_height, text="主机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
host = self.create_image(split_width * 4, img_height, image=self.host_img, anchor="nw")
self.bind_event(router, "路由器")
self.bind_event(switch, "交换机")
self.bind_event(hub, "集线器")
self.bind_event(host, "主机")
def create_config_button(self):
# 创建一个菜单栏,这里我们可以把他理解成一个容器,在窗口的上方
menubar = tk.Menu(root)
# 定义一个空菜单单元
@ -975,7 +959,32 @@ class NetWorkAnalog(Canvas):
menubar.add_command(label='清除屏幕', command=self.clear_canvas)
root.config(menu=menubar)
self.reload_data()
def create_widget(self):
# todo: 创建页面
"""
创建整体页面布局
:return:
"""
self.create_rectangle(0, 0, self.width * 0.8, self.height * 0.85, outline="#7f6000", width=3) # 矩形框,左上角坐标,右下角坐标
round_rectangle(self, self.width * 0.82, 30, self.width * 0.98, self.height * 0.85 / 2 - 30, outline="#ffff00", width=3, fill="#f4b88e") # 矩形框,左上角坐标,右下角坐标
self.create_text(self.width * 0.82 + 120, 30 + 15, text="网络配置信息", anchor="n", font=('微软雅黑', 18, 'bold'))
round_rectangle(self, self.width * 0.82, self.height * 0.85 / 2, self.width * 0.98, self.height * 0.85 - 30, outline="#ffff00", width=2, fill="#f4b88e") # 矩形框,左上角坐标,右下角坐标
self.create_text(self.width * 0.82 + 120, self.height * 0.85 / 2 + 15, text="路由/交换表信息", anchor="n", font=('微软雅黑', 18, 'bold'))
# 显示左边的固定图片
img_height, text_height, split_width = self.height - 120, self.height - 60, 120
self.create_text(split_width, text_height, text="路由器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
router = self.create_image(split_width, img_height, image=self.router_img, anchor="nw")
self.create_text(split_width * 2, text_height, text="交换机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
switch = self.create_image(split_width * 2, img_height, image=self.switch_img, anchor="nw")
self.create_text(split_width * 3, text_height, text="集线器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
hub = self.create_image(split_width * 3, img_height, image=self.hub_img, anchor="nw")
self.create_text(split_width * 4, text_height, text="主机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
host = self.create_image(split_width * 4, img_height, image=self.host_img, anchor="nw")
self.bind_event(router, "路由器")
self.bind_event(switch, "交换机")
self.bind_event(hub, "集线器")
self.bind_event(host, "主机")
if __name__ == '__main__':
@ -991,5 +1000,7 @@ if __name__ == '__main__':
(screen_height - root_attr['height']) / 2 - 30)
canvas = NetWorkAnalog(root, width=root_attr['width'], heigh=root_attr['height'], bg="white")
canvas.place(x=0, y=0, anchor='nw')
canvas.create_config_button()
canvas.reload_data()
root.geometry(size)
root.mainloop()

@ -26,6 +26,22 @@ class SimBase():
self.ObjX = x
self.ObjY = y
self.canvas = canvas
self.host_img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/主机.png").resize((60, 60)))
self.host_img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/主机_tm.png").resize((60, 60)))
self.router_img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/路由器.png").resize((60, 60)))
self.router_img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/路由器_tm.png").resize((60, 60)))
self.switch_img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/交换机.png").resize((60, 60)))
self.switch_img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/交换机_tm.png").resize((60, 60)))
self.hub_img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/集线器.png").resize((60, 60)))
self.hub_img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/集线器_tm.png").resize((60, 60)))
self.img = None
self.img_tm = None
self.interface = [{}, {}, {}, {}]
@ -94,6 +110,18 @@ class SimBase():
创建图片
:return:
"""
if self.ObjType == 1:
self.img = self.host_img
self.img_tm = self.host_img_tm
elif self.ObjType == 2:
self.img = self.router_img
self.img_tm = self.router_img_tm
elif self.ObjType == 3:
self.img = self.switch_img
self.img_tm = self.switch_img_tm
else:
self.img = self.hub_img
self.img_tm = self.hub_img_tm
self.canvas.delete("L")
id = self.canvas.create_image(self.ObjX - 30, self.ObjY - 30,
image=self.img if self.ConfigCorrect == 1 else self.img_tm, anchor="nw",
@ -455,10 +483,7 @@ class SimHost(SimBase):
self.interface = [{}]
self.connections = [None]
self.set_default_config()
self.img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/主机.png").resize((60, 60)))
self.img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/主机_tm.png").resize((60, 60)))
def create_packet(self, ip, mac, message):
"""
@ -519,10 +544,6 @@ class SimRouter(SimBase):
self.ObjLabel = label if label is not None else self.set_default_name()
self.router_table = {}
self.set_default_router_table()
self.img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/路由器.png").resize((60, 60)))
self.img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/路由器_tm.png").resize((60, 60)))
def set_default_router_table(self):
"""
@ -565,7 +586,9 @@ class SimRouter(SimBase):
if conn == packet.up_jump:
continue
ifs = self.connections.index(conn) + 1
for network in self.router_table[ifs]:
if self.router_table.get(ifs) is None:
continue
for network in self.router_table.get(ifs):
if self.check_destination_ip(packet.destination_ip, network):
flag = True
next_hop_ifs = ifs
@ -628,10 +651,6 @@ class SimSwitch(SimBase):
self.ObjLabel = label if label is not None else self.set_default_name()
self.mac_table = {}
self.set_default_mac_table()
self.img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/交换机.png").resize((60, 60)))
self.img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/交换机_tm.png").resize((60, 60)))
def set_default_mac_table(self):
"""
@ -721,10 +740,6 @@ class SimHub(SimBase):
super().__init__(canvas, x, y, self.ObjID, config, label)
self.ObjType = 4
self.ObjLabel = label if label is not None else self.set_default_name()
self.img = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/集线器.png").resize((60, 60)))
self.img_tm = ImageTk.PhotoImage(
Image.open(sys.path[0] + "/../datas/images/集线器_tm.png").resize((60, 60)))
def transmit(self, packet: SimPacket):
"""

Binary file not shown.

@ -1,63 +0,0 @@
class AllSimConnect():
# todo: 连接类
def __init__(self, nodex, nodex_ifs, nodey, nodey_ifs, config=None):
"""
连接对象
:param nodex: 节点
:param nodex_ifs: 节点接口
:param nodey: 节点
:param nodey_ifs: 节点接口
"""
self.ConfigCorrect = 0 if config is None else config
self.NobjS = nodex
self.NobjE = nodey
self.IfsS = nodex_ifs
self.IfsE = nodey_ifs
class SimHost():
"""
主机类
"""
def __init__(self, x=0, y=0, id=None, config=None, label=None):
self.ObjID = id
self.ObjType = 1
self.ObjLabel = label
self.interface = []
self.connections = []
class SimRouter():
"""
路由类
"""
def __init__(self, x=0, y=0, id=None, config=None, label=None, *args):
self.ObjID = id
self.ObjType = 2
self.ObjLabel = label
self.router_table = {}
class SimSwitch():
"""
交换机类
"""
def __init__(self, x=0, y=0, id=None, config=None, label=None, *args):
self.ObjID = id
self.ObjType = 3
self.ObjLabel = label
self.mac_table = {}
class SimHub():
"""
集线器类
"""
def __init__(self, x=0, y=0, id=None, config=None, label=None, *args):
self.ObjID = str() if id is None else id
self.ObjType = 4
self.ObjLabel = label
if __name__ == '__main__':
AllSimObj = []
host1 = SimHost()
AllSimObj.append(host1)
router = SimRouter()
AllSimObj.append(router)
connect = AllSimConnect(host1, 1, router, 1)

@ -0,0 +1,33 @@
from NetworkAnalog import NetWorkAnalog
from SimObjs import *
if __name__ == '__main__':
AllSimObj = {}
root = Window()
root.title('网络拓扑图')
root_attr = {
"width": root.winfo_screenwidth() * 0.83,
"height": root.winfo_screenheight() * 0.85,
}
size = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (root.winfo_screenwidth() - root_attr['width']) / 2,
(root.winfo_screenheight() - root_attr['height']) / 2 - 30)
canvas = NetWorkAnalog(root, width=root_attr['width'], heigh=root_attr['height'], bg="white")
canvas.place(x=0, y=0, anchor='nw')
root.geometry(size)
host1 = SimHost(canvas, 406, 268)
AllSimObj[host1.ObjID] = host1
host2 = SimHost(canvas, 323, 285)
AllSimObj[host2.ObjID] = host2
router = SimRouter(canvas, 216, 263)
AllSimObj[router.ObjID] = router
host1_router_connect = AllSimConnect(canvas, host1, 1, router, 1)
host2_router_connect = AllSimConnect(canvas, host2, 1, router, 2)
host1.connections[0] = host1_router_connect
router.connections[0] = host1_router_connect
host2.connections[0] = host2_router_connect
router.connections[1] = host2_router_connect
conns = [host1_router_connect, host2_router_connect]
canvas.show_obj(AllSimObj, conns)
root.mainloop()

Binary file not shown.
Loading…
Cancel
Save