本文整理汇总了Python中dispatcher.Dispatcher.register方法的典型用法代码示例。如果您正苦于以下问题:Python Dispatcher.register方法的具体用法?Python Dispatcher.register怎么用?Python Dispatcher.register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dispatcher.Dispatcher
的用法示例。
在下文中一共展示了Dispatcher.register方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: join
# 需要导入模块: from dispatcher import Dispatcher [as 别名]
# 或者: from dispatcher.Dispatcher import register [as 别名]
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
import settings
import irc
import log
from dispatcher import Dispatcher
def join(c,m):
for chan in settings.default_channels:
c.write("JOIN " + chan)
if __name__=="__main__":
connection = irc.Connection(
address=(settings.SERVER, settings.PORT),
nick=settings.NICK,
ident=settings.IDENT,
realname=settings.REALNAME)
connection.connect()
dispatcher = Dispatcher(connection)
dispatcher.register('003', join)
while True:
msg = irc.Message(connection.read())
dispatcher.dispatch(msg)
示例2: ServerTest
# 需要导入模块: from dispatcher import Dispatcher [as 别名]
# 或者: from dispatcher.Dispatcher import register [as 别名]
class ServerTest(unittest.TestCase):
def setUp(self):
self._head1 = MsgCSLogin('test', 0)
self._head2 = MsgCSMoveto(3, 5)
self._dispatcher = Dispatcher()
self._dispatcher.register(100, TestService())
self.count = 0
def tearDown(self):
self._head1 = None
self._head2 = None
self._dispatcher = None
self.count = 0
def addCount(self):
self.count += 1
def test_Parser(self):
# test header
data = self._head1.marshal()
print "hello", data
head = MsgCSLogin().unmarshal(data)
self.assertEqual(self._head1.name, head.name)
self.assertEqual(self._head1.icon, head.icon)
data = self._head2.marshal()
print "world", data
head = MsgCSMoveto().unmarshal(data)
self.assertEqual(self._head2.x, head.x)
self.assertEqual(self._head2.y, head.y)
# test dispatcher
msg = MsgService()
msg.sid = 100
msg.cid = 10
self.assertEqual(self._dispatcher.dispatch(msg, 'client1'), 'client1')
msg.cid = 20
self.assertEqual(self._dispatcher.dispatch(msg, 'client2'), 'client2')
# test network
host = SimpleHost()
host.startup(2000)
sock = NetStream()
last = time.time()
sock.connect('127.0.0.1', 2000)
stat = 0
last = time.time()
sock.nodelay(1)
while 1:
time.sleep(0.1)
host.process()
sock.process()
if stat == 0:
if sock.status() == conf.NET_STATE_ESTABLISHED:
stat = 1
data = cPickle.dumps((stat, 'Hello, world !!'), -1)
sock.send(data)
last = time.time()
elif stat == 1:
if time.time() - last >= 2.0:
stat = 2
data = cPickle.dumps((stat, 'exit'), -1)
sock.send(data)
event, wparam, data = host.read()
if event < 0:
continue
if event == conf.NET_CONNECTION_DATA:
client_stat, message = cPickle.loads(data)
host.sendClient(wparam, 'RE: ' + message)
if client_stat == 1:
self.assertEqual(message, 'Hello, world !!')
elif client_stat == 2:
self.assertEqual(message, 'exit')
host.closeClient(wparam)
host.shutdown()
break
# test timer
TimerManager.addRepeatTimer(0.15, self.addCount)
last = time.time()
while 1:
time.sleep(0.01)
TimerManager.scheduler()
if time.time() - last > 1.0:
break
self.assertEqual(self.count, 6)
return