Skip to content

Commit 2328254

Browse files
committed
Improve comments/docstrings/messages
1 parent b631aac commit 2328254

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

unpythonic/net/client.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@ def _handle_alarm(signum, frame):
2727
signal.signal(signal.SIGALRM, _handle_alarm)
2828

2929

30-
# Protocol for establishing connection:
30+
# Protocol for establishing a paired control/REPL connection:
3131
# - 1: Handshake: open the control channel, ask for metadata (prompts: sys.ps1, sys.ps2)
3232
# to configure the client's prompt detector before opening the primary channel.
33-
# - To keep us netcat compatible, handshake is optional. It is legal
33+
# - To keep us netcat compatible, the handshake is optional. It is legal
3434
# to just immediately connect on the primary channel, in which case
3535
# there will be no control channel paired with the REPL session.
36-
# - 2: Open the primary channel, parse the session id from the first line of text.
36+
# - 2: Open the primary channel. Parse the session id from the first line of text.
3737
# - To keep us netcat compatible, we must transmit the session id as
3838
# part of the primary data stream; it cannot be packaged into a message
39-
# since only `unpythonic` knows about the message protocol.
40-
# - So, print "Session XX connected\n" as the first line on the server side
41-
# when a client connects. In the client, parse the first line (beside
39+
# since only `unpythonic` knows about the message protocol, and the
40+
# REPL and control session server objects operate independently
41+
# (they must, since each accepts a separate incoming TCP connection,
42+
# which have nothing to do with each other).
43+
# - So, the server prints "session XX connected\n" on the first line
44+
# when a client connects. The client parses the first line (beside
4245
# printing it as usual, to have the same appearance for both unpythonic
43-
# and netcat connections).
44-
# - 3: Send command on the control channel to pair that control channel
45-
# to session id XX. Maybe print a message on the client side saying
46-
# that tab completion and Ctrl+C are available.
46+
# client and netcat connections).
47+
# - 3: Send a command on the control channel to pair that control channel
48+
# to session id XX.
4749

4850
# Messages must be processed by just one central decoder, to prevent data
4951
# races, but also to handle buffering of incoming data correctly, because
@@ -71,15 +73,15 @@ def _send_command(self, request):
7173
"""Send a command to the server, get the reply.
7274
7375
request: a dict-like, containing the "command" field and any required
74-
parameters (command-dependent).
76+
parameters (specific to each particular command).
7577
7678
On success, return the `reply` dict. On failure, return `None`.
7779
"""
7880
try:
7981
self._send(request)
8082
reply = self._recv()
8183
if not reply:
82-
print("Socket closed by other end.")
84+
print("Socket closed by server.")
8385
return None
8486
if reply["status"] == "ok":
8587
return reply
@@ -161,7 +163,7 @@ class SessionExit(Exception):
161163
for r in rs:
162164
data = sock.recv(4096)
163165
if len(data) == 0:
164-
print("replclient: disconnected by server.")
166+
print("unpythonic.net.client: disconnected by server.")
165167
raise SessionExit
166168
text = data.decode("utf-8")
167169
sys.stdout.write(text)
@@ -184,7 +186,7 @@ def sock_to_stdout():
184186
for r in rs:
185187
data = sock.recv(4096)
186188
if len(data) == 0:
187-
print("replclient: disconnected by server.")
189+
print("unpythonic.net.client: disconnected by server.")
188190
raise SessionExit
189191
text = data.decode("utf-8")
190192
sys.stdout.write(text)
@@ -219,20 +221,20 @@ def sock_to_stdout():
219221
except KeyboardInterrupt:
220222
controller.send_kbinterrupt()
221223
except EOFError:
222-
print("replclient: Ctrl+D pressed, asking server to disconnect.")
223-
print("replclient: if the server does not respond, press Ctrl+C to force.")
224+
print("unpythonic.net.client: Ctrl+D pressed, asking server to disconnect.")
225+
print("unpythonic.net.client: if the server does not respond, press Ctrl+C to force.")
224226
try:
225227
print("quit()") # local echo
226228
sock.sendall("quit()\n".encode("utf-8"))
227229
t.join() # wait for the EOF response
228230
except KeyboardInterrupt:
229-
print("replclient: Ctrl+C pressed, forcing disconnect.")
231+
print("unpythonic.net.client: Ctrl+C pressed, forcing disconnect.")
230232
finally:
231233
raise SessionExit
232234
except SystemExit: # catch the alarm signaled by the socket-listening thread.
233235
raise SessionExit
234236
except BrokenPipeError:
235-
print("replclient: socket closed unexpectedly, exiting.")
237+
print("unpythonic.net.client: socket closed unexpectedly, exiting.")
236238
raise SessionExit
237239

238240
except SessionExit:

unpythonic/net/server.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from unpythonic.net import server
1313
server.start(locals=globals())
1414
15-
To connect to a running REPL server::
15+
To connect to a running REPL server (with tab completion and Ctrl+C support)::
1616
1717
python3 -m unpythonic.net.client localhost 1337
1818
@@ -68,7 +68,6 @@
6868
The `socketserverREPL` package uses the same default, and actually its
6969
`repl_tool.py` can talk to this server (but doesn't currently feature
7070
remote tab completion).
71-
7271
"""
7372

7473
# TODO: use logging module instead of server-side print
@@ -209,7 +208,7 @@ class ClientExit(Exception):
209208
# may be included in arbitrary other fields.
210209
request = self._recv()
211210
if not request:
212-
server_print("Socket for {} closed by other end.".format(client_address_str))
211+
server_print("Socket for {} closed by client.".format(client_address_str))
213212
raise ClientExit
214213

215214
if "command" not in request:
@@ -340,14 +339,14 @@ def on_slave_disconnect(adaptor):
340339
# connection, not kill the server ungracefully. We have halt()
341340
# to do that gracefully.
342341
try:
343-
server_print("Opening session for {}.".format(client_address_str))
342+
server_print("Opening REPL session {} for {}.".format(self.session_id, client_address_str))
344343
self.console.interact(banner=None, exitmsg="Bye.")
345344
except SystemExit:
346345
pass
347346
finally:
348-
server_print('Closing PTY on {} for client {}.'.format(os.ttyname(adaptor.slave), client_address_str))
347+
server_print('Closing PTY on {} for {}.'.format(os.ttyname(adaptor.slave), client_address_str))
349348
adaptor.stop()
350-
server_print("Closing session for {}.".format(client_address_str))
349+
server_print("Closing REPL session {} for {}.".format(self.session_id, client_address_str))
351350
except BaseException as err:
352351
server_print(err)
353352
finally:
@@ -399,7 +398,8 @@ def start(locals, addrspec=("127.0.0.1", 1337), banner=None):
399398
# TODO: get name of module whose globals the session can update
400399
default_msg = ("Unpythonic REPL server at {addr}:{port}, on behalf of:\n"
401400
" {argv}\n"
402-
" Top-level assignments and definitions update the module's globals.\n"
401+
" Top-level assignments and definitions update the session locals;\n"
402+
" typically, these correspond to the globals of a module in the running app.\n"
403403
" quit() or EOF (Ctrl+D) at the prompt disconnects this session.\n"
404404
" halt() tells the server to close after the last session has disconnected.\n"
405405
" print() prints in the REPL session.\n"

0 commit comments

Comments
 (0)