<document>
<localtoc/>

Sending a message from one agent to another is a basic HMTP use case. Typical applications include chat text, game updates, Atom/RSS updates, pub/sub messaging, and event notification for administration consoles. In this example, a servlet send a message to an internal logging service.

The sending agent sends a message using `sendMessage` with the jid address of the target agent (to), and a message payload. The receiving agent implements the `sendMessage` method to receive messages. Since HMTP is a streaming API like `java.io.OutputStream`, the sending method is the same as the receiving method, `sendMessage`.

HMTP messages use a JID (Jabber ID) for addressing, which looks like `user@domain` or `user@domain/resource`. The second `user@domain/resource` is used for dynamic agent, e.g. a user logged into messaging with a cellphone.

The message payload can be any serializable object. Because Hessian is available for many languages, the message can easily interoperate with RIA platforms like Flash.

Since HMTP uses a broker/agent or hub-and-spoke messaging model, all messages go through the broker to be routed to the target agent. Each service and client will register an agent with the broker and receive a unique JID for the routing.

Writing a HMTP client involves the following steps:

1. Obtaining the `HmtpConnectionFactory` (usually with @In)
2. Creating a `HmtpConnection` (which automatically registers an agent)
3. Optionally: setting message handlers to receive messages
4. Sending messages
5. Close the connection

In the example, the servlet creates a `HmtpConnection` from a `HmtpConnectionFactory` and sends the message. Opening a connection registers an agent with the broker. When the connection closes, it will unregister the agent. The Web Beans `@javax.webbeans.In` injection annotation provides access to the `HmtpConnectionFactory`.

package example; import javax.servlet.*; import javax.webbeans.In; import com.caucho.hmtp.HmtpConnectionFactory; import com.caucho.hmtp.HmtpConnection; public class TestServlet extends GenericServlet { @In HmtpConnectionFactory _factory; public void service(ServletRequest req, ServletResponse response) { HmtpConnection conn = _factory.getConnection("demo@localhost", null); try { conn.sendMessage("test@localhost", "Hello, world!"); } finally { conn.close(); } } }

Writing a HMTP Service involves the following steps:

1. Implementing `HmtpService` (usually by extending `GenericService`)
2. Getting the `HmtpBroker` (either with `@In` or done by `GenericService` automatically)
3. Registering the `HmtpService` with `HmtpBroker` using `addService` (done by `GenericService` automatically)
4. Receiving messages to the implemented `HmtpAgentStream` (or overriding `HmtpStream` methods in `GenericService`)
5. Sending messages to other agents using the broker's `getBrokerStream`.

By extending `GenericService`, the service automatically gains a queuing ability. The broker will queue the message and spawn a new thread before calling the service's `sendMessage`, in order to isolate the receiver from the sender. Advanced applications can disable the queue if appropriate.

package example; import com.caucho.hemp.broker.GenericService; import java.io.Serializable; import java.util.logging.*; public class LogService extends GenericService { private static final Logger log = Logger.getLogger(LogService.class.getName()); @Override public void sendMessage(String to, String from, Serializable value) { log.info(this + " sendMessage from=" + from + " value=" + value); } }

The HMTP configuration consists of the broker and the service configured with Resin IoC. Because the `LogService` extends `GenericService`, it will automatically register itself with the broker.

<web-app xmlns="http://caucho.com/ns/resin"> <bean class="com.caucho.hemp.broker.HempBroker"/> <bean name="test@localhost" class="example.LogService"/> </web-app>

Remote calls are another primary use for HMTP. In this example, we just query a service for some basic information. In HMTP, queries are bidirectional: the server can also query the client. And the application can also use the messaging in the previous example.

To implement the server side of an RPC call, the service implements `sendQueryGet` or `sendQuerySet`, and examines the query to see if it understands the query class. If the service does not understand the query, it will return false (or call the super method for `GenericService`). If a query returns true, it must either send a query response or query error with the same `id`, so the waiting client will always receive a response.

The type-based query system gives enormous flexibility in creating services. Services can be mash-ups of capabilities just by adding new query types.

package example; import com.caucho.hemp.broker.GenericService; import java.io.Serializable; import java.util.logging.*; public class TestService extends GenericService { private static final Logger log = Logger.getLogger(LogService.class.getName()); @Override public boolean sendQueryGet(long id, String to, String from, Serializable query) { if (query instanceof TestQuery) { getBrokerStream().sendQueryResult(id, to, from, "hello response"); return true; } else { return super.sendQueryGet(id, to, from, query); } } }

The remote client, `HmtpClient`, extends the `HmtpConnection` API to provide extra calls for connecting and logging on to the server. The message passing calls are identical.

