本文整理汇总了Python中connection.Connection.stop方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.stop方法的具体用法?Python Connection.stop怎么用?Python Connection.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.stop方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: JammiNode
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import stop [as 别名]
class JammiNode(object):
def __init__(self, host, port, name, broadcasting):
self.host = host
self.port = port
self.name = name
self.conn = Connection(host, port, name)
self.conn.start()
self.subs = dict()
self.broadcasting = broadcasting
self.pub_man = pm.PublisherManager()
def run(self):
for topic, msg_type, trusted in self.broadcasting:
self.create_subscriber(topic, msg_type, trusted)
while not rospy.is_shutdown():
updates = self.conn.updates()
for v in updates.values():
self.pub_man.publish(v)
self.conn.stop()
def create_subscriber(self, topic, msg_type, trusted):
namespace, msg_name = msg_type.split("/")
mod = __import__(namespace + ".msg")
msg_cls = getattr(mod.msg, msg_name)
cb = self.create_callback(topic, msg_type, trusted)
self.subs[topic] = rospy.Subscriber(topic, msg_cls, cb, None, 1)
return self
def create_callback(self, topic, msg_type, trusted):
def callback(msg):
data = dict()
data["to"] = trusted.split(' ')
data["from"] = self.name
data["topic"] = "/{}{}".format(self.name, topic)
data["type"] = msg_type
data["stamp"] = time.time()
data["msg"] = mc.convert_ros_message_to_dictionary(msg)
self.conn.send_message(data)
return callback
示例2: CanopyClientNode
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import stop [as 别名]
class CanopyClientNode(object):
def __init__(self, host, port, name, broadcasting, private_key,
description, global_frames):
self.host = host
self.port = port
self.name = name.replace(" ", "").replace("/", "")
self.conn = dict()
self.receiver = None
self.descriptionConn = None
self.subs = dict()
self.broadcasting = broadcasting
self.private_key = private_key
self.description = description
self.global_frames = global_frames
self.pub_man = pm.PublisherManager()
self.timer = threading.Timer(0.1, self.descriptionSend)
# Creates all connections and subscribers and starts them.
# Runs a loop that checks for received messages.
def run(self):
for topic, msg_type, trusted in self.broadcasting:
if topic[0] != "/":
topic = "/" + topic
self.create_subscriber(topic, msg_type, trusted)
if topic == "/receiving":
rospy.logerr("{}: topic name 'receiving' is reserved".format(
self.name))
continue
self.conn[topic] = Connection(host, port, "{}{}".format(
self.name, topic), private_key)
self.conn[topic].start()
self.receiver = Connection(host, port, "{}{}".format(
self.name, "/receiving"), private_key)
self.descriptionConn = Connection(host, port, "{}/description".format(
self.name), private_key)
self.receiver.start()
self.descriptionConn.start()
self.timer.start()
while not rospy.is_shutdown():
#for key, conn in self.conn.iteritems():
# updates = conn.updates()
updates = self.receiver.updates()
for v in updates.values():
self.pub_man.publish(v)
for key, conn in self.conn.iteritems():
conn.stop()
self.receiver.stop()
self.timer.cancel()
self.descriptionConn.stop()
# Creates a subscriber for messages of msg_type published on topic.
def create_subscriber(self, topic, msg_type, trusted):
namespace, msg_name = msg_type.split("/")
mod = __import__(namespace + ".msg")
msg_cls = getattr(mod.msg, msg_name)
cb = self.create_callback(topic, msg_type, trusted)
self.subs[topic] = rospy.Subscriber(topic, msg_cls, cb, None, 1)
return self
# Creates a callback function for the subscribers.
# Formats the packet as a dictionary and sends it to the Connection.
def create_callback(self, topic, msg_type, trusted):
def callback(msg):
if msg._connection_header["callerid"] == rospy.get_name():
return
data = dict()
data["To"] = trusted.split(' ')
data["From"] = self.name
if topic == "/tf":
data["Topic"] = topic
else:
data["Topic"] = "/{}{}".format(self.name, topic)
data["Type"] = msg_type
data["Stamp"] = time.time()
data["Private_key"] = self.private_key
if msg_type == "tf2_msgs/TFMessage":
for t in msg.transforms:
t = self.modify_stamped_message(t)
else:
msg = self.modify_stamped_message(msg)
data["Msg"] = mc.convert_ros_message_to_dictionary(msg)
self.conn[topic].send_message(data)
return callback
def modify_stamped_message(self, message):
if hasattr(message, 'child_frame_id'):
if (message.child_frame_id.find("/") > 0 or
message.child_frame_id.count("/") > 1):
return message
if message.child_frame_id not in self.global_frames:
if message.child_frame_id[0] != "/":
message.child_frame_id = "/" + message.child_frame_id
message.child_frame_id = "{}{}".format(self.name,
message.child_frame_id)
if hasattr(message, 'header'):
if ((not hasattr(message, 'child_frame_id')) and
message.header.frame_id.find("/") > 0 and
message.header.frame_id.count("/") > 1):
return message
#.........这里部分代码省略.........
示例3: Bot
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import stop [as 别名]
class Bot(object):
def __init__(self, version = "HansBot"):
self.version = version
self.state = BotState(self)
self.connection = None
def start(self, server, nickname):
if self.connection:
LOGGER.warning("Starting the bot twice. Will disconnect old bot.")
self.stop()
self.connection = Connection(server, nickname, delegate = self.state,
version = self.version)
def join(self):
self.connection.join()
def send_message(self, user, message):
self.connection.send_message(destination = user.session, message = message)
def stop(self):
self.connection.stop()
self.connection = None
def rejected(self):
self.stop()
def connected(self):
pass
def channels(self):
return self.state.channels_by_id.values()
def users(self):
return self.state.users_by_session.values()
def get_channel_by_id(self, id):
return self.state.channels_by_id[id]
def get_user_by_id(self, id):
return self.state.users_by_id[id]
def get_user_by_name(self, name):
for u in self.state.users_by_session:
if self.state.users_by_session[u].name == name:
return self.state.users_by_session[u]
return None
def get_root(self):
return self.state.root
def is_connected(self):
return self.connection is not None
##############################################################################
### EVENTS FROM STATE
def on_text_message(self, from_user, to_users, to_channels, tree_ids,
message):
if self.state.user.session in to_users:
self.on_message_self(from_user = from_user, message = message)
if to_users:
self.on_message_users(from_user = from_user, to_users = to_users,
message = message)
if to_channels:
self.on_message_channels(from_user = from_user, to_channels = to_channels,
message = message)
if tree_ids:
self.on_message_trees(from_user = from_user, tree_ids = tree_ids,
message = message)
def on_voice_ping(self, session_id):
pass
def on_voice_talk(self, from_user, sequence, data):
pass
##############################################################################
### EVENTS
def on_message_self(self, from_user, message):
pass
def on_message_users(self, from_user, to_users, message):
pass
def on_message_channels(self, from_user, to_channels, message):
pass
def on_message_trees(self, from_user, tree_ids, message):
pass