本文整理匯總了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()