Skip to content

Commit 9e8d71d

Browse files
committed
Updated examples
1 parent 9bc55a9 commit 9e8d71d

12 files changed

+40
-52
lines changed

Examples/Announce.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import argparse
88
import random
9+
import sys
910
import RNS
1011

1112
# Let's define an app name. We'll use this for all
@@ -168,4 +169,4 @@ def received_announce(self, destination_hash, announced_identity, app_data):
168169

169170
except KeyboardInterrupt:
170171
print("")
171-
exit()
172+
sys.exit(0)

Examples/Broadcast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ def broadcastLoop(destination):
118118

119119
except KeyboardInterrupt:
120120
print("")
121-
exit()
121+
sys.exit(0)

Examples/Buffer.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def client(destination_hexhash, configpath):
157157
destination_hash = bytes.fromhex(destination_hexhash)
158158
except:
159159
RNS.log("Invalid destination entered. Check your input!\n")
160-
exit()
160+
sys.exit(0)
161161

162162
# We must first initialise Reticulum
163163
reticulum = RNS.Reticulum(configpath)
@@ -254,9 +254,8 @@ def link_closed(link):
254254
else:
255255
RNS.log("Link closed, exiting now")
256256

257-
RNS.Reticulum.exit_handler()
258257
time.sleep(1.5)
259-
os._exit(0)
258+
sys.exit(0)
260259

261260
# When the buffer has new data, read it and write it to the terminal.
262261
def client_buffer_ready(ready_bytes: int):
@@ -320,4 +319,4 @@ def client_buffer_ready(ready_bytes: int):
320319

321320
except KeyboardInterrupt:
322321
print("")
323-
exit()
322+
sys.exit(0)

Examples/Channel.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def server(configpath):
124124
def server_loop(destination):
125125
# Let the user know that everything is ready
126126
RNS.log(
127-
"Link example "+
127+
"Channel example "+
128128
RNS.prettyhexrep(destination.hash)+
129129
" running, waiting for a connection."
130130
)
@@ -212,7 +212,7 @@ def client(destination_hexhash, configpath):
212212
destination_hash = bytes.fromhex(destination_hexhash)
213213
except:
214214
RNS.log("Invalid destination entered. Check your input!\n")
215-
exit()
215+
sys.exit(0)
216216

