by Emmanuel Proulx
Session Initiation Protocol (SIP) is an important signaling protocol that the telecommunications industry is rapidly moving to for next-generation applications. Java is an excellent platform for SIP development, especially on the server side. Similar to HTTP servlets, the SIP Servlet API allows easier development of SIP services. This article describes SIP servlet technology and provides an annotated example.
Instant messaging is changing many people's lives. It is an invaluable tool that combines the best of email, Internet phone, and file transfer applications. I can even see who's online and who's busy. Sure, it can be used for countless hours of unproductive chatting. But it also allows me to give my boss that vital information he needs while he's in the middle of a meeting with a customer.
So it's no wonder that there are so many different instant messaging applications on the market. Having so many choices is a good thing, right? But what if I'm using a different application from the one my boss is using? I'd be in big trouble because most of these applications use proprietary protocols.
Good thing SIP came along. SIP will most probably become the standard instant messaging protocol.
In this article, I develop a simple SIP application—a chat room server that allows SIP instant messengers to connect with each other and broadcast messages to others.
SIMPLE, an acronym for SIP Instant Messaging and Presence Leveraging Extension, is a working group as well as a set of SIP extensions. One of these extensions is the MESSAGE message. You can use it to send instant messages with any combination of text and binary content. This message is extremely simple to use, which is why I decided to use it to develop our first SIP application.
To test our application, I provided a tiny SIP instant messenger application. (See the Download section at the end of the article.) This application sends MESSAGE messages to other messengers or servers. The user interface contains the client's address, and input fields for the buddy's address, a text message, and a submit button. Figure 1 shows the TextClient running.
Figure 1. TextClient in action
To start the TextClient, use this command:
java -jar textclient.jar dev2dev.textclient.TextClient
This command uses the JAIN SIP API reference implementation as a SIP stack. The source code is provided for this tool; if you want to learn more, I invite you to have a look.
Here are the requirements for our example:
A chat room is a virtual location where many instant messenger applications can talk to each other. Messages that arrive in a chat room are broadcast to all other people in the room. In other words, all messages are seen by all users. This means when a message arrives at our server application, the user's address will be added to a list. Then the message will be sent to all users in that list.
Additionally, you could implement "commands." A command begins with a forward slash (/) and is not broadcast, but instead is processed by the server itself for special features. The commands I'll implement are:
/join: Silently enter a chat room without broadcasting anything.
/who: Print the list of all users in this chat room.
/quit: Leave the chat room; no more messages will come in.
The SIP Servlet API (
JSR 116) is a server-side interface describing a container of SIP components or services. This is ideal for developing the ChatRoomServer.
Download the specification and decompress it. The produced folder contains libraries (
sipservlet.jar), and documentation. I couldn't get the Reference Implementation to run the example SIP servlet, so I don't think you should bother getting it.
The most central concept of SIP servlet is containment. A SIP service is a packaged up SIP servlet deployed (running on top of) a container or SIP application server. The container provides many services that your application can take advantage of, such as automatic retries, message dispatching and queuing, forking and merging, and state management. Your application only contains high-level message-handling and business logic. This makes the development of SIP services a breeze.
It is not this article's objective to teach you everything there is to know about SIP Servlet API technology. I only provide a brief introduction to this API and example code. For more information, consult the reference section at the end of this article.