1
1
import argparse
2
+ import os
2
3
import subprocess
4
+ import psutil
3
5
import threading
4
6
import time
5
7
from ipaddress import IPv4Network
@@ -104,15 +106,21 @@ def update_peer_list():
104
106
peer list
105
107
:return: None
106
108
"""
109
+ octets = ip .split ("." )
110
+ router = "{0}.{1}.{2}.1" .format (octets [0 ], octets [1 ], octets [2 ])
111
+ if utils .Utils .check_host_up (router , 80 ):
112
+ for i , peer in enumerate (dht1 ["peer_list" ]):
113
+ host , port = peer .split (":" )
114
+ peer_list = dht1 ["peer_list" ]
115
+ if not utils .Utils .check_host_up (host , int (port )):
116
+ peer_list .pop (i )
117
+ dht1 ["peer_list" ] = peer_list
118
+ else :
119
+ print ("\n router down! you are not connected to the Internet" )
120
+ os ._exit (1 )
107
121
timer = threading .Timer (15.0 , update_peer_list )
108
122
timer .daemon = True
109
123
timer .start ()
110
- for i , peer in enumerate (dht1 ["peer_list" ]):
111
- host , port = peer .split (":" )
112
- peer_list = dht1 ["peer_list" ]
113
- if not utils .Utils .check_host_up (host , int (port )):
114
- peer_list .pop (i )
115
- dht1 ["peer_list" ] = peer_list
116
124
117
125
118
126
# call peer list updater
@@ -121,14 +129,29 @@ def update_peer_list():
121
129
122
130
update_peer_list ()
123
131
132
+
133
+ def cleanup ():
134
+ """
135
+ Cleans up after shutdown, kills netcat processes
136
+ :return: Nothing
137
+ """
138
+ PROCNAME = "netcat"
139
+
140
+ for proc in psutil .process_iter ():
141
+ # check whether the process name matches
142
+ if proc .name () == PROCNAME :
143
+ print ("Killed [{1}]{0}" .format (PROCNAME , proc .pid ))
144
+ proc .kill ()
145
+
124
146
while True :
125
147
command = input ("Enter a command:" )
126
148
command = command .split (" " )
127
149
if command [0 ] == "/exit" :
128
150
peer_list = dht1 ["peer_list" ]
129
151
peer_list .remove ("{0}:{1}" .format (ip , PORT ))
130
152
dht1 ["peer_list" ] = peer_list
131
- exit ()
153
+ cleanup ()
154
+ exit ("Shutdown was successful" )
132
155
elif command [0 ] == "/push" :
133
156
if len (command ) != 3 :
134
157
print ("Incorrect usage: /push <key> <value>" )
0 commit comments