users_online.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 PKG = 'webui' # this package name
4 NAME = 'users_online'
5 
6 import roslib; roslib.load_manifest(PKG)
7 
8 import os
9 import sys
10 import time
11 import urllib2
12 import subprocess
13 
14 import rospy
15 from std_msgs.msg import String
16 from std_srvs.srv import Empty,EmptyResponse
17 
18 from optparse import OptionParser
19 from webui import config
20 
21 users = {}
22 active_user = None
23 publisher = None
24 subscriber = None
25 
26 active_user_file = config.ACTIVE_USER_FILE #"/var/ros/active_user.dat"
27 
28 INACTIVITY_LOGOUT = 120 # seconds after which to auto-logout a non-present user
29 
31  active_user = None
32  mod_time = 0
33  try:
34  user_file = open(active_user_file, "r")
35  active_user = user_file.read().strip()
36  user_file.close()
37  mod_time = os.stat(active_user_file).st_mtime
38  except:
39  pass
40  return active_user, mod_time
41 
42 def listener():
43  while not rospy.is_shutdown():
45  time.sleep(5)
46 
47 def callback(msg):
48  rospy.logdebug("got user presence: %s" % msg.data)
49  now = time.time()
50  users[msg.data] = now
51 
53  rospy.loginfo("removing active user")
54 
55  if config.get_robot_type().startswith("texas"):
56  if os.path.exists(config.VALID_USER_COOKIE_FILE):
57  cookie = open(config.VALID_USER_COOKIE_FILE).read()
58 
59  req = urllib2.Request(config.gLobby + "/lobby/lobby/disconnect.py?robot_name=" + config.get_robot_name())
60  req.headers['Cookie'] = cookie
61 
62  try:
63  response = urllib2.urlopen(req)
64  except Exception, e:
65  rospy.logwarn("error contacting lobby: %s" % e)
66 
67  os.remove(config.VALID_USER_COOKIE_FILE)
68 
69  if os.path.exists(active_user_file):
70  os.remove(active_user_file)
71 
72 
73 
75  # build list of current users online
76  active_user, mod_time = get_active_user()
77  online_users = []
78 
79  # if users == {} and active_user and time.time() - mod_time > INACTIVITY_LOGOUT:
80  # we should remove the active user
81  # remove_active_user()
82  # active_user = ""
83 
84  for user in users.keys():
85  last_ping = users[user]
86  if user == active_user:
87  # the active user file may have been updated before presence was published
88  last_ping = max(last_ping, mod_time)
89  rospy.logdebug("active user %s being considered; now: %f, last_ping: %f, mod_time: %f" % (user, time.time(), last_ping, mod_time))
90  if time.time() - last_ping < INACTIVITY_LOGOUT:
91  online_users.append(user)
92  else:
93  if user == active_user:
94  # we should remove the active user
96  active_user = ""
97 
98  online_users.sort()
99  out = String()
100  out.data = "%s:%s" % (','.join(online_users), active_user)
101  if active_user and online_users != []:
102  publisher.publish(out)
103  rospy.logdebug("publishing online users: %s" % out)
104 
105 if __name__ == '__main__':
106  try:
107  rospy.init_node(NAME, anonymous=True)
108  publisher = rospy.Publisher("users_online", String)
109  subscriber = rospy.Subscriber("presence", String, callback)
110  listener()
111  except KeyboardInterrupt, e:
112  pass
113  print "exiting"
114 
def remove_active_user()
Definition: users_online.py:52
def listener()
Definition: users_online.py:42
def publish_users()
Definition: users_online.py:74
def get_active_user()
Definition: users_online.py:30
def callback(msg)
Definition: users_online.py:47


webui
Author(s): Scott Hassan
autogenerated on Mon Jun 10 2019 15:51:24