當前位置: 首頁>>代碼示例>>Python>>正文


Python Router.initConnections方法代碼示例

本文整理匯總了Python中router.Router.initConnections方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.initConnections方法的具體用法?Python Router.initConnections怎麽用?Python Router.initConnections使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在router.Router的用法示例。


在下文中一共展示了Router.initConnections方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: userListen

# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import initConnections [as 別名]
	def userListen(self, *args):
		plt.ion()
		plt.show()
		
		#get user input (removing, asking stuff, testing, idk)
		while True:
			plt.pause(2)
			plt.draw()
			
			uInput = input("Enter number to choose option:\n\t[1] : List All Routers\n\t[2] : Send Text to Router"
						   "\n\t[3] : Show Network Graph\n\t[4] : Show Minimum Spanning Tree"
						   "\n\t[5] : Show Router's Forwarding Table\n\t[6] : Add Router\n\t[7] : Remove Router"
						   "\nEnter choice: ")

			#shows all router codes in network
			if (uInput == "1"):
				print(json.dumps(self.neighbors, sort_keys=True, indent=4), "\n")

			#send some text to a router
			elif (uInput == "2"):
				code = input("Enter router code to send to (i.e. A): ")
				try:
					qSend = self.arrSending[code]
					uMsg = input("Message: \n")
					qSend.put(self.wrapMessage("text", uMsg))
					print("Message '" + uMsg + "' sent.\n")
				except KeyError:
					print("That router does not exist.\n")

			#show network graph
			elif (uInput == "3"):

				for key, value in self.neighbors.items():
					data = self.wrapMessage("rGraph", ())

					# request updated graph and wait for it
					with self.condGraph:
						self.arrSending[key].put(data)
						self.condGraph.wait()
					break

				self.drawGraph(self.networkGraph)
				self.drawGraph(self.networkGraph)
				self.drawGraph(self.networkGraph)

			#show spanning tree
			elif (uInput == "4"):
				for key, value in self.neighbors.items():
					data = self.wrapMessage("rTree", ())

					# request updated graph and wait for it
					with self.condTree:
						self.arrSending[key].put(data)
						self.condTree.wait()
					break

				self.drawGraph(self.networkTree)
				#print(json.dumps(self.networkTree, sort_keys=True, indent=4), "\n")

			# show a specific router's forwarding table
			elif (uInput == "5"):
				
				code = input("Enter router code to get table (i.e. A): ")
				try:
					data = self.wrapMessage("rTable", ())

					#update this table with router's forwarding table
					with self.condTable:
						self.arrSending[code].put(data)
						self.condTable.wait()

					#print forwarding table
					print(json.dumps(self.forwarding, sort_keys=True, indent=4), "\n")
				except KeyError:
					print("That router does not exist.\n")

			#add a new router
			elif (uInput == "6"):
				code = input("Enter new router code (i.e. A): ")
				if code not in self.neighbors:
					port = input("Enter new port for router: ")
					try:
						nRouters = literal_eval(input("Enter routers to connect to in the form:\n"
													  "[('host', port), ('host', port), ...)]\n "))
						router = Router(code, "", eval(port))
						router.initConnections(nRouters)

						print("Router " + code + " added.\n")
					except:
						print("That wasn't formatted correctly.\n")

				else:
					print("That router already exists.\n")
					
				self.drawGraph(self.networkGraph)
				self.drawGraph(self.networkGraph)
				self.drawGraph(self.networkGraph)
			
			#remove a router
			elif (uInput == "7"):
#.........這裏部分代碼省略.........
開發者ID:Goahnary,項目名稱:network-emulator,代碼行數:103,代碼來源:monitor.py

示例2: Monitor

# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import initConnections [as 別名]
from router import Router
from monitor import Monitor
from server import Server

mon = Monitor("monitor", "", 5000)
r1 = Router("A", "", 5501)
r2 = Router("B", "", 5502)
r3 = Router("C", "", 5503)
r4 = Router("D", "", 5504)
r5 = Router("E", "", 5505)
r6 = Router("F", "", 5506)
r7 = Router("G", "", 5507)
r8 = Router("H", "", 5508)
r1.initConnections([])
r2.initConnections([("localhost", 5501)])
r3.initConnections([("localhost", 5501)])
r4.initConnections([("localhost", 5503)])
r5.initConnections([("localhost", 5503), ("localhost", 5504), ("localhost", 5502)])
r6.initConnections([("localhost", 5504)])
r7.initConnections([("localhost", 5506)])
r8.initConnections([("localhost", 5507), ("localhost", 5502), ("localhost", 5506)])

s = Server("server", "", 5678)
s.createConnection("localhost", 5508)

mon.userListen()
開發者ID:Goahnary,項目名稱:network-emulator,代碼行數:28,代碼來源:testnew.py


注:本文中的router.Router.initConnections方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。