When you create a `HmtpClient`, you'll send it the URL of the HMTP service, then call `connect()` and `login` to authenticate. The `login()` method will register an agent with the broker, letting the client send and receive messages.

This example sends a RPC query to test@localhost. RPC calls in HMTP are typed. Each query class might execute a different query on the server. In this case we create a trivial `TestQuery`.

package example; import com.caucho.hmtp.client.HmtpClient; public class TestClient { public static void main(String []args) throws Exception { HmtpClient client = new HmtpClient("http://localhost:8080/hmtp"); client.connect(); client.login("user@localhost", null); Object value = client.queryGet("test@localhost", new TestQuery()); System.out.println(value); client.close(); } }

The configuration for a remote service now has three components:

1. The `HmtpBroker`, implemented by HempBroker
2. Any registered `HmtpService`, e.g. the TestService
3. The exposed HMTP service, implemented with `HempServlet`

<web-app xmlns="http://caucho.com/ns/resin"> <bean class="com.caucho.hemp.broker.HempBroker"/> <bean name="test@localhost" class="example.TestService"/> <servlet-mapping url-pattern="/hmtp" servlet-class="com.caucho.hemp.servlet.HempServlet"/> </web-app>

Applications using HMTP will generally follow a Brokered Agent Messaging pattern, which is basically a hub-and-spoke messaging topology where the agents act as dynamic services: joining and detaching from the broker as the application progresses.

Services and clients register one or more agents with the HmtpBroker and then send messages between the agents. Each remote client will register a local agent with the local HMTP broker. Services will register one or more agents with the broker. In a tic-tac-toe game, the game instance might register two agents: one for each player in a particular game.

The diagram above has four agents: two agents for the game's players, and one agent for each logged-in user. tictactoe@host.com/1 is the game's agent for player #1, and harry@host.com/x is Harry's agent for his flash client. In the tic-tac-toe game, each user's agent talks to the matching game player, so harry@host.com/x always talks to tictactoe@host.com/1, and draco@host.com/y always talks to tictactoe@host.com/1.

The game's agents are ephemeral. When a new game begins, a `TicTacTocGame` instance registers two new agents for the new game, with unique names, e.g. tictactoe@host.com/3 and tictactoe@host.com/4. When the game ends, the instance will unregister its agents.

Because the game's agents are only created when a game begins, the tic-tac-toe game has a persistent agent for registration, tictactoe@host.com. When Harry logs on, the client will send a query to tictactoe@host.com asking for a new game. As soon as Draco asks for a match, the registration server will create a new game instance and tell Harry's client the name of his player agent, tictactoe@host.com/1.

HMTP is an adaptation of the XMPP (Jabber) instant messaging protocol. Where XMPP (Xml Messaging and Presence Protocol) is based on XML, HMTP (Hessian Message Transport Protocol) is based on Hessian. Because HMTP is designed to follow XMPP, its architecture and protocols are essentially identical until the very lowest layer, where HMTP substitutes Hessian for XML.

Because of the close relationship to XMPP, you may want to browse the XMPP specifications for a deeper understanding of how HMTP works. Since XMPP is only a wire protocol, not an API, it does not include all of the HMTP classes, but the architecture remains the same.

The primary advantages HMTP offers over XMPP include the performance advantages of Hessian over XML, and more importantly a more strict layering than XMPP provides. Because the payloads of the HMTP messages are all `Serializable`, applications have enormous flexibility in developing their own messages using application objects. In contrast, XMPP messages are always XML, so applications are not only restricted to XML data, but also must create their own XML parsers and formatters.

HMTP provides three categories of packets: messages, queries (rpc), and presence announcements. Messages and queries are typically the bulk of the packets, while presence announcements are used rarely.

Messages are unidirectional fire-and-forget packets.

Queries are request-response pairs. Each request must have a corresponding response or error.

Presence announcements are used to organize subscriptions. There are presence announcements to subscribe and unsubscribe, and presence notifications that a user has logged on, sent to all other users subscribed to his presence.

The main Message packet contains a target ("to"), a sender ("from"), and a payload ("value"). In HMTP, the payload can be any serializable value. Example messages could be IM text messages, administration console graph, game updates, or updated stock quotes. Since HMTP is bidirectional, messages can flow to and from any client.

- Message - sends a message to a resource
- MessageError - sends a message error to a resource

Query packages are RPC get and set packets with a matching response or error. Because the query will always have a matching response packet or an error packet, clients can either block for the result or attach a callback.

Like the other packets, queries are bidirectional, so a service can query a client as well as the usual client querying the server.

