Go to the documentation of this file.00001
00002
00003 PKG = 'webui'
00004 NAME = 'users_online'
00005
00006 import roslib; roslib.load_manifest(PKG)
00007
00008 import os
00009 import sys
00010 import time
00011 import urllib2
00012 import subprocess
00013
00014 import rospy
00015 from std_msgs.msg import String
00016 from std_srvs.srv import Empty,EmptyResponse
00017
00018 from optparse import OptionParser
00019 from webui import config
00020
00021 users = {}
00022 active_user = None
00023 publisher = None
00024 subscriber = None
00025
00026 active_user_file = config.ACTIVE_USER_FILE
00027
00028 INACTIVITY_LOGOUT = 120
00029
00030 def get_active_user():
00031 active_user = None
00032 mod_time = 0
00033 try:
00034 user_file = open(active_user_file, "r")
00035 active_user = user_file.read().strip()
00036 user_file.close()
00037 mod_time = os.stat(active_user_file).st_mtime
00038 except:
00039 pass
00040 return active_user, mod_time
00041
00042 def listener():
00043 while not rospy.is_shutdown():
00044 publish_users()
00045 time.sleep(5)
00046
00047 def callback(msg):
00048 rospy.logdebug("got user presence: %s" % msg.data)
00049 now = time.time()
00050 users[msg.data] = now
00051
00052 def remove_active_user():
00053 rospy.loginfo("removing active user")
00054
00055 if config.get_robot_type().startswith("texas"):
00056 if os.path.exists(config.VALID_USER_COOKIE_FILE):
00057 cookie = open(config.VALID_USER_COOKIE_FILE).read()
00058
00059 req = urllib2.Request(config.gLobby + "/lobby/lobby/disconnect.py?robot_name=" + config.get_robot_name())
00060 req.headers['Cookie'] = cookie
00061
00062 try:
00063 response = urllib2.urlopen(req)
00064 except Exception, e:
00065 rospy.logwarn("error contacting lobby: %s" % e)
00066
00067 os.remove(config.VALID_USER_COOKIE_FILE)
00068
00069 if os.path.exists(active_user_file):
00070 os.remove(active_user_file)
00071
00072
00073
00074 def publish_users():
00075
00076 active_user, mod_time = get_active_user()
00077 online_users = []
00078
00079
00080
00081
00082
00083
00084 for user in users.keys():
00085 last_ping = users[user]
00086 if user == active_user:
00087
00088 last_ping = max(last_ping, mod_time)
00089 rospy.logdebug("active user %s being considered; now: %f, last_ping: %f, mod_time: %f" % (user, time.time(), last_ping, mod_time))
00090 if time.time() - last_ping < INACTIVITY_LOGOUT:
00091 online_users.append(user)
00092 else:
00093 if user == active_user:
00094
00095 remove_active_user()
00096 active_user = ""
00097
00098 online_users.sort()
00099 out = String()
00100 out.data = "%s:%s" % (','.join(online_users), active_user)
00101 if active_user and online_users != []:
00102 publisher.publish(out)
00103 rospy.logdebug("publishing online users: %s" % out)
00104
00105 if __name__ == '__main__':
00106 try:
00107 rospy.init_node(NAME, anonymous=True)
00108 publisher = rospy.Publisher("users_online", String)
00109 subscriber = rospy.Subscriber("presence", String, callback)
00110 listener()
00111 except KeyboardInterrupt, e:
00112 pass
00113 print "exiting"
00114