Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
00019 from os import path
00020 import subprocess
00021 import sys
00022 import threading
00023 import time
00024
00025 import rospy
00026
00027 default_img_path = '../res/pictures/'
00028
00029 class MyHandler(BaseHTTPRequestHandler):
00030 def do_GET(self):
00031 global default_img_path
00032 print "\nIncoming request!"
00033 try:
00034 spath = self.path[1:]
00035 if spath.endswith('.jpg') or spath.endswith('.jpeg'):
00036 self.send_response(200)
00037 self.send_header('Content-type','image/jpg')
00038 self.end_headers()
00039 fn = path.abspath(default_img_path+spath)
00040 with open(fn, 'rb') as f:
00041 self.wfile.write(f.read())
00042 return
00043
00044 elif spath.endswith('.png'):
00045 self.send_response(200)
00046 self.send_header('Content-type','image/png')
00047 self.end_headers()
00048 fn = path.abspath(default_img_path+spath)
00049 print 'fn: ', fn
00050 with open(fn, 'rb') as f:
00051 self.wfile.write(f.read())
00052 return
00053
00054 except IOError:
00055 self.send_error(404, 'File Not Found: %s' % self.path)
00056
00057 class ResourceServer(threading.Thread):
00058 def __init__(self):
00059 threading.Thread.__init__(self)
00060 self.server = None
00061 self.daemon = True
00062 self.ns_global_prefix="/android/resource_server"
00063 if rospy.has_param(self.ns_global_prefix + "/default_img_path"):
00064 global default_img_path
00065 default_img_path = rospy.get_param(self.ns_global_prefix + "/default_img_path")
00066 if not default_img_path.endswith("/"):
00067 default_img_path = default_img_path + "/"
00068
00069 def run(self):
00070 self.server = HTTPServer(('', 44644), MyHandler)
00071 print '\nStarted Android resource server on port 44644'
00072 self.server.serve_forever()
00073
00074 def close(self):
00075 self.server.shutdown()
00076 self.server.socket.close()
00077
00078 def resource_server_main():
00079 rospy.init_node('android_resource_server')
00080 resServer = ResourceServer()
00081 resServer.start()
00082 rospy.spin()
00083 resServer.close()