217217
# We must first initialise Reticulum
218218
reticulum = RNS.Reticulum(configpath)
@@ -276,7 +276,7 @@ def client_loop():
276276
packed_size = len(message.pack())
277277
channel = server_link.get_channel()
278278
if channel.is_ready_to_send():
279-
if packed_size <= channel.MDU:
279+
if packed_size <= channel.mdu:
280280
channel.send(message)
281281
else:
282282
RNS.log(
@@ -321,9 +321,8 @@ def link_closed(link):
321321
else:
322322
RNS.log("Link closed, exiting now")
323323

324-
RNS.Reticulum.exit_handler()
325324
time.sleep(1.5)
326-
os._exit(0)
325+
sys.exit(0)
327326

328327
# When a packet is received over the channel, we
329328
# simply print out the data.
@@ -387,4 +386,4 @@ def client_message_received(message):
387386

388387
except KeyboardInterrupt:
389388
print("")
390-
exit()
389+
sys.exit(0)

Examples/Echo.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
##########################################################
77

88
import argparse
9+
import sys
910
import RNS
1011

1112
# Let's define an app name. We'll use this for all
@@ -130,7 +131,7 @@ def client(destination_hexhash, configpath, timeout=None):
130131
except Exception as e:
131132
RNS.log("Invalid destination entered. Check your input!")
132133
RNS.log(str(e)+"\n")
133-
exit()
134+
sys.exit(0)
134135

135136
# We must first initialise Reticulum
136137
reticulum = RNS.Reticulum(configpath)
@@ -328,4 +329,4 @@ def packet_timed_out(receipt):
328329
client(args.destination, configarg, timeout=timeoutarg)
329330
except KeyboardInterrupt:
330331
print("")
331-
exit()
332+
sys.exit(0)

Examples/Filetransfer.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def client(destination_hexhash, configpath):
224224
destination_hash = bytes.fromhex(destination_hexhash)
225225
except:
226226
RNS.log("Invalid destination entered. Check your input!\n")
227-
exit()
227+
sys.exit(0)
228228

229229
# We must first initialise Reticulum
230230
reticulum = RNS.Reticulum(configpath)
@@ -462,7 +462,7 @@ def filelist_timeout_job():
462462
global server_files
463463
if len(server_files) == 0:
464464
RNS.log("Timed out waiting for filelist, exiting")
465-
os._exit(0)
465+
sys.exit(0)
466466

467467

468468
# When a link is closed, we'll inform the
@@ -475,9 +475,8 @@ def link_closed(link):
475475
else:
476476
RNS.log("Link closed, exiting now")
477477

478-
RNS.Reticulum.exit_handler()
479478
time.sleep(1.5)
480-
os._exit(0)
479+
sys.exit(0)
481480

482481
# When RNS detects that the download has
483482
# started, we'll update our menu state
@@ -601,4 +600,4 @@ def clear_screen():
601600

602601
except KeyboardInterrupt:
603602
print("")
604-
exit()
603+
sys.exit(0)

Examples/Identify.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def client(destination_hexhash, configpath):
133133
destination_hash = bytes.fromhex(destination_hexhash)
134134
except:
135135
RNS.log("Invalid destination entered. Check your input!\n")
136-
exit()
136+
sys.exit(0)
137137

138138
# We must first initialise Reticulum
139139
reticulum = RNS.Reticulum(configpath)
@@ -245,9 +245,8 @@ def link_closed(link):
245245
else:
246246
RNS.log("Link closed, exiting now")
247247

248-
RNS.Reticulum.exit_handler()
249248
time.sleep(1.5)
250-
os._exit(0)
249+
sys.exit(0)
251250

252251
# When a packet is received over the link, we
253252
# simply print out the data.
@@ -311,4 +310,4 @@ def client_packet_received(message, packet):
311310

312311
except KeyboardInterrupt:
313312
print("")
314-
exit()
313+
sys.exit(0)

Examples/Link.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
119119
destination_hash = bytes.fromhex(destination_hexhash)
120120
except:
121121
RNS.log("Invalid destination entered. Check your input!\n")
122-
exit()
122+
sys.exit(0)
123123

124124
# We must first initialise Reticulum
125125
reticulum = RNS.Reticulum(configpath)
@@ -222,9 +222,8 @@ def link_closed(link):
222222
else:
223223
RNS.log("Link closed, exiting now")
224224

225-
RNS.Reticulum.exit_handler()
226225
time.sleep(1.5)
227-
os._exit(0)
226+
sys.exit(0)
228227

229228
# When a packet is received over the link, we
230229
# simply print out the data.
@@ -288,4 +287,4 @@ def client_packet_received(message, packet):
288287

289288
except KeyboardInterrupt:
290289
print("")
291-
exit()
290+
sys.exit(0)

Examples/Minimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
##########################################################
66

77
import argparse
8+
import sys
89
import RNS
910

1011
# Let's define an app name. We'll use this for all
@@ -98,4 +99,4 @@ def announceLoop(destination):
9899

99100
except KeyboardInterrupt:
100101
print("")
101-
exit()
102+
sys.exit(0)

Examples/Ratchets.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
##########################################################
66

77
import argparse
8+
import sys
89
import RNS
910

1011
# Let's define an app name. We'll use this for all
@@ -138,7 +139,7 @@ def client(destination_hexhash, configpath, timeout=None):
138139
except Exception as e:
139140
RNS.log("Invalid destination entered. Check your input!")
140141
RNS.log(str(e)+"\n")
141-
exit()
142+
sys.exit(0)
142143

143144
# We must first initialise Reticulum
144145
reticulum = RNS.Reticulum(configpath)
@@ -337,4 +338,4 @@ def packet_timed_out(receipt):
337338
client(args.destination, configarg, timeout=timeoutarg)
338339
except KeyboardInterrupt:
339340
print("")
340-
exit()
341+
sys.exit(0)

Examples/Request.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
119119
destination_hash = bytes.fromhex(destination_hexhash)
120120
except:
121121
RNS.log("Invalid destination entered. Check your input!\n")
122-
exit()
122+
sys.exit(0)
123123

124124
# We must first initialise Reticulum
125125
reticulum = RNS.Reticulum(configpath)
@@ -226,9 +226,8 @@ def link_closed(link):
226226
else:
227227
RNS.log("Link closed, exiting now")
228228

229-
RNS.Reticulum.exit_handler()
230229
time.sleep(1.5)
231-
os._exit(0)
230+
sys.exit(0)
232231

233232

234233
##########################################################
@@ -284,4 +283,4 @@ def link_closed(link):
284283

285284
except KeyboardInterrupt:
286285
print("")
287-
exit()
286+
sys.exit(0)

Examples/Speedtest.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ def server_packet_received(message, packet):
149149
time.sleep(0.2)
150150
rc = 0
151151
received_data = 0
152-
# latest_client_link.teardown()
153-
# os._exit(0)
154152

155153

156154
##########################################################
@@ -159,6 +157,7 @@ def server_packet_received(message, packet):
159157

160158
# A reference to the server link
161159
server_link = None
160+
should_quit = False
162161

163162
# This initialisation is executed when the users chooses
164163
# to run as a client
@@ -175,7 +174,7 @@ def client(destination_hexhash, configpath):
175174
destination_hash = bytes.fromhex(destination_hexhash)
176175
except:
177176
RNS.log("Invalid destination entered. Check your input!\n")
178-
exit()
177+
sys.exit(0)
179178

180179
# We must first initialise Reticulum
181180
reticulum = RNS.Reticulum(configpath)
@@ -216,24 +215,15 @@ def client(destination_hexhash, configpath):
216215
client_loop()
217216

218217
def client_loop():
219-
global server_link
218+
global server_link, should_quit
220219

221220
# Wait for the link to become active
222221
while not server_link:
223222
time.sleep(0.1)
224223

225224
should_quit = False
226225
while not should_quit:
227-
try:
228-
text = input()
229-
230-
# Check if we should quit the example
231-
if text == "quit" or text == "q" or text == "exit":
232-
should_quit = True
233-
server_link.teardown()
234-
235-
except Exception as e:
236-
raise e
226+
time.sleep(0.2)
237227

238228
# This function is called when a link
239229
# has been established with the server
@@ -276,17 +266,17 @@ def link_established(link):
276266
# When a link is closed, we'll inform the
277267
# user, and exit the program
278268
def link_closed(link):
269+
global should_quit
279270
if link.teardown_reason == RNS.Link.TIMEOUT:
280271
RNS.log("The link timed out, exiting now")
281272
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
282273
RNS.log("The link was closed by the server, exiting now")
283274
else:
284275
RNS.log("Link closed, exiting now")
285-
286-
RNS.Reticulum.exit_handler()
287276

277+
should_quit = True
288278
time.sleep(1.5)
289-
os._exit(0)
279+
sys.exit(0)
290280

291281
def client_packet_received(message, packet):
292282
pass
@@ -344,4 +334,4 @@ def client_packet_received(message, packet):
344334

345335
except KeyboardInterrupt:
346336
print("")
347-
exit()
337+
sys.exit(0)

0 commit comments

Comments
 (0)