mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
Full broadcast support
This commit is contained in:
parent
da164d2bff
commit
b637221d93
1 changed files with 43 additions and 26 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
# Remote nusers client interface
|
# Remote nusers client interface
|
||||||
|
|
||||||
import rpc
|
import rpc
|
||||||
from rpc import Packer, Unpacker, TCPClient, UDPClient
|
from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient
|
||||||
|
|
||||||
|
|
||||||
class RnusersPacker(Packer):
|
class RnusersPacker(Packer):
|
||||||
|
|
@ -34,48 +34,65 @@ class RnusersUnpacker(Unpacker):
|
||||||
return self.unpack_array(self.unpack_utmpidle)
|
return self.unpack_array(self.unpack_utmpidle)
|
||||||
|
|
||||||
|
|
||||||
class RnusersClient(UDPClient):
|
class PartialRnusersClient:
|
||||||
|
|
||||||
def addpackers(self):
|
def addpackers(self):
|
||||||
self.packer = RnusersPacker().init()
|
self.packer = RnusersPacker().init()
|
||||||
self.unpacker = RnusersUnpacker().init('')
|
self.unpacker = RnusersUnpacker().init('')
|
||||||
|
|
||||||
|
def Num(self):
|
||||||
|
return self.make_call(1, None, None, self.unpacker.unpack_int)
|
||||||
|
|
||||||
|
def Names(self):
|
||||||
|
return self.make_call(2, None, \
|
||||||
|
None, self.unpacker.unpack_utmpidlearr)
|
||||||
|
|
||||||
|
def Allnames(self):
|
||||||
|
return self.make_call(3, None, \
|
||||||
|
None, self.unpacker.unpack_utmpidlearr)
|
||||||
|
|
||||||
|
|
||||||
|
class RnusersClient(PartialRnusersClient, UDPClient):
|
||||||
|
|
||||||
def init(self, host):
|
def init(self, host):
|
||||||
return UDPClient.init(self, host, 100002, 2)
|
return UDPClient.init(self, host, 100002, 2)
|
||||||
|
|
||||||
def Num(self):
|
|
||||||
self.start_call(1)
|
|
||||||
self.do_call()
|
|
||||||
n = self.unpacker.unpack_int()
|
|
||||||
self.end_call()
|
|
||||||
return n
|
|
||||||
|
|
||||||
def Names(self):
|
class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):
|
||||||
self.start_call(2)
|
|
||||||
self.do_call()
|
|
||||||
list = self.unpacker.unpack_utmpidlearr()
|
|
||||||
self.end_call()
|
|
||||||
return list
|
|
||||||
|
|
||||||
def Allnames(self):
|
def init(self, bcastaddr):
|
||||||
self.start_call(3)
|
return BroadcastUDPClient.init(self, bcastaddr, 100002, 2)
|
||||||
self.do_call()
|
|
||||||
list = self.unpacker.unpack_utmpidlearr()
|
|
||||||
self.end_call()
|
|
||||||
return list
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
import sys
|
import sys
|
||||||
host = ''
|
if not sys.argv[1:]:
|
||||||
if sys.argv[1:]: host = sys.argv[1]
|
testbcast()
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
host = sys.argv[1]
|
||||||
c = RnusersClient().init(host)
|
c = RnusersClient().init(host)
|
||||||
list = c.Names()
|
list = c.Names()
|
||||||
def strip0(s):
|
|
||||||
while s and s[-1] == '\0': s = s[:-1]
|
|
||||||
return s
|
|
||||||
for (line, name, host, time), idle in list:
|
for (line, name, host, time), idle in list:
|
||||||
line = strip0(line)
|
line = strip0(line)
|
||||||
name = strip0(name)
|
name = strip0(name)
|
||||||
host = strip0(host)
|
host = strip0(host)
|
||||||
print name, host, line, time, idle
|
print `name`, `host`, `line`, time, idle
|
||||||
|
|
||||||
|
def testbcast():
|
||||||
|
c = BroadcastRnusersClient().init('<broadcast>')
|
||||||
|
def listit(list, fromaddr):
|
||||||
|
host, port = fromaddr
|
||||||
|
print host + '\t:',
|
||||||
|
for (line, name, host, time), idle in list:
|
||||||
|
print strip0(name),
|
||||||
|
print
|
||||||
|
c.set_reply_handler(listit)
|
||||||
|
all = c.Names()
|
||||||
|
print 'Total Count:', len(all)
|
||||||
|
|
||||||
|
def strip0(s):
|
||||||
|
while s and s[-1] == '\0': s = s[:-1]
|
||||||
|
return s
|
||||||
|
|
||||||
|
test()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue