本文整理汇总了Python中bus.Bus.start方法的典型用法代码示例。如果您正苦于以下问题:Python Bus.start方法的具体用法?Python Bus.start怎么用?Python Bus.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bus.Bus
的用法示例。
在下文中一共展示了Bus.start方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Peer
# 需要导入模块: from bus import Bus [as 别名]
# 或者: from bus.Bus import start [as 别名]
class Peer(object):
def __init__(self, share_path, host=None, port=None):
self.host, self.port = (host or ALL), (port or PORT)
self.db = dal.DB("sqlite://")
self.db.define_table("config", dal.Field("key", key=True), dal.Field("value"))
if "peerid" not in self.db.config:
self.db.config["peerid"] = hex(random.getrandbits(48))[2:14]
self.db.define_table(
"peers",
dal.Field("peerid"),
dal.Field(
"address",
serialize=lambda x: "%s:%i" % x,
convert=lambda x: tuple(f(a) for f, a in zip((str, int), x.rsplit(":", 1))),
),
dal.Field("ignore", default=False),
)
self.db.define_table("resources", dal.Field("path", key=True), dal.Field("age", int), dal.Field("real_path"))
if os.path.isdir(share_path) and share_path[-1] != "/":
share_path += "/"
for path in find(os.path.abspath(share_path)):
short_path = path[len(share_path) :]
print path, short_path
self.db.resources.insert(path=short_path, age=mod_time(os.stat(path)), real_path=path)
self.bus = Bus()
self.bus.connect(MessageType.HeardFromPeer, self.introduce)
self.bus.connect(MessageType.Request, self.notify)
self.bus.connect(MessageType.RemoteUpdate, self.remote_update)
self.public = Broadcaster(self.bus, self.db.config["peerid"], (self.host, self.port))
self.private = Whisperer(self.bus, (self.host, self.port))
def start(self):
self.bus.start()
self.public.start()
self.private.start()
def announce(self):
self.broadcaster.send("Hello?")
def remote_update(self, message):
if self.filter(message.path):
self.private.retrieve(message.address, message.path)
def introduce(self, message):
print message
self.db.peers.insert(**message)
def notify(self, message):
print message
self.public.send(self.db.resources[message.path].age)
def stop(self):
self.private.stop()
self.public.stop()
self.bus.stop()