Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 37
Текст из файла (страница 37)
Similarly, the function send message() is wrapped to the function thread send message() which lets the main thread access theconnection to the server, which is owned by the communication thread.Sharing resources between threads is a complicated matter. Often, itis a good idea to avoid using threads in the first place because of thesedifficulties.
If your application really needs them, the best idea is to re-usedesigns that have been proven to work before.To re-use this communication infrastructure, namely functions readmessage(), send message(), connect() and communicate(),in another application, you have to replace the function handlemessage() with an application-specific handler.Example 83 shows the message handler function handle message()for the instant messenger.Example 83: Instant messenger (2/3)def show_photo(jpeg_data):global imgf = file("E:\\Images\\msg.jpg", "w")f.write(jpeg_data)f.close()img = graphics.Image.open("E:\\Images\\msg.jpg")def handle_message(msg):global text, notePEER-TO-PEER NETWORKING191if "photo" in msg:show_photo(msg["photo"])text = {"from": msg["from"], "txt": ""}elif "txt" in msg:text = msgelif "note" in msg:note = msg["note"]redraw(None)def send_photo():handle_message({"note": u"Taking photo..."})dst = appuifw.query(u"To", "text")img = camera.take_photo(size = (640, 480))img = img.resize((320, 240))img.save("E:\\Images\\temp.jpg")jpeg = file("E:\\Images\\temp.jpg").read()handle_message({"note": u"Sending photo..."})thread_send_message({"!dst": dst, "photo": jpeg, "from": me})def send_text():resp = appuifw.multi_query(u"To", u"Message")if resp:dst, txt = respthread_send_message({"!dst": dst, "txt": txt, "from": me})This part of the application contains many functions related to graphicsand the user interface that are already familiar to you from previousexamples.
The application has to handle four tasks:•The user may take and send a photo through the ‘Send photo’ menuitem. This task is handled by the function send photo().•The user may send a text message through the ‘Send text’ menu item.This task is handled by the function send text().•The user may receive a photo from another user. The incomingmessage is first handled in handle message() which calls thefunction show photo() to prepare the received photo for showing.•The user may receive a text from another user. The incoming messageis first handled in handle message(), as above, and the functionredraw() shows the received message on the canvas.Note that both the send functions use the function thread sendmessage() to pass a new message to the networking thread.
Thisfunction is a wrapper for the function send message() which actuallysends the message to the gateway. As required by our JSON gateway, the"!dst" key is used to define the recipient’s name in a message.Example 84: Instant messenger (3/3)def quit():global keep_talking, to_server, conn192MOBILE NETWORKINGkeep_talking = Falsethread_send_message({"!close": True})app_lock.signal()def redraw(rect):RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)canvas.clear((255, 255, 255))if img:canvas.blit(img, scale = 1)if note:canvas.text((10, canvas.size[1] - 30), note,\fill = RED, font = "title")if text:canvas.text((10, 80), u"From: %s" % text["from"],\fill = GREEN, font = "title")canvas.text((10, 110), unicode(text["txt"]),\fill = BLUE, font = "title")img = text = note = Nonekeep_talking = Truethread_handle_message = e32.ao_callgate(handle_message)appuifw.app.exit_key_handler = quitappuifw.app.title = u"Instant Messenger"appuifw.app.menu = [(u"Send Photo", send_photo),\(u"Send Text", send_text)]canvas = appuifw.Canvas(redraw_callback = redraw)appuifw.app.body = canvasme = appuifw.query(u"Login name", "text")handle_message({"note": u"Contacting server..."})if me:app_lock = e32.Ao_lock()thread.start_new_thread(communicate, ())if keep_talking:app_lock.wait()In this last part of the application, there are two new functions, whichhave not been used earlier in this book.
The function e32.ao callgate() was discussed above. The function thread.start newthread() is used to start the function communicate() in anotherthread of execution.This means that the while loop in the function communicate(),which is responsible for receiving incoming messages, keeps on going inthe background although events related to the user interface are handledin the main thread.You can test this application by sending messages to yourself.
Just login with an arbitrary name, for instance ‘Matt’, choose ‘Send Photo’ in theapplication menu and as the recipient, write ‘Matt’. Almost immediately,after the message has made a round-trip to the JSON gateway, the photoshould appear on your phone’s screen.USING A PHONE AS A WEB SERVICE1938.7 Using a Phone as a Web ServiceAccessing the phone’s resources by way of the web is a ‘bleedingedge’ approach that might be used as a springboard for many kinds ofunprecedented applications.We have already seen some examples where a PyS60 program fetchessome information from the web.
Many more examples along these linesfollow in Chapter 9. In this section, however, we consider the oppositecase: the phone could serve information to the web (see Figure 8.7).Figure 8.7Phone providing a web serviceThink about various possibilities: for example, you could take a photowith someone else’s camera phone from a web page, you could requestthe phone’s holder in real time to take a photo for you or your mobilewebsite could list people around you at that moment, based on Bluetoothscanning.Apache, the world’s most popular web server, has been ported to theS60 platform so one can experiment with scenarios like this.
You coulduse Mobile Web Server (MWS) to implement the above scenarios. You caneven use PyS60 to write extensions and request handlers for the server.You can find the MWS home page at http://opensource.nokia.com/projects/mobile-web-server/.Here, however, we show you how to serve resources of your mobilephone to the web in pure PyS60. Compared to MWS, this approach ismore flexible since you can control every part of the server. On the other194MOBILE NETWORKINGhand, in some settings this approach may require much more work, sinceyou have to control every part of the server. Anyway, it is good to knowthat the basic task is not really complex, as Example 86 shows.The ‘Phone as a web server’ scenario is not different from theother phone-server scenarios which have been discussed in Sections 8.5and 8.6.
Generally speaking, one cannot connect to the phone directlyfrom the Internet. There are two options: either use a web proxy, to whichthe phone pushes information or have a gateway which routes HTTPrequests to the phone, in a similar way to the JSON gateway.We give an example of the proxy approach. A proxy server receivesHTTP requests from the web and saves them until the phone polls theserver for new requests. The proxy caches responses sent by the phone,so the web clients get a response without any delay.Since web clients are not directly in contact with the phone, thisapproach scales well.
Also, the proxy can handle a cluster of phones inthe background. Requests can be directed to a specific phone based onthe URL or the proxy can balance requests between the phones.It is even possible to construct an on-demand web service, where onlythose phones which have access to a requested resource, for instance,those in a specific location, respond to the requests. Although this mightsound extremely sophisticated, you have already learnt enough to do itby yourself, based on Example 85 and positioning techniques presentedin Section 6.4.The proxy is based on the simple HTTP server that is described inSection 8.4.2.
As before, you should be able to port this example to anyweb framework of your choice with relative ease, instead of using ourtoy HTTP server. The web server functions are elegantly simple as all theprocessing is done on the phone side.Example 85: Phone–web proxydef init_server():global phone_req, cachephone_req = {}cache = {}def process_json(msg):global cachecache = msgreturn phone_reqdef process_get(path, query):phone_req[path] = Trueif path in cache:return cache[path]return "text/plain",\"Your request is being processed."\"Reload the page after a while."USING A PHONE AS A WEB SERVICE195init_server()httpd = Server((’’, 9000), Handler)httpd.serve_forever()The phone both delivers responses to previous requests and fetches newones using JSON messages that are handled by the process json()function.
Requests by the web browser are handled in the processget() function. If the requested resource is not available in the cache, abrief message is returned that informs the user about this.You should combine this example with the basic HTTP server inExample 75 to make the full proxy server. These lines should come afterthe main server.On the phone side, we make some resources of the phone available tothe proxy, namely the camera, screenshots and the battery indicator. Inother words, you can use the phone’s camera and see its current screenand the battery level on the web browser! It is really straightforward toadd more resources to be shared by the phone.If this sounds unbelievable to you, just see Figure 8.8.
The screenshotshowing on the browser is not a static image, but is generated by thisexample on the fly as requested by the web browser. Amusingly, thescreenshot captured some log lines printed out on the console whichwere caused by the screenshot request itself.Figure 8.8The phone’s screen as viewed in a web browser196MOBILE NETWORKINGExample 86: Phone–web serverimport e32, json, camera, graphics, sysinfo, urllibURL = "http://192.168.0.2:9000"def json_request(req):enc = json.write(req)return json.read(urllib.urlopen(URL, enc).read())def take_photo():img = camera.take_photo(size = (640, 480))img.save("E:\\Images\\temp.jpg")return file("E:\\Images\\temp.jpg").read()def screenshot():img = graphics.screenshot()img.save("E:\\Images\\temp.jpg")return file("E:\\Images\\temp.jpg").read()go_on = Truemsg = {}print "Web service starts..."while go_on:ret = {}for path in json_request(msg):print "Requesting", pathif path == "/camera.jpg":ret[path] = ("image/jpeg", take_photo())elif path == "/screenshot.jpg":ret[path] = ("image/jpeg", screenshot())elif path == "/battery":ret[path] = ("text/plain",\"Current battery level is %d" %sysinfo.battery())elif path == "/exit":go_on = Falseelse:ret[path] = ("text/plain", "known resource")msg = rete32.ao_sleep(5)print "Bye!"Again, make sure that the constant URL refers to your actual server.These resources are mapped to logical URLs, ‘/camera.jpg’, ‘/screenshot.jpg’ and ‘/battery’ – see the navigation bar in Figure 8.8 to see oneof the URLs in practice.