JXME-4-SE

A small (back-back-)port of JXME client to work under JSE.

Introduction

Given the current state of JXTA-JXME, its clear to figure its current unmaintained state. This page presents a technical overview, as well as presenting some solutions for compiling and prototyping it under Java SE.

DISCLAIMER

I made this document in hopes of sharing the knowledge acquired when dealing with JXTA-JXSE. It doesn't mean that it's the best solution, nor it's a criticism of neither JXSE nor JXME. It's just a small note regarding my experiences when doing JXTA work for CLDC, and also prototyping under JSE.

Overview

JXME Link is a small framework to talk to a JXTA Super Peer with the JxtaProxy capabilities. It works by using a custom protocol to make the super peer (acting as a proxy/JxtaProxy) work on its behalf. It consists of a class, PeerNetwork, which lets you poll() and send() custom Messages, which are a collection of Elements.

If this description looks too shallow, check the sources for the Chat sample (net.jxta.midp.demo.chat.Chat). It's a pretty nice nice overview into JXME.

Compiling It

It's not easy, when using the supplied build.xml. A Hack suitable for MIDP is to just copy the files from the midp directory (packages net/jxta/j2me) over to a new project. If you want to check the Chat sample, copy Chat.java. It should easy to figure the missing bits and pieces.

Required Work to Port to JSE

