Move stuff off of top level

This commit is contained in:
Juhani Krekelä 2019-07-14 20:12:45 +03:00
parent 94c4e0fa0c
commit a534cacd0e
1 changed files with 71 additions and 64 deletions

View File

@ -61,77 +61,84 @@ def handle_user_command(backend, command):
else:
print('s - request status, i - request msgid, m - send message, ^D - quit')
# Create unbuffered version of stdin
unbuf_stdin = open(sys.stdin.buffer.fileno(), 'rb', buffering = 0)
_, interface, nick = sys.argv
def eventloop(proc):
# Create unbuffered version of stdin
unbuf_stdin = open(sys.stdin.buffer.fileno(), 'rb', buffering = 0)
proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', interface], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = sys.stderr, bufsize = 0)
# Set up a poll for inputs (but do output blockingly)
poll = select.poll()
poll.register(proc.stdout, select.POLLIN)
poll.register(unbuf_stdin, select.POLLIN)
# Tell the backend the status and nick
status = 0
nick = nick.encode('utf-8')
writeall(proc.stdin, bytes([status, len(nick)]) + nick)
input_buffer = bytearray()
# Read our MAC
mac = readall(proc.stdout, 6)
running = True
while running:
for fd, event in poll.poll():
if fd == proc.stdout.fileno() and event & select.POLLIN:
event_type = readall(proc.stdout, 1)
if event_type == b's':
# Status
source_mac = readall(proc.stdout, 6)
status, = readall(proc.stdout, 1)
nick_length, = readall(proc.stdout, 1)
nick = readall(proc.stdout, nick_length,)
print('Own mac: %s' % format_mac(mac))
print('%s (%s) ~%s' % (format_mac(source_mac), format_status(status), nick.decode('utf-8')))
print('s - request status, i - request msgid, m - send message, ^D - quit')
poll = select.poll()
poll.register(proc.stdout, select.POLLIN)
poll.register(unbuf_stdin, select.POLLIN)
input_buffer = bytearray()
running = True
while running:
for fd, event in poll.poll():
if fd == proc.stdout.fileno() and event & select.POLLIN:
event_type = readall(proc.stdout, 1)
if event_type == b's':
# Status
source_mac = readall(proc.stdout, 6)
status, = readall(proc.stdout, 1)
nick_length, = readall(proc.stdout, 1)
nick = readall(proc.stdout, nick_length,)
print('%s (%s) ~%s' % (format_mac(source_mac), format_status(status), nick.decode('utf-8')))
else:
# Not sth we handle yet
data = proc.stdout.read(1023)
if data == b'':
data = b'[!] ' + event_type
else:
data = b'[!] ' + event_type + data
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
# Not sth we handle yet
data = proc.stdout.read(1023)
if data == b'':
data = b'[!] ' + event_type
else:
data = b'[!] ' + event_type + data
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
elif fd == proc.stdout.fileno() and event & select.POLLHUP:
print('Backend exited')
running = False
elif fd == unbuf_stdin.fileno() and event & select.POLLIN:
data = unbuf_stdin.read(1024)
input_buffer.extend(data)
while True:
newline_location = input_buffer.find(b'\n')
if newline_location == -1:
break
line, _, input_buffer = input_buffer.partition(b'\n')
handle_user_command(proc.stdin, line.decode('utf-8'))
if data == b'':
# ^D
writeall(proc.stdin, b'q')
elif fd == proc.stdout.fileno() and event & select.POLLHUP:
print('Backend exited')
running = False
else:
raise Exception('Unreachable')
elif fd == unbuf_stdin.fileno() and event & select.POLLIN:
data = unbuf_stdin.read(1024)
input_buffer.extend(data)
proc.wait()
while True:
newline_location = input_buffer.find(b'\n')
if newline_location == -1:
break
line, _, input_buffer = input_buffer.partition(b'\n')
handle_user_command(proc.stdin, line.decode('utf-8'))
if data == b'':
# ^D
writeall(proc.stdin, b'q')
running = False
else:
raise Exception('Unreachable')
def main():
_, interface, nick = sys.argv
proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', interface], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = sys.stderr, bufsize = 0)
# Tell the backend the status and nick
status = 0
nick = nick.encode('utf-8')
writeall(proc.stdin, bytes([status, len(nick)]) + nick)
# Read our MAC
mac = readall(proc.stdout, 6)
print('Own mac: %s' % format_mac(mac))
print('s - request status, i - request msgid, m - send message, ^D - quit')
eventloop(proc)
proc.wait()
if __name__ == '__main__':
main()