Query packets have an associated id field to match requests with responses. The client will increment the id for each new query.

- QueryGet - sends an information request
- QuerySet - sends an action query
- QueryResponse - returns a response
- QueryError - returns an error

Presence packets send specialized information for subscription notification. Many applications will not need to use any presence packets at all.

- Presense - sends a presence (login) notification
- PresenseUnavailable - sends unavailable (logout) notification
- PresenseProbe - query probe for IM clients
- PresenseSubscribe - request to subscribe to a service
- PresenseSubscribed - acknowledgement of a subscription
- PresenseUnsubscribe - notification of an unsubscription
- PresenseUnsubscribed - notification of an unsubscription
- PresenseError - error message

HMTP resources all have unique identifiers called JIDs (Jabber IDs). The id looks like:

user@domain/resource

The resource and user are optional.

jid description
ferg@foo.com IM user resource
ferg@foo.com/xB8 User login agent, i.e. the HMTP address corresponding to a logged in IM session.
tictactoe@foo.com tic-tac-toc game manager resource
tictactoe@foo.com/1 player #1 agent of a tic-tac-toe game
tictactoe@foo.com/2 player #2 agent of a tic-tac-toe game
tictactoe@foo.com/3 player #1 agent of a tic-tac-toe game #2
tictactoe@foo.com/4 player #2 agent of a tic-tac-toe game #2
myroom@foo.com chatroom instance
myroom@foo.com/harry chatroom nickname for user #1
myroom@foo.com/draco chatroom nickname for user #2
announcements@foo.com publish/subscribe resource

`HmtpConnection` is the primary client interface for both local and remote clients. Messages are sent using the `HmtpConnection` methods. Messages are received by setting a handler: HmtpMessageStream, HmtpQueryStream, or HmtpPresenceStream.

An active `HmtpConnection` has an associated agent registered with the broker. The agent's jid is available with the `getJid()` call.

For clients that need low-level access to the broker stream, e.g. to implement an RPC/Query handler, `getBrokerStream()` returns the underlying stream.

package com.caucho.hmtp; public interface HmtpConnection { String getJid(); boolean isClosed(); void close(); void setMessageHandler(HmtpMessageStream handler); void setQueryHandler(HmtpQueryStream handler); void setPresenceHandler(HmtpPresenceStream handler); void sendMessage(String to, Serializable value); Serializable queryGet(String to, Serializable query); Serializable querySet(String to, Serializable query); void queryGet(String to, Serializable query, HmtpQueryCallback callback); void querySet(String to, Serializable query, HmtpQueryCallback callback); void presence(Serializable []data); void presence(String to, Serializable []data); void presenceUnavailable(Serializable []data); void presenceUnavailable(String to, Serializable []data); void presenceProbe(String to, Serializable []data); void presenceSubscribe(String to, Serializable []data); void presenceSubscribed(String to, Serializable []data); void presenceUnsubscribe(String to, Serializable []data); void presenceUnsubscribed(String to, Serializable []data); void presenceError(String to, Serializable []data, HmtpError error); HmtpStream getBrokerStream(); }

The `HmtpConnectionFactory` produces `HmtpConnection` agents for client code. Typically, the factory implementation will be a `HmtpBroker`, although that is not required by the clients.

package com.caucho.hmtp; public interface HmtpConnectionFactory { HmtpConnection getConnection(String uid, String password); }

`HmtpQueryCallback` is used for callback-style RPC. When the query response completes, the agent will call the `HmtpQueryCallback` with the query's response.

package com.caucho.hmtp; public interface HmtpQueryCallback { void onQueryResult(String to, String from, Serializable value); void onQueryError(String to, String from, Serializable value, HmtpError error); }

`HmtpClient` is the remote client API for Java clients. Most of the methods are extended from `HmtpConnection`. The additional method provide some control for connection and login. Once the client is logged in, applications will typically use `HmtpConnection` methods to send messages and set handlers to receive messages.

