Serve clients’ specific protocol requirements with Brazil, Part 4
Build multicast-aware apps with JRMS
So far in this series, I have discussed how to meet client protocol requirements utilizing Brazil technology. You have learned how to use Java applets, JavaScript, BSL, plain text, Palm query applications, and WML (Wireless Markup Language) to deliver data to recipients using any browser or device. In Part 4, I’ll introduce the Java Reliable Multicast Service (JRMS) API, which allows you to develop multicast-aware applications.
Read the whole series on Brazil technology:
- Part 1: Learn how to build an application server that can deliver data to clients requiring different protocols
- Part 2: How to support XML applications with the Brazil project
- Part 3: Economically sustain PQA, UP.SDK, and J2ME with the Brazil project
- Part 4: Build multicast-aware apps with JRMS
- Part 5: Manage users and content with Brazil
- Part 6: Plug Jini, BeanShell, and JAXM into Brazil
The multicast protocol supports simultaneous message transmission to multiple recipients, without having a point-to-point connection with each recipient. The JRMS from Sun Microsystems provides an implementation for building multicast-aware applications. Multicast decreases bandwidth consumption and, with minimal programming and coordination, allows developers to deploy applications in new ways using channels that have current data. Multicast is particularly valuable for satellite providers that use fast, scarce bandwidth to provide news, stock, video, game, and general replication update feeds over single channels to entire cities.
Technologies like JRMS offer the basis to achieve multicast-aware applications. In this article, I will introduce some examples of JRMS that you can use in your own network. Then I will provide sample code for a Brazil handler that creates a weather multicast channel; users can tune to it and select different sources of weather information.
After understanding these small examples, you should be able to develop applications that are multicast-aware — network topology permitting.
What is multicast?
As mentioned above, the multicast protocol sends a message to multiple receivers, but does not establish a point-to-point connection with each receiver. For example, you could multicast n radio stations on n ports for a large population of Internet users.
Multicast scales without packed duplication; unicast does not. Net applications are mostly unicast. For example, most audio and video applications open point-to-point connections between the client and the server. So 1 million users employing a unicast approach require 1 million different ports, and each packet must be duplicated 1 million times. Using the multicast approach, 1 million users could receive that data using only a fraction of those ports.
Some efforts involving multicast protocols have not yet taken off, like Multicast Backbone (MBONE). (See Resources for a link.) Many vendors of applications and services in the video, audio, and generic data feed market do not use multicast. Their applications often establish point-to-point connections between the clients and servers. Multicast is not something you just use in your application; multicast protocols require support at the protocol stack level and in the network infrastructure.
Multicast is not yet widely available, so the Internet cannot efficiently deliver the same information to many users. As networking technology improves and bandwidth increases, multicast-aware applications may become more common.
What is JRMS?
The JRMS developers page states that the JRMS project mission is to:
Create a network service which enables building multicast-aware applications that distribute information over IP networks.
Sun Labs technical report #TR-98-68 provides a technical explanation of what multicast is and how it can be used (see Resources for link to report).
JRMS examples from Sun JRMS team
Download the JRMS API from Sun and try a few of the examples. I suggest testing the examples on two machines, but if you only have one, you can run the code on your computer using two windows.
Sun’s JRMS team provided the examples in the table below. You can run them from various command line shells, such as Unix terminal windows or DOS windows. It can be instructive to see the packets going out on the network; use your favorite TCP/IP analysis tool to display the packets. Windows developers should consider downloading the Cygwin tools to create a more powerful command window-scripting environment. (See Resources.)
The examples in the following table use either lightweight reliable multicast protocol (LRMP) or tree-based reliable multicast protocol (TRAM). LRMP is an implementation that provides multipoint-to-multipoint reliable and ordered delivery for multiple senders. TRAM provides large-scale point-to-multipoint delivery for single senders. Consult the JRMS Website for more details on these protocols (see Resources for a link).
JRMS examples
Directory | Description |
---|---|
stock |
Source code for the sample stock ticker application, which uses TRAM to periodically send stock information from one sender (StockServer ) to multiple receivers (StockViewer ). The server requests information from www.yahoo.com and parses it before multicasting to the receivers. |
slingerSwing |
Sample file-transfer application with GUI and command line interfaces. |
chat |
This application uses LRMP in a multisender chat application. |
testtools |
Testing programs — start with JRMStest . |
msendreceive |
Simple example that provides Sender and Receiver using MulticastSocket and joinGroup . |
mctest |
This application uses reliable multicast to stream data from a sender to multiple receivers. |
TreeTest |
The TreeTest package is a simple GUI application that simulates a set of network nodes building a TRAM repair tree. TreeTest can be invoked with a configuration file as an argument, or the user can manually add nodes. Once the nodes are in place, TRAM may be started. Multicast messages display on the screen as expanding circles. It is recommended that you turn off the display when simulating large numbers of nodes; it takes up extra processing time. |
You can find documentation for the JRMS APIs online. (See Resources for a link.)
Brazil technology and JRMS
Real-time weather data from the one-wire weather station is one of the data sources for this Brazil series. Weather data is a particularly good candidate for multicast because there is no shortage of it, the data is the same for everyone, and users want it constantly updated. In fact, many telemetry applications could benefit from the use of multicast channels.
The JRMS API is easy to use, so adding support for both TRAM and LRMP to Brazil is not complicated.
The weather handler (see Resources) simply creates a JRMS channel and uses a thread with requests to a JRMS weather object, which is called when new weather data becomes available. The weather object serializes the weather data and sends it out the channel. Clients that have posted a read on the channel receive the data and display it. The following code segment in the Weatherhandler.java
file creates the channel:
try {
Class c = Class.forName("com.sun.multicast.allocation.Address");
dbg("JRMS starting");
jrms = new WeatherJRMSServer();
jrms.createChannel(jrmsIP, jrmsPort);
server.log(Server.LOG_WARNING, null, prefix + ". JRMS available");
} catch (ClassNotFoundException notFound) {
notFound.printStackTrace();
server.log(Server.LOG_WARNING, null,
prefix + ". JRMS not available");
return (true);
} catch (Exception e) {
server.log(Server.LOG_WARNING, null,
prefix + ". JRMS not started " + e.getMessage());
return (false);
}
First, make sure that you have the required JRMS class files. The WeatherHandler
starts up several different services so that if the JRMS class library is unavailable, the other services still operate. The WeatherJRMSServer
object contains methods for creating a channel and preparing data for transmission. The jrmsIP
and jrmsPort
variables identify the multicast IP address and the port number to use for communication. Clients will look for data at this pair of address ports. For the example above, the values of 224.10.10.10 and 4343 are used. Multicast addresses are allocated from certain parts of the IP address space. (See Resources for more information.)
Once you start the service and create a channel using the aforementioned technique, you are ready to write some data to the channel.
The following code segment writes a property object to multicast clients:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos);
os.writeObject(p);
os.flush();
bos.close();
if (jrms != null) {
if (!jrms.sendDataPacket(bos.toByteArray())) {
server.log(Server.LOG_DIAGNOSTIC, null, "No members");
}
}
The above code segment is in the thread that waits for new data from the WeatherStation
. The segment checks for a server object; if one exists, the code segment serializes a property object that contains properties like weather.sample.temperature=105
. The serialized property object is written to a byte stream. The sendDataPacket()
method then transmits those bytes as a multicast packet.
Server
The server object that transmits weather data to JRMS clients supports either TRAM or LRMP on the created channel; you can use both on a port at one time. (See Resources for the annotated source for the server.) The two methods in WeatherJRMSServer
create a channel and send data over it: public void createChannel(String address, int port)
and public boolean sendDataPacket(byte data[])
.
The createChannel()
method supports either TRAM or LRMP. Use TRAM when you have a single sender and many receivers.
LRMP is better for smaller groups with more than one sender. The code to set up a sender using LRMP is:
lrmp = (LRMPTransportProfile) new LRMPTransportProfile(mcastAddress, port);
lrmp.setTTL((byte) 20);
ms = lrmp.createRMPacketSocket(TransportProfile.SEND_RECEIVE);
where ms
is of type RMPacketSocket
.
The code to send a packet is simple:
sendPacket = new DatagramPacket(data, data.length, mcastAddress, port);
ms.send(sendPacket);
Client as a Java application
The client application provides an easy-to-use GUI that allows users to select a particular weather station within a channel. The UI provides the following interface:
The above application lets users easily select different sources of weather information from the UI. Developers should also be able to provide historical, graphical, and analytical views of the data. For example, application functionality could be added to provide client-side processing of multicast data, which would offer more information. Or additional data, such as running averages, could be sent on the channel for all users.
JRMS (using LRMP) supports multiple senders and receivers. So all the senders could register to send weather packets with the description field and the corresponding latitude/longitude (LAT/LON) to ensure uniqueness. (For more information on LAT/LON, see Resources.) Receivers could then connect to these channels and select a sample. Over time, of course, channels could be developed that support specific areas around a central point. For example, a channel name could be a LAT/LON with a radius indicating what LATs/LONs the channel encompasses.
See Resources to download the source code for the client.
The following code segments demonstrate how to locate a channel and read data from it. The first segment creates a client using LRMP:
tp = new LRMPTransportProfile(InetAddress.getByName(address), port);
ms = tp.createRMPacketSocket(TransportProfile.RECEIVE);
The following code segment reads the data sent by the transmitter:
byte inPacket[] = recvPacket.getData();
ByteArrayInputStream bis = new ByteArrayInputStream(inPacket);
ObjectInputStream p = new ObjectInputStream(bis);
Properties props = (Properties) p.readObject();
A developer can read the data using recvPacket
. The transmitter has sent the data as a serialized property object. The received bytes are then used as input to an ObjectInputStream
so that the Property
object can be reconstituted. The transport layer has no knowledge of the data being transmitted in this application, so you create a Property
object from the received bytes. The rest of the program deals with program startup and shutdown, as well as a minimal user interface.
Run the JRMS code as an applet
Certain applications benefit from the use of an applet model of distribution, rather than an application model. The applet model allows users to dynamically download small portions of functionality, so installation is not required. However, since JRMS and many other APIs require security privileges and supporting class libraries, sometimes it makes sense to install the classfiles as a general resource for all browser-based applications. The JRMS weather client can be modified so that browser-based applications can send and receive multicast data. Recently, Sun developed additional technology to further simplify this process. (See Web Start in Resources.) Developers can use JRMS in applets by following these steps:
- Provide the correct functionality in
start()
,stop()
, andinit()
- Determine how jar files will be distributed; consider using Web Start
- Provide directions for users to install the Java Plug-in on IE and Netscape
- Determine security requirements, since a multicast-aware applet will require additional privileges
Conclusion
When using JRMS, you must first make sure that users have access to multicast packets. The easiest way to test that is to ensure that users can run a simple example using their protocol of choice, LRMP or TRAM. Do not assume that multicast will work on corporate intranets; the routers must be configured to support multicast. Many IT managers who are responsible for network infrastructures do not like to support multicast. To justify the use of JRMS in a corporate Internet environment, you may have to show IT managers the benefit of designing the application with and without multicast ability. Getting the entire Internet to support multicast is a much larger issue. Occasionally an ISP or ASP will support multicast technology, but you must make sure that all your users have similar abilities.
In these four articles, I have demonstrated how Brazil can easily support new protocols and interface them to existing protocols with minimal user effort. You can control startup, shutdown, and management of these services by providing URLs for the specific abilities desired.
For example, in the management area, JRMS provides statistics in the RMStatistics
object, which TRAM implements as TRAMStats
. These statistics can be made available as properties, which can be processed using the Brazil Scripting Language to create custom HTML reports.
We need a technology to support applications in the collaborative computing areas with streaming multimedia and real-time data delivery. These applications inherently require multipoint communication between one or more senders and many receivers. JRMS provides an API that allows developers to use multicast to develop multicast-aware applications. The fact that JRMS is written in Java provides further investment protection, as networks and bandwidth increase in devices, like cell phones, and expensive telemetry devices, like TINI boards.
Acknowledgements
I would like to thank Miriam Kadansky, Joe Provino, and Dah Ming Chiu of the JRMS team at Sun Microsystems Laboratories for reviewing this article and providing such a well-documented API with simple and complex examples.