Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b637221

Browse files
committed
Full broadcast support
1 parent da164d2 commit b637221

1 file changed

Lines changed: 43 additions & 26 deletions

File tree

Demo/rpc/rnusersclient.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Remote nusers client interface
22

33
import rpc
4-
from rpc import Packer, Unpacker, TCPClient, UDPClient
4+
from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient
55

66

77
class RnusersPacker(Packer):
@@ -34,48 +34,65 @@ def unpack_utmpidlearr(self):
3434
return self.unpack_array(self.unpack_utmpidle)
3535

3636

37-
class RnusersClient(UDPClient):
37+
class PartialRnusersClient:
3838

3939
def addpackers(self):
4040
self.packer = RnusersPacker().init()
4141
self.unpacker = RnusersUnpacker().init('')
4242

43-
def init(self, host):
44-
return UDPClient.init(self, host, 100002, 2)
45-
4643
def Num(self):
47-
self.start_call(1)
48-
self.do_call()
49-
n = self.unpacker.unpack_int()
50-
self.end_call()
51-
return n
44+
return self.make_call(1, None, None, self.unpacker.unpack_int)
5245

5346
def Names(self):
54-
self.start_call(2)
55-
self.do_call()
56-
list = self.unpacker.unpack_utmpidlearr()
57-
self.end_call()
58-
return list
47+
return self.make_call(2, None, \
48+
None, self.unpacker.unpack_utmpidlearr)
5949

6050
def Allnames(self):
61-
self.start_call(3)
62-
self.do_call()
63-
list = self.unpacker.unpack_utmpidlearr()
64-
self.end_call()
65-
return list
51+
return self.make_call(3, None, \
52+
None, self.unpacker.unpack_utmpidlearr)
53+
54+
55+
class RnusersClient(PartialRnusersClient, UDPClient):
56+
57+
def init(self, host):
58+
return UDPClient.init(self, host, 100002, 2)
59+
60+
61+
class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):
62+
63+
def init(self, bcastaddr):
64+
return BroadcastUDPClient.init(self, bcastaddr, 100002, 2)
6665

6766

6867
def test():
6968
import sys
70-
host = ''
71-
if sys.argv[1:]: host = sys.argv[1]
69+
if not sys.argv[1:]:
70+
testbcast()
71+
return
72+
else:
73+
host = sys.argv[1]
7274
c = RnusersClient().init(host)
7375
list = c.Names()
74-
def strip0(s):
75-
while s and s[-1] == '\0': s = s[:-1]
76-
return s
7776
for (line, name, host, time), idle in list:
7877
line = strip0(line)
7978
name = strip0(name)
8079
host = strip0(host)
81-
print name, host, line, time, idle
80+
print `name`, `host`, `line`, time, idle
81+
82+
def testbcast():
83+
c = BroadcastRnusersClient().init('<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)
93+
94+
def strip0(s):
95+
while s and s[-1] == '\0': s = s[:-1]
96+
return s
97+
98+
test()

0 commit comments

Comments
 (0)