package com.caucho.hmtp; public class HmtpClient implements HmtpConnection { public HmtpClient(String url); public void connect() throws IOException; public void login(String uid, String password); // HmtpConnection methods String getJid(); boolean isClosed(); void close(); void setMessageHandler(HmtpMessageStream handler); void setQueryHandler(HmtpQueryStream handler); void setPresenceHandler(HmtpPresenceStream handler); void sendMessage(String to, Serializable value); Serializable queryGet(String to, Serializable query); Serializable querySet(String to, Serializable query); void queryGet(String to, Serializable query, HmtpQueryCallback callback); void querySet(String to, Serializable query, HmtpQueryCallback callback); void presence(Serializable []data); void presence(String to, Serializable []data); void presenceUnavailable(Serializable []data); void presenceUnavailable(String to, Serializable []data); void presenceProbe(String to, Serializable []data); void presenceSubscribe(String to, Serializable []data); void presenceSubscribed(String to, Serializable []data); void presenceUnsubscribe(String to, Serializable []data); void presenceUnsubscribed(String to, Serializable []data); void presenceError(String to, Serializable []data, HmtpError error); HmtpStream getBrokerStream(); }

Applications will implement `HmtpMessageStream` to receive unidirectional messages from the agent. Typically, applications will extends `AbstractHmtpMessageStream` instead of implementing `HmtpMessageStream` directly.

package com.caucho.hmtp; public interface HmtpMessageStream { public void sendMessage(String to, String from, Serializable value); public void sendMessageError(String to, String from, Serializable value, HmtpError error); }

Applications will implement `HmtpQueryStream` to receive RPC calls and responses from the agent. If the application implements `sendQueryGet`, it must either send a `QueryResponse` to the sender, or send a `QueryError` or return false from the method. Queries will always have a response or an error.

package com.caucho.hmtp; public interface HmtpQueryStream { boolean sendQueryGet(long id, String to, String from, Serializable query); boolean sendQuerySet(long id, String to, String from, Serializable query); void sendQueryResult(long id, String to, String from, Serializable value); void sendQueryError(long id, String to, String from, Serializable query, HmtpError error); }

The presence methods implement the specialized subscription and presence messages. IM applications use presence messages to announce availability to people in a buddy list (roster).

Publish/Subscribe applications can also use subscription packets to subscribe and unsubscribe from the publishing service.

package com.caucho.hmtp; public interface HmtpPresenceStream { void sendPresence(String to, String from, Serializable []data); void sendPresenceUnavailable(String to, String from, Serializable []data); void sendPresenceProbe(String to, String from, Serializable []data); void sendPresenceSubscribe(String to, String from, Serializable []data); void sendPresenceSubscribed(String to, String from, Serializable []data); void sendPresenceUnsubscribe(String to, String from, Serializable []data); void sendPresenceUnsubscribed(String to, String from, Serializable []data); void sendPresenceError(String to, String from, Serializable []data, HmtpError error); }

`HmtpStream` is the core streaming API for the broker and its registered agents. It is simply a combination of all the message, query and presence packets.

package com.caucho.hmtp; public interface HmtpStream extends HmtpMessageStream, HmtpQueryStream, HmtpPresenceStream { }

`HmtpAgentStream` marks a stream toward an agent, extending `HmtpStream` and returning the agent's jid.

package com.caucho.hmtp; public interface HmtpAgentStream extends HmtpStream { Strin getJid(); }

`HmtpBroker` is the central player in the HMTP server. It's responsible for routing messages between the agents, for any forwarding to remote servers, and managing dynamic agents and services.

For all that responsibility, the API is fairly simple. The `HmtpBroker` extends `HmtpConnectionFactory`, enabling client agents, and allows custom `HmtpService` services to be implemented. Most importantly, it implements a broker stream (`HmtpStream`) which serves as the destination for all inbound messages.

package com.caucho.hmtp; public interface HmtpBroker extends HmtpConnectionFactory { HmtpStream getBrokerStream(); void addService(HmtpService service); void removeService(HmtpService service); void addServiceManager(ServiceManager manager); }

`HmtpService` represents a registered, persistent service with a known jid address. Typically the services will be registered in a configuration file, although they can also be created dynamically using the `HmtpServiceManager`. Most applications will extend the `GenericService` instead of implementing `HmtpService` directly.

The key methods are `getJid` and `getAgentStream`. The jid is used for registration with the `HmtpBroker` and `getAgentStream` is used to receive any messages.

The additional methods are used for specialized applications like instant messaging and multiuser-chat, to manage clients logging in.

package com.caucho.hmtp; public interface HmtpService { public String getJid(); public HmtpAgentStream getAgentStream(); public HmtpAgentStream findAgent(String jid); public void onAgentStart(String jid); public void onAgentStop(String jid); public HmtpAgentStream getAgentFilter(HmtpAgentStream stream); public HmtpStream getBrokerFilter(HmtpStream stream); }

`HmtpServiceManager` is a specialized manager for finding persistent sessions. In instant messaging, for example, the registered users might be stored in a database. When a message goes to harry@host.com, the `HmtpServiceManager` will lookup the appropriate user.

package com.caucho.hmtp; public interface HmtpServiceManager { public HmtpService findService(String jid); }
</document>