`
weiweirenhong
  • 浏览: 32582 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Application Fundamentals--Remote procedure calls(远程过程调用)

阅读更多
Remote procedure calls--远程过程调用

Android has a lightweight mechanism for remote procedure calls (RPCs) — where a method is called locally, but executed remotely (in another process), with any result returned back to the caller. This entails decomposing the method call and all its attendant data to a level the operating system can understand, transmitting it from the local process and address space to the remote process and address space, and reassembling and reenacting the call there. Return values have to be transmitted in the opposite direction. Android provides all the code to do that work, so that you can concentrate on defining and implementing the RPC interface itself.

翻译:Android提供了对远程调用(RPC)的轻量级支持--RPC从表面上看是一个本地调用,但是实际是在另外一个进程中执行远程方法,执行结果返回本地调用者。这个过程要求操作系统能够对方法调用、方法参数及返回值具有解析能力;能够把数据对象从本地进程、本地地址空间传递到远程进程、远程地址空间;而且返回值能够逆向传递到本地进程中。所有这些,Android都提供了支持,作为开发者只需要关注远程过程调用(RPC)接口的定义和实现。

An RPC interface can include only methods. By default, all methods are executed synchronously (the local method blocks until the remote method finishes), even if there is no return value.

翻译:RPC接口可以只是包含方法定义,接口中的方法默认都是同步执行(也就是说本地方法的执行需要等待远程方法执行完成方可继续执行,即使远程方法没有返回值。)

In brief, the mechanism works as follows: You'd begin by declaring the RPC interface you want to implement using a simple IDL (interface definition language). From that declaration, the aidl tool generates a Java interface definition that must be made available to both the local and the remote process. It contains two inner class, as shown in the following diagram:

翻译:简单说,实际过程是:开发者先声明RPC接口(使用IDL接口定义语言),然后用aidl 工具生成该接口的Java接口定义,该接口对于本地和远程进程都必须是可见的,这个Java接口中包含两个内部类,如下图所示:


The inner classes have all the code needed to administer remote procedure calls for the interface you declared with the IDL. Both inner classes implement the IBinder interface. One of them is used locally and internally by the system; the code you write can ignore it. The other, called Stub, extends the Binder class. In addition to internal code for effectuating the IPC calls, it contains declarations for the methods in the RPC interface you declared. You would subclass Stub to implement those methods, as indicated in the diagram.

翻译:这两个内部类中提供了所有必要的代码,用于基于你所定义的接口的远程过程调用管理,这两个类都实现了IBinder接口,其中一个内部类用于本地,与操作系统交互。作为远程方法的开发者可以不必考虑它,另外一个内部类也叫Stub(存根),是Binder的子类,该类除了负责RPC调用的相关代码外,还包含RPC接口中所声明的方法声明,开发者应该把这个Stub(存根)类作为基类扩展出子类,在子类中实现接口定义的方法,请参考上图。

Typically, the remote process would be managed by a service (because a service can inform the system about the process and its connections to other processes). It would have both the interface file generated by the aidl tool and the Stub subclass implementing the RPC methods. Clients of the service would have only the interface file generated by the aidl tool.

翻译:通常,远程进程是由一个服务来管理的(因为服务可以向系统通知进程以及和其他进程的连接),这样的服务既包含了aidl工具生成的接口文件也包含远程方法开发者由Stub类扩展出的子类实现的远程方法,使用该服务的客户端代码只需要aidl工具生成的接口文件。

Here's how a connection between a service and its clients is set up:

下面是服务使用方和服务本身之间连接创建的过程:

    * Clients of the service (on the local side) would implement onServiceConnected() and onServiceDisconnected() methods so they can be notified when a successful connection to the remote service is established, and when it goes away. They would then call bindService() to set up the connection.

      翻译:服务客户端 (运行在本地) 需要实现 onServiceConnected() 和 onServiceDisconnected() 方法,这两个方法在与远程服务连接建立完成后或是连接断开时被系统调用。一旦连接建立,onServiceConnected()方法继续调用 bindService() 方法。
   
   * The service's onBind() method would be implemented to either accept or reject the connection, depending on the intent it receives (the intent passed to bindService()). If the connection is accepted, it returns an instance of the Stub subclass.

      翻译:服务的bindService()的作用要么是接受要么是拒绝连接,具体取决于方法的参数intent,如果接受连接,那么该方法返回Stub类的子类实例对象(IBinder接口的实现类)。

    * If the service accepts the connection, Android calls the client's onServiceConnected() method and passes it an IBinder object, a proxy for the Stub subclass managed by the service. Through the proxy, the client can make calls on the remote service.

      翻译:如果服务接受了连接,Android将立刻调用客户端的onServiceConnected()方法,同时为该方法传递一个IBinder对象,这个IBinder对象是一个Stub类的子类代理的概念,由服务负责管理它,借助这个代理实例,客户端就可以对远程服务做调用。

This brief description omits some details of the RPC mechanism. For more information, see Designing a Remote Interface Using AIDL and the IBinder class description.

翻译:以上简要描述的过程中忽视了RPC过程的一些细节,更多信息请查阅Designing a Remote Interface Using AIDL and the IBinder class description.
  • 大小: 30 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics