毕业论文(设计)基于Intel UPnP SDK 的UPnP 协议编程28925.doc
文章编号:1009-8119(2005)07-0044-02基于Intel UPnP SDK的UPnP协议编程王增军 (北京理工大学计算机科学工程系,北京 100081)摘 要简单介绍了Intel UPnP开发包并以媒体服务器为例介绍了基于其上的UPnP协议编程。关键词 UPnP, Intel SDK, MediaServerIntel UPnP SDK Based UPnP ProgrammingWang Zengjun (Department of Computer Science and Technology,Beijing Institute of Technology,Beijing 100081)Abstract A simple introduction to Intel® SDK for UPnP Devices for Linux and an example of UPnP MediaServer developed based on the SDK.Keywords UPnP, Intel SDK, MediaServer1 UPnP简介UPnP(通用即插即用Universal Plug and Play)是Microsoft对单机环境下即插即用技术概念在网络环境中的扩展。UPnP 支持零配置网络及自动查找功能,这样一来,设备即可动态加入网络,获取 IP 地址,宣布自己的名称,根据需要提供功能,并了解其它设备的存在和功能。此外,设备还可自动平稳地脱离网络,而不会遗留任何不必要的状态。UPnP 不依赖于特定的操作系统、编程语言或物理媒体。UPnP 面向家庭网络、小型企业和商住楼中的网络。正如Microsoft公司所称,“UPnP将延伸到家庭中的每一个设备,它会成为个人电脑、应用程序、智能设备集成工作所必需的框架、协议和接口标准”。 UPnP最基本的概念模型是设备模型,设备可以是物理的设备,也可以是逻辑的设备。设备分为两种:控制点和提供服务的设备,或称为受控设备,控制点是一个控制器,它可以检索设备和服务描述,发送动作到服务,查询服务的状态变量和从服务接收事件。允许用户使用或运行一个设备(例如CD播放机)的程序可以认为是控制点。控制点至少包括发现(Discovery)、描述(Description)和命令转换器(Rehydrator),还可以包含事件订阅等。一个控制点可同时控制多个设备,而控制点所在的设备可以同时是一个提供服务的设备。受控设备同样是一组软件模块的集合,用于实现同用户控制点的通讯,这些软件模块包括发现服务器(Discovery Server)、描述服务器(Description Server)、控制服务器(Control Server)以及表征服务器、事件订阅服务器、事件源等。UPnP 使用Internet 上广泛应用的多种标准,包括 IP、TCP、UDP、HTTP 及 XML等作为设备之间通讯的协议, 使用这些标准化的协议有助于确保实现供应商之间的互操作性。由于相同的协议已在使用之中,因此几乎不需要执行任何操作就可以让 UPnP 设备在现有的网络化环境中工作。其中XML 是 UPnP 的核心部分,被用在设备和服务描述、控制消息和事件处理中。UPnP设备工作过程包括设备寻址、设备发现、设备描述、设备控制、事件通知和设备表征几部分。设备寻址是指当一个设备接入网络时,通过DHCP服务器,或者通过Auto IP获得唯一的IP地址。下面就是设备发现,使用简单服务发现协议SSDP(Simple Service Discovery Protocol)来完成,控制点可以主动搜索,其他设备响应,或者设备通过广播所能提供的服务通知控制点,控制点将得到一个指向服务设备描述页面的URL,控制点根据这个URL获得服务设备详细的信息。在接收设备和服务描述之后,控制点可以向这些服务发出动作,为获得服务的状态,控制点也可以进行轮询,或者向服务订阅事件,当服务的状态改变时,服务就会向控制点发布设备事件。设备表征并不强制实现,它采用HTML页的形式,使控制点能在浏览器中打开,通常用来给用户提供控制设备或者浏览设备状态的界面。2 Intel® SDK for UPnP Devices for LinuxIntel开发包使用C语言开发,它是开源的。开发包封装了UPnP涉及的众多协议,涵盖从寻址、发现到表征各个过程,提供了简单的接口。这样,开发人员就摆脱了各种协议的细节,只需进行服务或者控制所需的具体开发,简化了UPnP应用的设计开发。另外,该开发包还包括多线程支持库,XML解释器,迷你Web服务器等。基于Intel开发包开发UPnP应用,以服务设备为例,主要有以下几个基本步骤:首先是初始化:包括开发包的初始化和设备的初始化。(1)调用UPnPInit()初始化整个开发包;(2)通过UPnPSetWebServerRootDir()注册设备描述文档存放的目录;(3)调用UPnPRegisterRootDevice()或者UPnPRegisterRootDevice2()向开发包注册设备描述文档;(4)调用UPnPSendAdvertisement()向网络广播设备。初始化完成后,设备进入等待请求循环,响应控制点的请求。请求主要有:事件订阅、获取服务状态、设定服务状态等,当服务状态改变时,调用UPnPNotify() 或者 UpnPNotifyExt()通知控制点。设备关闭首先向网络发送一个“bye-bye” SSDP 消息,然后调用UpnPUnRegisterRootDevice()向开发包取消设备注册,最后调用UpnPFinish()让开发包做必要的清理工作。3 开发实例MediaServer3.1 UPnP AV规范控制点媒体服务器内容目录内容传输连接管理媒体播放器播放控制连接管理内容传输家庭网络图1 UpnP AV 体系结构媒体服务器遵循UPnP AV规范 3,它是UPnP论坛4定义的规范之一,该规范以消费类电子产品为对象,定义了一系列的UPnP 设备和服务模板,目标是通过UPnP使其摆脱现有的模拟线路连接方式,转向数字家庭网络。如图1所示,UPnP AV规范所定义的体系结构由三种主要的实体组成:控制点、媒体服务器和播放器。媒体服务器提供内容目录、内容传输、连接管理三种服务,负责向网络中的其他UPnP 设备提供各种娱乐内容,供播放器展现出来,控制点提供用户控制的界面,并协调服务器和播放器之间的交互操作。下面就以媒体服务器为例简要介绍一下如何利用Intel的开发包在linux系统下开发UPnP应用。3.2 MediaServer设计媒体服务器一般用来存放各种形式的多媒体内容,它们数量庞大,格式繁杂,所以要实现AV规范所规定的服务,首先要考虑的是以何种方式发现或者管理所有的内容,以何种有效的形式存放和管理有关信息。如果对向服务器中添加、修改、删除内容等操作提供统一的接口,则简化了内容发现,并且可以及时的掌握内容的变化情况;利用数据库管理信息,则方便了查找,然后决定服务器所提供的服务种类。所以整个应用可以分为以下几个部分:(1)媒体库管理:提供统一的接口,按照用户需求将各种媒体加入媒体库中,或者改变内容,还可以手动进行内容同步;(2)数据库管理:按照媒体库生成存放媒体信息的数据库,提供查询功能。在这个简化的例子中,假设服务器中的媒体内容变化不频繁,所以媒体库管理和数据库管理结合在一起,只提供了手动重建数据库功能,并不能动态的跟踪媒体的变化。(3)UPnP服务:本例只实现了内容目录,支持的动作只有browse。3.3 MediaServer实现本例的开发环境为Mandrake Linux, BerkerlyDB, GNU 编译器,开发语言是C+,根据设计,如图2所示,整个应用主要分如下几个部分:媒体库/数据库管理MeteDBMgr.cppUPnPDeviceConnectionManagerContentDirectoryUPnPServiceMediaServerMetaDBMgr图2 媒体服务器结构虽然进行了简化设计,不对媒体变化进行动态跟踪,但是由于媒体类型多,各种媒体文件格式迥异,所以这一部分是整个应用开发中工作量最大的,所幸的是互联网上有大量的源码可以参考或直接利用。由MetaDBMgr类实现,主要接口有:init(string path)在指定的目录中创建数据库,为简化实现,所有的属性存放在一个数据库表中。populate(string mediapath)在指定的目录中遍历搜索可以识别的媒体文件,解析出所需的属性信息,如长度,大小等存入数据库中。search(string id,string key,vetor<string>*result)是一个简单的搜索接口。UPnPService.cpp: UPnPService类是服务类的基类,定义如下:class UPnPService/响应UPnPDevice传来的事件 virtual int handleRequest(UPnP_EventType* event); int getVar(string* var, string *varval);/取得服务中变量名及值 virtual string serialize ();/生成描述服务的XML片断 struct serVar string name; string value; struct serActstring name; upnp_action handler; list<serVar> varList; list<serAct> actionList;class ContentDirectory:public UPnPService/实现内容目录服务,int browse(IXML_Document* in, IXML_Document* out, char* errorString); bool initMetaDB (String rootdir = "/medialib") ;/初始化数据库;MediaServer.cppclass UPnPDevice/UPnPD设备的基类string serialize(); /遍历serviceList生成设备描述XML格式的字串int add(UPnPIcon&);int add(UPnPService&);int handleRequest(UPnP_EventType EventType, void *Event, void *Cookie );/分发请求给各服务类list< UPnPService> serviceList;class MediaServer:public UPnPDeviceint init();/初始化内容目录服务、连接服务int shutdown();main.cppint callBack(void* cookie) /SDK回调函数,((UPnPDevice*)cookie)-> handleRequest;/int main()/主函数 UpnpInit(); MediaServer ms; ms.init(); string desc(ms.serialize(); UPnPRegisterRootDevice2(UPNPREG_BUF_DESC, desc, des.length(), 1, &ms,handleRequest);4 结束语待添加的隐藏文字内容3UPnP是应计算网络化、家电信息化的趋势而生,必然像PnP一样,成为未来一种主流技术。当然,现在的UPnP还并不完善,如在安全上重视不够等。另外,由Sun主推的Jini,在体系结构和应用目标上和UPnP非常相像,功能上各有长短,它们之间必将会有一定的竞争。但UPnP有效率上的优势,而且又得到了包括Intel在内的多个大厂商支持,一定会有迅速的发展。参考文献1 UPnP协议.http:/www.upnp.org/resources/2 Programming Guide Intel® SDK for UPnP Devices by Intel3 UPnP AV规范.http:/www.UPnP.org/standardizeddcps/mediaserver.asp4 UPnP 论坛. http:/www.up np.orgEditor's note: Judson Jones is a meteorologist, journalist and photographer. He has freelanced with CNN for four years, covering severe weather from tornadoes to typhoons. Follow him on Twitter: jnjonesjr (CNN) - I will always wonder what it was like to huddle around a shortwave radio and through the crackling static from space hear the faint beeps of the world's first satellite - Sputnik. I also missed watching Neil Armstrong step foot on the moon and the first space shuttle take off for the stars. Those events were way before my time.As a kid, I was fascinated with what goes on in the sky, and when NASA pulled the plug on the shuttle program I was heartbroken. Yet the privatized space race has renewed my childhood dreams to reach for the stars.As a meteorologist, I've still seen many important weather and space events, but right now, if you were sitting next to me, you'd hear my foot tapping rapidly under my desk. I'm anxious for the next one: a space capsule hanging from a crane in the New Mexico desert.It's like the set for a George Lucas movie floating to the edge of space.You and I will have the chance to watch a man take a leap into an unimaginable free fall from the edge of space - live.The (lack of) air up there Watch man jump from 96,000 feet Tuesday, I sat at work glued to the live stream of the Red Bull Stratos Mission. I watched the balloons positioned at different altitudes in the sky to test the winds, knowing that if they would just line up in a vertical straight line "we" would be go for launch.I feel this mission was created for me because I am also a journalist and a photographer, but above all I live for taking a leap of faith - the feeling of pushing the envelope into uncharted territory.The guy who is going to do this, Felix Baumgartner, must have that same feeling, at a level I will never reach. However, it did not stop me from feeling his pain when a gust of swirling wind kicked up and twisted the partially filled balloon that would take him to the upper end of our atmosphere. As soon as the 40-acre balloon, with skin no thicker than a dry cleaning bag, scraped the ground I knew it was over.How claustrophobia almost grounded supersonic skydiverWith each twist, you could see the wrinkles of disappointment on the face of the current record holder and "capcom" (capsule communications), Col. Joe Kittinger. He hung his head low in mission control as he told Baumgartner the disappointing news: Mission aborted.The supersonic descent could happen as early as Sunday.The weather plays an important role in this mission. Starting at the ground, conditions have to be very calm - winds less than 2 mph, with no precipitation or humidity and limited cloud cover. The balloon, with capsule attached, will move through the lower level of the atmosphere (the troposphere) where our day-to-day weather lives. It will climb higher than the tip of Mount Everest (5.5 miles/8.85 kilometers), drifting even higher than the cruising altitude of commercial airliners (5.6 miles/9.17 kilometers) and into the stratosphere. As he crosses the boundary layer (called the tropopause), he can expect a lot of turbulence.The balloon will slowly drift to the edge of space at 120,000 feet (22.7 miles/36.53 kilometers). Here, "Fearless Felix" will unclip. He will roll back the door.Then, I would assume, he will slowly step out onto something resembling an Olympic diving platform.Below, the Earth becomes the concrete bottom of a swimming pool that he wants to land on, but not too hard. Still, he'll be traveling fast, so despite the distance, it will not be like diving into the deep end of a pool. It will be like he is diving into the shallow end.Skydiver preps for the big jumpWhen he jumps, he is expected to reach the speed of sound - 690 mph (1,110 kph) - in less than 40 seconds. Like hitting the top of the water, he will begin to slow as he approaches the more dense air closer to Earth. But this will not be enough to stop him completely.If he goes too fast or spins out of control, he has a stabilization parachute that can be deployed to slow him down. His team hopes it's not needed. Instead, he plans to deploy his 270-square-foot (25-square-meter) main chute at an altitude of around 5,000 feet (1,524 meters).In order to deploy this chute successfully, he will have to slow to 172 mph (277 kph). He will have a reserve parachute that will open automatically if he loses consciousness at mach speeds.Even if everything goes as planned, it won't. Baumgartner still will free fall at a speed that would cause you and me to pass out, and no parachute is guaranteed to work higher than 25,000 feet (7,620 meters).It might not be the moon, but Kittinger free fell from 102,800 feet in 1960 - at the dawn of an infamous space race that captured the hearts of many. Baumgartner will attempt to break that record, a feat that boggles the mind. This is one of those monumental moments I will always remember, because there is no way I'd miss this.