network_status.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 import re
00003 import rospy
00004 from std_msgs.msg import Float32
00005 from collections import deque
00006 
00007 class NetworkStatus():
00008     def __init__(self):
00009         rospy.init_node('network')
00010         self.match_face = re.compile('(.+):(.*)')
00011         self.hz = 100
00012 
00013         self.init_publisher()
00014         rospy.Timer(rospy.Duration(1.0/self.hz), self.publish)
00015         rospy.spin()
00016 
00017     def init_publisher(self):
00018         faces = self.read_net_file()
00019         self.faces_map = {}
00020         for face in faces:
00021             pub_transmit = rospy.Publisher(face[0] + '/transmit', Float32)
00022             pub_receive = rospy.Publisher(face[0] + '/receive', Float32)
00023             queue_transmit = deque()
00024             queue_reveice = deque()
00025 
00026             for i in range(self.hz):
00027                 queue_transmit.append(-1)
00028                 queue_reveice.append(-1)
00029 
00030             self.faces_map[face[0]] = {}
00031             self.faces_map[face[0]]["transmit"] = {"publisher": pub_transmit, "value": queue_transmit}
00032             self.faces_map[face[0]]["receive"] = {"publisher": pub_receive, "value": queue_reveice}
00033 
00034     def publish(self, event):
00035         faces = self.read_net_file()
00036         for face in faces:
00037             name = face[0]
00038 
00039             if name in self.faces_map:
00040                 self.faces_map[name]["transmit"]["value"].append(int(face[1]))
00041                 self.faces_map[name]["receive"]["value"].append(int(face[2]))
00042 
00043                 msg_transmit = Float32((int(face[1]) - self.faces_map[name]["transmit"]["value"].popleft()) * 8)
00044                 msg_receive = Float32((int(face[2]) - self.faces_map[name]["receive"]["value"].popleft()) * 8)
00045 
00046                 self.faces_map[name]["transmit"]["publisher"].publish(msg_transmit)
00047                 self.faces_map[name]["receive"]["publisher"].publish(msg_receive)
00048 
00049     def read_net_file(self):
00050         with open("/proc/net/dev") as f:
00051             status =  f.readlines()
00052         ret = []
00053         for s in status:
00054             match = self.match_face.match(s)
00055             if match:
00056                 nums = match.group(2).split()
00057                 ret.append([match.group(1).strip(), nums[8], nums[0]])
00058         return ret
00059 
00060 if __name__ == '__main__':
00061     NetworkStatus()


jsk_network_tools
Author(s): Yusuke Furuta
autogenerated on Mon Oct 6 2014 11:03:45