RMI specifications-Chapter 10

Source: Internet
Author: User
Tags object serialization

RMI communication protocol

10.1 Overview
RMI uses the other two Protocols as their communication formats: Java object serialization and HTTP. Object order
The column-based protocol is used to group calls and return data. The HTTP protocol is used to call the remote method of "send" and
Obtain the returned data when conditions permit. Each Protocol has a special syntax document. Non-termination in product rules
Symbols may reference rules managed by other protocols (Object serialization or HTTP. Cross-Protocol Boundary
The embedded protocol will be used for subsequent products.

Description of syntax symbols
We use symbols similar to those in the Java language specification (see Section 2.3 of JLS.
The control code in the stream is represented by a hexadecimal text value.
Some non-terminologies in the syntax indicate the values related to the application provided in the call. This non-Terminator
The definition of the number is composed of its Java type. The syntax is followed by ing these non-terminator characters to the corresponding type
Table.
10.2 RMI transmission protocol
The communication format of RMI is represented by stream. The term used here is from the client perspective.
Out refers to the output message, while in refers to the input message. The content of the transmitted title is not formatted using object serialization.
.

Stream: Out in

The input and output streams used by RMI appear in pairs. Each out stream has an in stream. In the syntax
In, the out stream is mapped to the socket output stream (from the client perspective ). The in stream (in syntax) is
Matches the input stream of the socket. Because the output is paired with the input stream, it is unique in the input stream.
The title information is a confirmation indicating whether the protocol is understood; other title information (such as magic number and version number)
) Will be hidden by the context of the stream pair.

10.2.1 output stream format
The output stream in RMI consists of the transmission title information followed by a message sequence. In addition, the output stream can contain
Embedded in HTTP.

Out: Header messages httpmessage

Header: 0x4a 0x52 0x4d 0x49 version Protocol

Version: 0x00 0x01

Protocol: streamprotocol singleopprotocol multiplexprotocol

Streamprotocol: 0x4b

Singleopprotocol: 0x4c

Multiplexprotocol: 0x4d

Messages: Message messages Message

Messages will be packaged in a specific protocol specified by protocol. For singleopprotocol, he
A message may be followed by one message, and the message does not wrap other data. Single
Opprotocol is used for Embedded calls in HTTP requests. Both requests and responses can be one request.

For streamprotocol and multiplexprotocol, the server must use the byte 0x4e (Supporting
With this Protocol) and endpointidentifier (including the host name and port number, the server can see them
In use by the client. If the operation cannot be performed due to security reasons, the client can
Use this information to determine the host name. Later, the client must use another default end that contains the accepted connection.
The endpointidentifier of the vertex responds. For multiplexprotocol, the server can use it
To identify the client.

For streamprotocol, after this endpoint negotiation, messages will be sent to the output stream, instead
Data is further packaged. For multiplexprotocol, the socket uses the connection as a multiplexing connection.
The specific connection, as described in Section 10.6 "RMI multiplexing protocol. In this multiplexing connection
The initialized virtual connection consists of a series of messages, as described below.

There are three types of output messages: Call, Ping, and dgcack. Call will encode the method call. Pin
G is a transmission-level message used to test the activity of remote virtual machines. Dgcack is
The confirmation of the distributed garbage collector indicates that the client has received the remote object in the return value of the server.

Message: Call Ping dgcack

Call: 0x50 calldata

Ping: 0x52

Dgcack: 0x54 uniqueidentifier

10.2.2 input stream format
There are three types of input information: returndata, httpreturn, and pingack. Returndata is
The result of the "normal" RMI call. Httpreturn is the result of an embedded call in the HTTP protocol. P
Ingack is the confirmation of the ping message.

In: protocolack returns protocolnotsupported httpreturn

Protocolack: 0x4e

Protocolnotsupported: 0x4f

Returns: Return returns return

Return: returndata pingack

Returndata: 0x51 returnvalue

Pingack: 0x53
10.3 use of RMI for Object serialization Protocol
The call and return data in RMI calls are formatted using the Java object serialization protocol. Each party
Method call calldata is represented as objectidentifier (call target), Operation (generation
Number of methods to be called in the table) and hash (check whether the client stub and the remote object skeleton make
Number of the same stub protocol), followed by the list of zero or multiple parameters of the call.

In the jdk1.1 stub protocol, operation represents the method number (allocated by rmic), while hash is
Stub/skeleton is the interface hash of the stub. In jdk1.2 stub protocol (using
In jdk1.2 stub generated by rmic with the-v1.2 option), the operation value is-1 and hash
Represents the hash of the method to be called. Hash is described in the remoteref interface section.

Calldata: objectidentifier operation hash argumentsopt

Objectidentifier: objectnumber uniqueidentifier

Uniqueidentifier: Number Time count

Arguments: Value arguments Value

Value: Object primitive

The returnvalue called by RMI is returned by the Code indicating a normal or abnormal response, and the UN that marks the returned value
Iqueidentifier (used to send dgcack when necessary) is followed by the following returned results: returned value
Or throw an exception.

Returnvalue: 0x01 uniqueidentifier valueopt 0x02 uniqueidentifier exc
Eption

----------------------------------------------------------------------
----------

Note-objectidentifier, uniqueidentifier, and endpointidentifier are not
Written by default serialization, but respectively using their own write method (but not used for Object serialization)
The writeobject method); The write method of each identifier adds its component data to
Output stream.

----------------------------------------------------------------------
----------

10.3.1 class annotation and class loading
RMI respectively covers annotateclas of objectoutputstream and objectinputstream
S and resolveclass methods. Each class uses codebase URL (the location where the class is loaded) for injection.
. In the annotateclass method, the class loader that loads the class is queried to get its codebase URL.
If the class loader is not empty and Its codebase is not empty, objectoutputstream. wr is used.
The iteobject method writes the codebase to the stream. Otherwise, the writeobject method is used to write null values.
Writing to the stream. Note: It is best not to annotate classes in the "Java" package, because they are always
Is available.

Class annotation is parsed using the objectinputstream. resolveclass method during serialization recovery.
The resolveclass method first reads the annotation using the objectinputstream. readobject method. If
The annotation (codebase URL) is not empty. It obtains the class loader of the URL and tries to load the class. Exploitation
Java.net. urlconnection obtains the class bytes to load the class (with
The Applet Class Loader uses the same mechanism ).

10.4 use of RMI for http post Protocol
To call remote methods through the firewall, some RMI calls use the HTTP protocol, especially HTTP
Post. The URL specified in the transfer title can be one of the following:

Http: //:/http: // cgi-bin/Java-RMI? Forward =

The first URL is used to directly communicate with the RMI server on a specific host and port. Second form
The URL is used to call the "cgi" script on the server. The latter forwards the call to
Server.

Httppostheader is the standard HTTP header of the POST request. Httpresponseheader is a pair
The standard HTTP Response of the delivery process. If the response status code is not 200, no return value is considered. Note
Only one RMI call can be embedded in an http post request.

Httpmessage: httppostheader header message

Httpreturn: httpresponseheader return

----------------------------------------------------------------------
----------

Note: Only singleopprotocol appears in the httpmessage title. Httpreturn not
Contains the bytes used to confirm the protocol.

----------------------------------------------------------------------
----------
10.5 RMI values related to applications
This table lists the non-terminologies used by RMI to represent values related to applications. This table adds each symbol
Ing to the corresponding type. Each symbol uses the protocol embedded in it for formatting.

Count short

Exception java. Lang. Exception

Hash long

Hostname UTF

Number int

Object java. Lang. Object

Objectnumber long

Operation int

Portnumber int

Primitive byte, Int, short, long...

Time long

10.6 RMI multiplexing protocol
The purpose of multiplexing is to provide a model in which two endpoints can open the full double link between multiple vertices to the other end.
In the same environment, when other tools (such as TCP connections) are used, only one endpoint can
Enable this two-way connection. With this simple multiplexing protocol, RMI allows customers
When other protocols are powerless, connect to the RMI server object. For example, some applets
The security manager of the environment does not allow the creation of server socket listening incoming connections to prevent such appl
Et exports RMI objects from direct socket connections and provides remote call services. However, if the app
Let can open a normal socket connection to its codebase host, and it can use
The multiplexing protocol allows the codebase host to call the RMI object exported by the applet.
Method. This section describes the format and rules of multiplexing protocols.

 

10.6.1 Definition
This section defines some terms that will be used in the rest of the Protocol.

The endpoint is one of the two users connected by multiplexing protocol.

The multiplexing protocol must be located above the existing bidirectional reliable byte stream. Assume that one endpoint is directed to another.
Click to initialize. In the current RMI usage, it is usually a TCP connection, which is composed of java.net. Sock
Et object creation. This connection is called a specific connection.

The multiplexing protocol facilitates the use of virtual connections. Virtual Connection itself is a two-way reliable byte stream.
Table specific sessions between two endpoints. The virtual connection set between two endpoints on one connection is composed of multiple
Multiplexing connection. With the multiplexing protocol, virtual connections can be opened and closed by any endpoint. Virtual Connection
The status of a given endpoint is defined by the multiplexing protocol elements sent and received over a specific connection. The
The status indicates whether the connection is enabled, closed, or transmitted, and the related traffic control mechanism. If no
Note that the rest of this section uses the term connection to represent a virtual connection.

The virtual connection in a given multiplexing connection is identified by a 16-bit integer and is called a connection identifier. Therefore
There may be 65,536 virtual connections in A multiplexing connection. The implementation may limit
The number of virtual connections used.

10.6.2 connection status and Flow Control
Connections are controlled by various operations defined by multiplexing protocols. The following is the operation name defined by the Protocol: op
En, close, closeack, request, and transmit. The correct format and rules for all operations are
Section 10.6.3 "protocol format" details.

Open, close, and closeack operations control the opening and closing of connections, while request and transm
It operations are used to transmit data over open connections within the limits of the flow control mechanism.

Connection status
If the endpoint sends the connection open operation or receives the connection open operation (and does not close it later)
. The following describes different protocol operations.

If the endpoint sends the close operation of the connection but does not receive the close or close operation of the connection
Ack operation, the virtual connection is waiting to be closed relative to the endpoint.

If the endpoint never opens a connection or receives the close or closeack operation of the connection (and then
), The virtual connection is closed relative to the endpoint.

Flow Control
Multiplexing uses a simple Packet Flow Control mechanism to allow multiple virtual connections to coexist on the same specific connection.
. The advanced requirement of the flow control mechanism is that the status of all virtual connections is independent; the status of one connection is not
Other connections are affected. For example, if the data buffer for processing data from a connection is full, do not
It will prevent data transmission and processing of other connections. If the connection continues to depend on the end of another connection (
For example, when calling recursive RMI), this is crucial. Therefore, its practical significance is required for implementation.
Always consume and process all multiplexing protocols that are prepared for input on a specific connection (assuming it complies with this specification)
Discuss data.

Each endpoint has two State values associated with each connection: the number of Status values that have been requested but not received by this endpoint
Data bytes (number of input requests) and the number of data bytes requested by the other end but not provided by this endpoint (output
Requests ).

The number of output requests of the endpoint increases when the request operation is received from other endpoints, while
The ansmit operation is reduced. The number of input requests of the endpoint increases when it sends a request.
It will decrease when it receives the transmit operation. If these values are negative, they violate the protocol.

If the number of input requests increases and exceeds the value of the request sent by the endpoint, no blocking occurs.
The number of bytes is in violation of the Protocol. However, if the connected user is waiting to read data, ensure that the user's input
The number of requests is greater than zero.

If the transmit operation package sent by the endpoint contains more bytes than the number of output requests, the Protocol is violated.
. It can buffer outbound data until the connection user request explicitly refreshes the data written to the connection. However
If the data must be sent on the connection because the explicit refresh or input buffer is full, the connection uses
User may be blocked until there is enough transmit operation.

When the above rules are met, the request and transmit operations can be freely sent.
. For example, if the input buffer is not empty, the endpoint can request more data to be connected.

10.6.3 protocol format
The byte stream format of multiplexing protocol is composed of consecutive variable length record sequences. The first byte of the record.
It is an operation code that identifies recorded operations and determines the format of the rest of its content. We decide
The following valid operation codes are defined:

Value Name

0xe1 open

0xe2 close

0xe3 closeack

0xe4 request

0xe5 transmit

If the first byte of the record is not the operation code defined, the Protocol is violated. The following sections describe each
The format of the operation code record.

Open Operation
The following is the record format of an open operation:

Size (byte) Name Description

1 opcode operation code (open)

2 ID connection ID

The endpoint sends an open operation to open the specified connection. If the ID points to the current enabled or
The connection to be closed is in violation of the Protocol. After the connection is enabled, the connection input and request count are in two statuses.
Point is zero.

When an open operation is received, the other end is opening the specified connection. Connection output after the connection is enabled
And the request count status are zero at both endpoints.

To prevent identifier conflicts between two vertices, the valid connection identifier space is divided into two halves based on the maximum value.
. Each endpoint only allows a connection with a high value as a specific value. The endpoint of the specific connection must be
Enable the high bit as the link in the identifier, and the other end point must only open the high bit zero connection. For example
RMI applet that can create server sockets starts multiplexing connection with its codebase host
The applet can open a virtual connection with the identifier range of 0x8000-7fff, while the server can
Open a virtual connection with the identifier range of 0-0x7fff.

Close Operation
The format of the close operation is as follows:

Size (byte) Name Description

1 opcode operation code (open)

2 ID connection ID

The endpoint sends the close operation to close the specified connection. If the ID points to the current disabled or
The connection to be closed (if it has sent the close operation of this connection, it may also be
Will close the connection), it violates the protocol. After close is sent, the connection is about to close the sending endpoint.
Closed connection. Therefore, the endpoint cannot re-open the connection until it receives the clo from the other end point.
Se or closeack.

When the close operation is received, the specified connection is closed at the other end. Therefore, the connection is already on the receiving end.
Disabled. Although the receiving endpoint may no longer send other operations for this connection (until it is opened again ),
It should still provide data in the input buffer for the connected reader. If the connection has been enabled (
), The receiving endpoint must use the closeack operation as a response.

Closeack operation
The following is the record format of closeack operations:

Size (byte) Name Description

1 opcode operation code (open)

2 ID connection ID

The closeack operation sent by the endpoint indicates that the close operation from the receiving endpoint has been received. If you receive
If the connection indicated by the ID is not the connection to be closed for the receiving endpoint, the Protocol is violated.

After receiving the closeack operation, you can change the status of the specified connection from closing to closed.
You can re-open the connection.

Request operation
The request operation record format is as follows:

Size (byte) Name Description

1 opcode operation code (open)

2 ID connection ID

4 count additional bytes of the request

The endpoint sends a request to increase the number of input requests for the specified connection. If the ID does not point to the sending endpoint
The connection is in violation of the Protocol. The number of input requests of the endpoint increases by count. The value of count is
A 32-bit signed integer. If the value is negative or zero, the Protocol is violated.

When the request is received, the number of output requests for the specified connection increases by count. If the receiving endpoint
If the connection is to be closed, the request operation will be ignored.

Transmit operation
The following is the record format of the transmit operation.

Size (byte) Name Description

1 opcode operation code (open)

2 ID connection ID

4 count the number of bytes transmitted

Count data transmission data

After the endpoint sends the transmit operation, data is transmitted on the specified connection. If the ID does not point
The connection opened by the sending endpoint violates the protocol. The output requests of the endpoint decrease by count. Count
The value is a 32-bit signed integer. If the value is negative or zero, the Protocol is violated. If the transmit operation is imported
If the number of output requests becomes negative, the Protocol is also violated.

When the transmit operation is received, the byte queue that can be read from the connection will increase the data of Count bytes.
The number of input requests received by the endpoint decreases by count. If this changes the number of input requests to zero, the connection
If the user tries to read more data, the endpoint applies another request operation as a response. For example
If the receiving endpoint is about to close the connection, the transmit operation is ignored.

Breach of Agreement
If the above violation or communication error is detected in a specific connection, multiplexing
The connection is closed. The actual connection is terminated, and all virtual connections are closed immediately. Connected user
You can read data that can already be read from a virtual connection.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.