Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 1 | # Remote nusers client interface |
| 2 | |
| 3 | import rpc |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 4 | from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class RnusersPacker(Packer): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 8 | def pack_utmp(self, ui): |
| 9 | ut_line, ut_name, ut_host, ut_time = utmp |
| 10 | self.pack_string(ut_line) |
| 11 | self.pack_string(ut_name) |
| 12 | self.pack_string(ut_host) |
| 13 | self.pack_int(ut_time) |
| 14 | def pack_utmpidle(self, ui): |
| 15 | ui_itmp, ui_idle = ui |
| 16 | self.pack_utmp(ui_utmp) |
| 17 | self.pack_uint(ui_idle) |
| 18 | def pack_utmpidlearr(self, list): |
| 19 | self.pack_array(list, self.pack_itmpidle) |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class RnusersUnpacker(Unpacker): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 23 | def unpack_utmp(self): |
| 24 | ut_line = self.unpack_string() |
| 25 | ut_name = self.unpack_string() |
| 26 | ut_host = self.unpack_string() |
| 27 | ut_time = self.unpack_int() |
| 28 | return ut_line, ut_name, ut_host, ut_time |
| 29 | def unpack_utmpidle(self): |
| 30 | ui_utmp = self.unpack_utmp() |
| 31 | ui_idle = self.unpack_uint() |
| 32 | return ui_utmp, ui_idle |
| 33 | def unpack_utmpidlearr(self): |
| 34 | return self.unpack_array(self.unpack_utmpidle) |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 35 | |
| 36 | |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 37 | class PartialRnusersClient: |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 38 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 39 | def addpackers(self): |
| 40 | self.packer = RnusersPacker() |
| 41 | self.unpacker = RnusersUnpacker('') |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 42 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 43 | def Num(self): |
| 44 | return self.make_call(1, None, None, self.unpacker.unpack_int) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 45 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 46 | def Names(self): |
| 47 | return self.make_call(2, None, \ |
| 48 | None, self.unpacker.unpack_utmpidlearr) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 49 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 50 | def Allnames(self): |
| 51 | return self.make_call(3, None, \ |
| 52 | None, self.unpacker.unpack_utmpidlearr) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class RnusersClient(PartialRnusersClient, UDPClient): |
| 56 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 57 | def __init__(self, host): |
| 58 | UDPClient.__init__(self, host, 100002, 2) |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 61 | class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient): |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 62 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 63 | def __init__(self, bcastaddr): |
| 64 | BroadcastUDPClient.__init__(self, bcastaddr, 100002, 2) |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def test(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 68 | import sys |
| 69 | if not sys.argv[1:]: |
| 70 | testbcast() |
| 71 | return |
| 72 | else: |
| 73 | host = sys.argv[1] |
| 74 | c = RnusersClient(host) |
| 75 | list = c.Names() |
| 76 | for (line, name, host, time), idle in list: |
| 77 | line = strip0(line) |
| 78 | name = strip0(name) |
| 79 | host = strip0(host) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 80 | print("%r %r %r %s %s" % (name, host, line, time, idle)) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 81 | |
| 82 | def testbcast(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 83 | c = BroadcastRnusersClient('<broadcast>') |
| 84 | def listit(list, fromaddr): |
| 85 | host, port = fromaddr |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 86 | print(host + '\t:', end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | for (line, name, host, time), idle in list: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 88 | print(strip0(name), end=' ') |
| 89 | print() |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 90 | c.set_reply_handler(listit) |
| 91 | all = c.Names() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 92 | print('Total Count:', len(all)) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 93 | |
| 94 | def strip0(s): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 95 | while s and s[-1] == '\0': s = s[:-1] |
| 96 | return s |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 97 | |
| 98 | test() |