Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20537
  • 博文数量: 27
  • 博客积分: 665
  • 博客等级: 上士
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-28 10:34
文章分类
文章存档

2011年(27)

我的朋友
最近访客

分类: 嵌入式

2011-05-23 17:34:27

针对手持设备的云计算和软件开发是两项非常热门的技术,并被越来越多地结合起来用于创建混合解决方案。在本文中,了解如何连接 Google App Engine(Google 的云计算功能)和 iPhone(Apple 的移动平台),以及如何通过连接到 App Engine 云并缓存应用程序数据以备离线使用,从而利用开源库 TouchEngine 动态控制 iPhone 上的应用程序数据。

过去几年出现了很多创新技术,2008 年对技术而言是不同凡响的一年。两项最让人兴奋的创新是云计算和移动应用程序开发。在本文中,我们将探究一种通信方法,这种方法能利用这两个技术来实现协作开发人员的梦想。在本文中,我们将使用 Google App Engine(Google 的云计算平台)和 iPhone(Apple 的移动平台)来开发一个能同步 “云” 数据的应用程序。

我们将利用一种简单的方法来从 App Engine 拉出数据放到 iPhone 上; 这种方法需要大量使用 python 和 App Engine。使用 RSS、ATOM 或 REST 将数据连锁到 iPhone 的常规方法非常简单,但是必须要编写一个解析器。更简单的一种做法是使用 XML 属性列表或 plist。根据属性列表的手册页面(参见 参考资料):“属性列表使用几个核心基础类型将数据组织成指定的值和值的列表,这些类型包括 CFString、CFNumber、CFBoolean、CFDate、CFData、CFArray 和 CFDictionary。借助这些类型,您就能够生成结构良好、可传输、可存储和可访问的数据,并且还尽可能提高了效率。”

plist 消除了在 iPhone 上解析 XML 的烦扰,因为这些 plist 是 XML 文件格式的,Cocoa Touch 可以很容易将其解析并转变成有意义的对象。在 App Engine 上使用 Python 内的 plist 库,不用费什么力气就能将任意一个简单 Python 库对象发送给 iPhone,但前提是 Python 库内的数据类型是 plist 允许的。本文展示了使用 TouchEngine 开源库开发应用程序以便查看莎士比亚的十四行诗。要获得 Google Code 项目的链接,请参见 参考资料

首先,让我们先来看看有关 iPhone SDK 和 Google App Engine 的背景信息。

Native iPhone SDK 可通过 Objective-C 语言得到。它非常类似于 Mac OS X® 上的 Cocoa 编程,包括了能充分利用 iPhone 独特特性的一些 API,比如 GPS、触摸屏(multi-touch)、加速器(accelerometer)以及屏幕键盘。将来的功能还将包括对通知自动推入(push notification)等技术的支持。有关 iPhone Native SDK 的更多信息,请参见 参考资料

对于移动应用程序开发人员而言,iPhone 提供了丰富的开发环境。直到最近,Objective-C 对 很多开发人员而言仍旧是一种相当深奥的语言,因为它只用于 NeXT 和 Apple,但是现在通过 Cocoa Touch SDK,它的支持者开始多了起来。借助 iPhone,Objective-C 更是成为了全部新一代移动应用程序开发人员的前沿和中心。

有了 Amazon 的 S3 存储和 EC2 弹性计算服务,云计算在可视化方面得到了很大的推进。Google App Engine 是基于服务的云计算市场的一个新生力量。Google App Engine 为著名的 Google 可伸缩数据中心提供了一个 Python 语言的 API(将来会出现其他语言的版本)。这是一个极大的变革,它让软件开发人员能够从管理应用程序伸缩性的固有复杂性中解脱出来,让他们能将精力集中于应用程序的编写。

我们先来看看如何从 Google App Engine 生成 plist 文件,之后,您会通过 iPhone Cocoa Touch SDK 在 iPhone 上使用该文件。由于 App Engine 起初是免费的,所以它成为了移动应用程序开发人员的一种有趣的原型化方法。此外,此 API 是 Python 版本,而该语言享有开发迅速的美誉;而且它还是一种解释效率很高的语言。通过 App Engine 和 Python 将 iPhone 应用程序的繁重任务以及数据存储外包给 “云功能”,是一种非常有益的做法。

要跟随本文进行操作,需要下载 App Engine SDK(参见 参考资料 以获得最新版本)。有了 App Engine,很容易就能让一个 protype 在几分钟内工作起来。请注意,您也可以从本文附带的源代码下载此示例。

为了将 plist 文件提供给 iPhone 应用程序使用,只需将 App Engine project 目录内的 plistlib.py 包括进来,稍微修改一下 main.py 脚本,再包括进 sonnet.py。Sonnet.py 是一个 Python 源文件,其中的一个目录包含所有莎士比亚十四行诗的文本。清单 1 所示的就是这个 main.py 文件。



#!/usr/bin/env python #Python sonnet maker import wsgiref.handlers from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app #external imports import sonnet import plistlib class MainHandler(webapp.RequestHandler): """Returns sonnets dictionary as a converted plist""" def get(self): plist = plistlib.writeplistToString(sonnet.verses) self.response.out.write(plist) def main(): application = webapp.WSGIApplication([('/plists/sonnets', MainHandler), ], debug=True) run_wsgi_app(application) if __name__ == '__main__': main()

 

上面这个代码片段将包含莎士比亚十四行诗的字典的内容转变成一个 XML plist,并将其提供给任何请求此 /plists/sonnets URL 的客户机。不管您相信与否,这就是我们的这个 Google App Engine 应用程序的主体。清单 2 给出了 sonnet.py 的一小部分。



verses={"verses":[["I","""FROM fairest creatures we desire increase, That thereby beauty's rose might never die, But as the riper should by time decease, His tender heir might bear his memory: But thou, contracted to thine own bright eyes, Feed'st thy light'st flame with self-substantial fuel, Making a famine where abundance lies, Thyself thy foe, to thy sweet self too cruel. Thou that art now the world's fresh ornament And only herald to the gaudy spring, Within thine own bud buriest thy content And, tender churl, makest waste in niggarding. Pity the world, or else this glutton be, To eat the world's due, by the grave and thee."""]} [NOTE: EDITED FOR SPACE]

 

main 函数将 URL /plists/sonnets 传递到类 MainHandler。如果此客户机通过 HTTP GET 请求数据,就会返回类似清单 3 的结果。



verses I FROM fairest creatures we desire increase, That thereby beauty's rose might never die, But as the riper should by time decease, His tender heir might bear his memory: But thou, contracted to thine own bright eyes, Feed'st thy light'st flame with self-substantial fuel, Making a famine where abundance lies, Thyself thy foe, to thy sweet self too cruel. Thou that art now the world's fresh ornament And only herald to the gaudy spring, Within thine own bud buriest thy content And, tender churl, makest waste in niggarding. Pity the world, or else this glutton be, To eat the world's due, by the grave and thee. [NOTE: EDITED FOR SPACE]

Python 和 App Engine 示例的更详细信息以及有关 Google App Engine 的高级教程的链接

本文转自 亚嵌教育
阅读(286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~