First, JSE doesn't implement the GCF. Instead, we need to implement the equivalent functionality. Mainly, by replacing the GCF calls to HTTP Protocol to, say, Jakarta HttpClient. I'm using 3.1, thus meaning having to import also commons-logging., and commons-codec (I'm using versions 1.1 and 1.3, respectively).

Copy the sources from the JXME definition. This patch should be applied to HttpMessenger class for CLDC, which should be copied over to the net.jxta.j2me package:

59a60
> import java.io.ByteArrayOutputStream;
64,65c65,68
< import javax.microedition.io.Connector;
< import javax.microedition.io.HttpConnection;
---
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
> import org.apache.commons.httpclient.methods.GetMethod;
> import org.apache.commons.httpclient.methods.PostMethod;
68,70c71,73
<  * Provides a messaging service for the JXTA for MIDP peer. Facilities
<  * to send and receive messages are provided. Message receiving is
<  * based on establishing a relationship with a JXTA relay server.
---
>  * Provides a messaging service for the JXTA for MIDP peer. Facilities to send
>  * and receive messages are provided. Message receiving is based on establishing
>  * a relationship with a JXTA relay server.
76,81c79,82
<      * The duration that the client is willing to for the
<      * connection to block until there is a message to be
<      * sent. Setting it to -1 means do not block. Setting
<      * it to 0 would mean wait forever, but the server will
<      * never comply: it caps it at some tunable value
<      * (120000 actually).
---
> 	 * The duration that the client is willing to for the connection to block
> 	 * until there is a message to be sent. Setting it to -1 means do not block.
> 	 * Setting it to 0 would mean wait forever, but the server will never
> 	 * comply: it caps it at some tunable value (120000 actually).
89,93c90,92
<      * The duration that the client is willing to block for
<      * more messages on the same connection (using
<      * chunking). If set to -1, the connection closes after
<      * one message.
<      * TBD: Not Used
---
> 	 * The duration that the client is willing to block for more messages on the
> 	 * same connection (using chunking). If set to -1, the connection closes
> 	 * after one message. TBD: Not Used
102,103c100
<     private final static String RELAY_SERVICE_ID =
<             "uuid-DEADBEEFDEAFBABAFEEDBABE0000000F05";
---
> 	private final static String RELAY_SERVICE_ID = "uuid-DEADBEEFDEAFBABAFEEDBABE0000000F05";
124c123,124
<     HttpMessenger() { }
---
> 	HttpMessenger() {
> 	}
126,129c126,127
<     private String constructURL(String initRelayUrl,
<             String command,
<             int timeout,
<             String pid) {
---
> 	private String constructURL(String initRelayUrl, String command,
> 			int timeout, String pid) {
133,134c131
<         "/" + pid +
<                 "?" + Integer.toString(timeout) +
---
> 				"/" + pid + "?" + Integer.toString(timeout) +
148,150c145,146
<             url += "," +
<                     Integer.toString(DEFAULT_CLIENT_LEASE) + "," +
<                     "keep,other";
---
> 			url += "," + Integer.toString(DEFAULT_CLIENT_LEASE) + ","
> 					+ "keep,other";
155a152
> 	private final HttpClient httpClient = new HttpClient();
160,161c157,160
<      * @param  initRelayUrl     Relay url
<      * @param  persistedPeerId  PeerID if any
---
> 	 * @param initRelayUrl
> 	 *            Relay url
> 	 * @param persistedPeerId
> 	 *            PeerID if any
163c162,163
<      * @exception  IOException  if an i/o error occurs
---
> 	 * @exception IOException
> 	 *                if an i/o error occurs
167,168c167
< 
<         if (initRelayUrl == null) {
---
> 		if (initRelayUrl == null)
170c169
<         }
---
> 
176,179c175,176
<             relay = constructURL(initRelayUrl,
<                     COMMAND_GET_PID,
<                     DEFAULT_CONNECT_POLL_TIMEOUT,
<                     UNKNOWN_PID);
---
> 			relay = constructURL(initRelayUrl, COMMAND_GET_PID,
> 					DEFAULT_CONNECT_POLL_TIMEOUT, UNKNOWN_PID);
181,184c178,179
<             relay = constructURL(initRelayUrl,
<                     COMMAND_CONNECT,
<                     DEFAULT_CONNECT_POLL_TIMEOUT,
<                     peerId);
---
> 			relay = constructURL(initRelayUrl, COMMAND_CONNECT,
> 					DEFAULT_CONNECT_POLL_TIMEOUT, peerId);
187c182,185
<         HttpConnection conn = null;
---
> 		GetMethod getMethod = new GetMethod(relay);
> 
> 		getMethod.addRequestHeader("Content-Type", DEFAULT_MIME_TYPE);
> 
190,200c188,192
<             conn = (HttpConnection) Connector.open(relay, Connector.READ_WRITE);
<             conn.setRequestMethod(HttpConnection.GET);
<             conn.setRequestProperty("Connection", "close");
<             conn.setRequestProperty("Content-Length", "0");
<             conn.setRequestProperty("Content-Type", DEFAULT_MIME_TYPE);
< 
<             if (conn.getResponseCode() != HttpConnection.HTTP_OK &&
<                     conn.getResponseCode() != 100) {
<                 throw new IOException("HTTP Error: " +
<                         conn.getResponseCode() + " " +
<                         conn.getResponseMessage());
---
> 			int responseCode = httpClient.executeMethod(getMethod);
> 
> 			if (responseCode != 200 && responseCode != 100) {
> 				throw new IOException("HTTP Error: " + responseCode + "/"
> 						+ getMethod.getStatusLine());
203c195
<             if (conn.getLength() <= 0) {
---
> 			if (getMethod.getResponseContentLength() <= 0L) {
208c200
<             dis = conn.openDataInputStream();
---
> 			dis = new DataInputStream(getMethod.getResponseBodyAsStream());
216,217c208,209
<                 if (Message.RESPONSE_TAG.equals(el.getName()) &&
<                         COMMAND_GET_PID.equals(data)) {
---
> 				if (Message.RESPONSE_TAG.equals(el.getName())
> 						&& COMMAND_GET_PID.equals(data)) {
237c229
<             if (dis != null) {
---
> 			if (dis != null)
239,242c231,232
<             }
<             if (conn != null) {
<                 conn.close();
<             }
---
> 			if (getMethod != null)
> 				getMethod.releaseConnection();
250,251c239,240
<      * Polls for a message from the relay. This is a blocking call. If a
<      * message is not received within the timeout value, null is returned
---
> 	 * Polls for a message from the relay. This is a blocking call. If a message
> 	 * is not received within the timeout value, null is returned
253,254c242,245
<      * @param  timeout          timeout in millis
<      * @param  outgoing         if not empty, message is sent
---
> 	 * @param timeout
> 	 *            timeout in millis
> 	 * @param outgoing
> 	 *            if not empty, message is sent
256c247,248
<      * @exception  IOException  if an i/o error occurs
---
> 	 * @exception IOException
> 	 *                if an i/o error occurs
261c252,254
<         HttpConnection conn = null;
---
> 		PostMethod postMethod = null;
> 		int responseCode = 0;
> 
268,278c261,267
<             conn = (HttpConnection) Connector.open(relay, Connector.READ_WRITE);
<             conn.setRequestMethod(HttpConnection.POST);
<             conn.setRequestProperty("Connection", "close");
<             if (outgoing == Message.EMPTY) {
<                 conn.setRequestProperty("Content-Length", "0");
<             } else {
<                 conn.setRequestProperty("Content-Length",
<                         Integer.toString(outgoing.getSize()));
<                 conn.setRequestProperty("Content-Type", DEFAULT_MIME_TYPE);
<                 //System.out.println ("send Con-Len " + outgoing.getSize());
<                 dos = conn.openDataOutputStream();
---
> 
> 			final ByteArrayOutputStream baos = new ByteArrayOutputStream();
> 
> 			postMethod = new PostMethod(relay);
> 
> 			if (outgoing != Message.EMPTY) {
> 				dos = new DataOutputStream(baos);
279a269,273
> 
> 				postMethod.setRequestEntity(new ByteArrayRequestEntity(baos
> 						.toByteArray(), DEFAULT_MIME_TYPE));
> 
> 				responseCode = httpClient.executeMethod(postMethod);
281,285c275,279
<             if (conn.getResponseCode() != HttpConnection.HTTP_OK &&
<                     conn.getResponseCode() != 100) {
<                 throw new IOException("HTTP Error: " +
<                         conn.getResponseCode() + " " +
<                         conn.getResponseMessage());
---
> 
> 			if (responseCode != 100 && responseCode != 200) {
> 				throw new IOException("HTTP Error: "
> 						+ postMethod.getStatusCode() + "/"
> 						+ postMethod.getStatusLine());
287,288c281,282
<             System.out.println("recv Con Length: " + conn.getLength());
<             if (conn.getLength() <= 0) {
---
> 
> 			if (postMethod.getResponseContentLength() <= 0L)
290d283
<             }
292c285
<             dis = conn.openDataInputStream();
---
> 			dis = new DataInputStream(postMethod.getResponseBodyAsStream());
297,300c290,293
<             if (conn != null) {
<                 conn.close();
<             }
<             if (dos != null) {
---
> 			if (postMethod != null)
> 				postMethod.releaseConnection();
> 
> 			if (dos != null)
302,303c295
<             }
<             if (dis != null) {
---
> 			if (dis != null)
306d297
<         }
codigo/java/jxme-4-se.txt · Última modificação: 2010/03/09 21:47 (edição externa)
Voltar ao topo
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0