• No results found

A basic Java Jabber server

IM concepts and Jabber protocols

3.1 A basic Java Jabber server

If you have ever written code for a server, the basic design of a Jabber server will be very familiar to you. If not, don’t worry. Server software, in its basic form, is surpris- ingly simple. Most of the cost, complexity, and work that goes into large-scale server software lies in adding extra features that allow the server to handle large numbers of users simultaneously and to do so in a secure, reliable manner.

The Jabber server in this book is designed to be as simple as possible while implementing the Jabber protocols. This simplicity makes it easy to understand and modify-—two features that for our purposes (learning) are more important than the typical server features that interest programmers. Once you understand the Jabber software, you can easily modify it to add features as you need them.

Our simple Jabber server does three things:

■ Handles network connections with Jabber clients ■ Reads and writes Jabber XML streams

Reacts to input from Jabber clients

Surprisingly, we can create a server to do these things in just a few Java classes. In this section, we’ll create a basic Jabber server that carries out all three tasks.

3.1.1 Goals for our server

Before writing a piece of software, it helps to have a clear set of project goals. Goals provide general guidelines that drive design and implementation decisions. For the Jabber server we’ll use the following design goals:

Simple—Simple things appeal from a purely aesthetic point of view. In addi-

tion, simple software tends to be more robust, easier to understand, and eas- ier to modify. The latter two are also project goals we’ll cover later. By keeping things simple, we have a head start on meeting our other goals as well.

Standards compliant—We’re developing the Jabber server software to explain

and illustrate the Jabber protocols. Obviously we want to make sure that it implements the Jabber protocols correctly or it loses its educational value. In addition, compliant servers will be interoperable with the significant amount of Jabber software available today.

A basic Java Jabber server 61

Easy to understand—The software must be easy to understand. It is difficult

to learn from confusing software and it is even harder to explain it well. The clearer the software the less work both of us have to do.

Easy to modify—Although this software is an educational tool, hopefully

some of you will want to use it in the real world. In most cases, you will need to modify it to meet your needs (its present form is severely limited in capa- bility). We should make it as easy as possible to modify while still meeting our other goals.

It is also vital that we establish what the server is not trying to do. The Jabber server software in this book is not intended to be secure, scalable, manageable, robust (transactional), efficient, or fast. On a reasonably fast machine the server should be able to provide non-mission-critical IM to about 20 users: perfect for a small workgroup but not enough for most small- to medium-sized businesses.

3.1.2 Our server software

The server software in this chapter will accept and handle incoming client con- nections, parse an XML stream over that connection, and react to Jabber com- mands sent over the XML stream. The software will be extremely limited to start, but as we cover new Jabber protocols code supporting them will be added to the server, expanding the server’s features and capabilities.

All of the software in this book is available for download from the book’s web site at Manning Publications (www.manning.com/shigeoka). I’m releasing the software under a modified Apache (BSD) license. The license permits you to do almost anything you want with the software including copying it, selling it, or incorporating it into commercial products. Send a note through the book’s web- site, letting me know what you’re doing with the software.

There are several interesting directions that you can take the software includ- ing implementing the advanced Jabber features. Covered in chapter 10, they include implementing server security, embedding Jabber functionality into exist- ing applications, or integrating it into other enterprise resources.1 In addition, the server is useful as a normal Jabber server for small workgroups. You may want to work on optimizations in I/O (input/output), threading, parsing, distributed processing, management/administration, transactions/journaling, size, space, or

1 These bridges to enterprise resources are often referred to as middleware, and Jabber’s use in this area

62 CHAPTER 3

IM concepts and Jabber protocols

memory consumption. The last three optimizations are very important when Jab- ber is used in small, resource-constrained, embedded systems.2

3.1.3 The basic server design

My Jabber server is broken up into three basic functional modules:

Session pool—A collection of Jabber client/server sessions. Each session

manages a java.net.Socket connection and metainformation such as the

associated session ID and Jabber ID. A session index allows other server modules to locate a session by Jabber ID.

XML parser—The XML parsing classes handle the incoming Jabber XML data

and transform it into Java packet objects that can be used by the Jabber ser ver. The XML parsing classes rely on the Simple API for XML (SAX) parser in the Xerces XML Java library.

Packet handlers—The server reacts to incoming packets. In most cases, the

server simply routes packets to a particular delivery endpoint. However many packets must be processed by the ser ver, generating one or more reply packets.

The modules are joined together by a PacketQueue that holds Jabber Packet

objects:

PacketQueue—This class is a basic queue data structure for Packet java

objects.3 The PacketQueue joins the packet handlers to the XML parser module by allowing the XML parser to deposit Packet objects into the

PacketQueue. The packet handlers remove Packet objects from the PacketQueue and handles them. The PacketQueue is designed to be thread-safe allowing separate ser ver threads to work with the Packet- Queue simultaneously.

Jabber packets—The Packet class stores information about the Jabber XML

data packets that are sent between a Jabber client and server. The general architecture is shown in figure 3.1.

2 Who would run a Java Jabber server in an embedded system? There is a whole category of embedded

systems called “home gateways” or “digital hubs” that manage home networks, primarily for automation and entertainment. Many run Java and could benefit from embedded Jabber servers for communica- tion and presence.

3 A queue is a basic first-in, first-out (FIFO) list. Items placed into the queue are taken from it in the order

The session pool maintains client connections 63

Figure 3.1 The basic server modules joined by the PacketQueue

The basic server operation begins when a client connects to the Jabber server. The server will create a session object for the connection and start an XML parser reading incoming XML. The XML parser generates Java Packet objects that are pushed onto the packet queue. A set of packet handlers takes packets from the queue, processes them as necessary, generates XML for outgoing packets, uses the session index to find the correct Session object’s outgoing XML stream, and writes the outgoing XML to that stream.

The session pool, XML parser, and packet queue work together to support pro- cessing Jabber packets. We will implement the various Jabber protocols in the packet handler classes. For now we will provide rudimentary routing for all incoming packets. As we cover new Jabber protocols we’ll add more packet handlers.