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): |
| 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) |
| 20 | |
| 21 | |
| 22 | class RnusersUnpacker(Unpacker): |
| 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) |
| 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 | |
| 39 | def addpackers(self): |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 40 | self.packer = RnusersPacker() |
| 41 | self.unpacker = RnusersUnpacker('') |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 43 | def Num(self): |
| 44 | return self.make_call(1, None, None, self.unpacker.unpack_int) |
| 45 | |
| 46 | def Names(self): |
| 47 | return self.make_call(2, None, \ |
| 48 | None, self.unpacker.unpack_utmpidlearr) |
| 49 | |
| 50 | def Allnames(self): |
| 51 | return self.make_call(3, None, \ |
| 52 | None, self.unpacker.unpack_utmpidlearr) |
| 53 | |
| 54 | |
| 55 | class RnusersClient(PartialRnusersClient, UDPClient): |
| 56 | |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +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 | |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +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(): |
| 68 | import sys |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 69 | if not sys.argv[1:]: |
| 70 | testbcast() |
| 71 | return |
| 72 | else: |
| 73 | host = sys.argv[1] |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 74 | c = RnusersClient(host) |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 75 | list = c.Names() |
Guido van Rossum | cdd04ca | 1992-12-20 18:14:23 +0000 | [diff] [blame] | 76 | for (line, name, host, time), idle in list: |
| 77 | line = strip0(line) |
| 78 | name = strip0(name) |
| 79 | host = strip0(host) |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 80 | print `name`, `host`, `line`, time, idle |
| 81 | |
| 82 | def testbcast(): |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 83 | c = BroadcastRnusersClient('<broadcast>') |
Guido van Rossum | b637221 | 1992-12-21 14:32:55 +0000 | [diff] [blame] | 84 | def listit(list, fromaddr): |
| 85 | host, port = fromaddr |
| 86 | print host + '\t:', |
| 87 | for (line, name, host, time), idle in list: |
| 88 | print strip0(name), |
| 89 | print |
| 90 | c.set_reply_handler(listit) |
| 91 | all = c.Names() |
| 92 | print 'Total Count:', len(all) |
| 93 | |
| 94 | def strip0(s): |
| 95 | while s and s[-1] == '\0': s = s[:-1] |
| 96 | return s |
| 97 | |
| 98 | test() |