Some platforms have rl_completion_append_character but not rl_completion_suppress_append.
[python.git] / Demo / rpc / rnusersclient.py
blob1f3a882c5a115542c68af78de7f95b92bb878422
1 # Remote nusers client interface
3 import rpc
4 from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient
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)
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)
37 class PartialRnusersClient:
39 def addpackers(self):
40 self.packer = RnusersPacker()
41 self.unpacker = RnusersUnpacker('')
43 def Num(self):
44 return self.make_call(1, None, None, self.unpacker.unpack_int)
46 def Names(self):
47 return self.make_call(2, None, \
48 None, self.unpacker.unpack_utmpidlearr)
50 def Allnames(self):
51 return self.make_call(3, None, \
52 None, self.unpacker.unpack_utmpidlearr)
55 class RnusersClient(PartialRnusersClient, UDPClient):
57 def __init__(self, host):
58 UDPClient.__init__(self, host, 100002, 2)
61 class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):
63 def __init__(self, bcastaddr):
64 BroadcastUDPClient.__init__(self, bcastaddr, 100002, 2)
67 def test():
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)
80 print "%r %r %r %s %s" % (name, host, line, time, idle)
82 def testbcast():
83 c = BroadcastRnusersClient('<broadcast>')
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)
94 def strip0(s):
95 while s and s[-1] == '\0': s = s[:-1]
96 return s
98 test()