[转]Introduction to JAIN SIP API part3_FINAL
2016-12-08 17:23
357 查看
[本文转自http://www.oracle.com/technology/pub/articles/dev2arch/2007/10/introduction-jain-sip3.html]
Receiving a Response
Earlier, you registered a listener of incoming messages. The listener interface,SipListener, contains the method
processResponse(), which is called by the SIP stack when a SIP response message arrives.
processResponse()takes a single parameter of type
ResponseEvent, which encapsulates a
Responseobject. Let's implement this method now.
public void processResponse(ResponseEvent evt) { Response response = evt.getResponse(); int status = response.getStatusCode(); if( (status >= 200) && (status < 300) ) { //Success! messageProcessor.processInfo("--Sent"); return; } messageProcessor.processError("Previous message not sent: " + status); }
In this method you check if the response of an earlier MESSAGE message represents a success (2xx range of status codes) or an error (otherwise). You then relay this information back to the user through the callback interface.
Typically, you read only the
Responseobject in the
processResponse()method. The only exception is for a success response to an INVITE message; in this case, you must send an ACK request right back, like this:
Dialog dialog = evt.getClientTransaction().getDialog() Request ack = dialog.createAck() dialog.sendAck( ack );
For a description of the
Responseinterface, refer to the
Appendix.
Receiving a Request
Receiving a SIP request message is just as easy as receiving a response. You just implement another method of theSipListenerinterface,
processRequest(), and the SIP stack will call it automatically. The single parameter of this method is a
RequestEventobject, which contains (you guessed it) a
Requestobject. This is the same type that you've seen before, and it has the same methods. However, you shouldn't set any fields on an incoming request as it doesn't make much
sense.
A typical implementation of
processRequest()analyzes the request, and then creates and sends back an appropriate response. This is what you'll do now:
public void processRequest(RequestEvent evt) { Request req = evt.getRequest(); String method = req.getMethod(); if( ! method.equals("MESSAGE")) { //bad request type. messageProcessor.processError("Bad request type: " + method); return; } FromHeader from = (FromHeader)req.getHeader("From"); messageProcessor.processMessage( from.getAddress().toString(), new String(req.getRawContent())); Response response=null; try { //Reply with OK response = messageFactory.createResponse(200, req); ToHeader toHeader = (ToHeader)response.getHeader(ToHeader.NAME); toHeader.setTag("888"); //Identifier, specific to your application ServerTransaction st = sipProvider.getNewServerTransaction(req); st.sendResponse(response); } catch (Throwable e) { e.printStackTrace(); messageProcessor.processError("Can't send OK reply."); } }
In this case, you always reply with a success response (200), but you could also send back any of the error responses (typicall
4000
y 4xx range). Here's a useful list of
SIP status codes.
Dealing With Error Conditions
There are other methods in theSipListenerinterface that you haven't implemented yet. They are called by the SIP stack when a request cannot be sent for specific reasons. For example, the
processTimeout()is called when the end point receiving the message doesn't answer in time. This is a special situation for which there is no response, so no
Responseobject is available. The
TimeoutEventparameter contains, among other things, the
ClientTransactionof the request that timed out, and you can use this to link back to the original request if you want to. In this implementation you simply inform the user using the callback interface:
public void processTimeout(TimeoutEvent evt) { messageProcessor.processError("Previous message not sent: " + "timeout"); }
Similarily, Input/Output (IO) errors are processed using the following method:
public void processIOException(IOExceptionEvent evt) { messageProcessor.processError("Previous message not sent: " + "I/O Exception"); }
Point-to-Point vs. Client/Server
SIP client applications can be used standalone (point to point) or together with a server to provide extra functionality like proxying or call routing.I suggest you have a look at my Aarticle on SIP Servlets. It contains a neat SIP Server application that can work with TextClient to provide a chat room-type service. This illustrates how you can use the TextClient together with the BEA
WebLogic SIP Server and double its usefulness.
Download
Download the TextClient source code here.Summary
This article provides an overview of the JAIN SIP API, and I've shown how to write a simple application to use this technology. Right now, you should have a pretty good idea of the APIs available, and know how to write your own IM clientwith SIP.
Nevertheless, why stop here? I could add many more features to this application. And as I said before, if the client talks with a server application, you can double its usefulness. If you need suggestions, consider the following:
Automatic text answers, store-and-forward (for example, "John is offline right now, but he will receive your messages as soon as he logs back in")
A neat networked checkers video game
A location-based service for your laptop
A media-sharing client
An RSS-like client
The possibilities are almost limitless.
References
A concisetutorial, including SIP message formats. Be sure to read the section "Relation among Call, Dialog, Transaction & Message" (external tutorial)
Standard SIP stack properties
Non-standard SIP stack properties of the reference implementation
SIP status codes
RTP stack
SIP Communicator (java.net project)
Appendix
This section is a reference to the various classes and interfaces available in the JAIN SIP API.API overview
Here's an overview of the main classes and interfaces found in the JAIN SIP API reference implementation.Class / InterfaceDescription
SipFactory / AddressFactory / HeaderFactory / MessageFactory | Factory classes to create the various objects of the system. They return objects that implement standard interfaces. |
SipStack | The first interface you'll need, used to create ListeningPoints and SipProviders. |
ListeningPoint | This interface encapsulates a transport/port pair (for example, UDP/5060). |
SipProvider | This interface is used to send SIP messages. You can also register a listener for incoming SIP messages using this interface. SeeSipListenerbelow. |
SipListener | You must implement this interface to allow receiving incoming SIP messages. |
RequestEvent / ResponseEvent | Represent an incoming SIP request, response. Passed to your SipListenerfor processing. Contains a Requestor Responseobject, respectively. |
TimeoutEvent | Represents a failure condition when there's no reply to an outgoing request. Passed to yourSipListenerfor processing. |
IOExceptionEvent | Represents a failure condition when there's an Input/Output problem sending an outgoing request. Passed to yourSipListenerfor processing. |
Request / Response | Represent a SIP request, response. Both are sub-interfaces of the Messageinterface. They provide access to headers, content, and other parts of SIP messages. |
Dialog | An object of this interface encapsulates a SIP dialog. (Reminder: In a dialog all messages are related to the same call; a dialog often starts with an INVITE and ends with a BYE.) |
ClientTransaction / ServerTransaction | Encapsulate SIP transactions. (Reminder: A transaction starts with a request and ends with a final response. Transactions often live within a dialog.) |
Message
Interface
The Messageinterface is the base interface for SIP messages. For your reference, here's an overview of available methods.
MethodDescription
void addHeader(Header) void setHeader(Header) | Sets header fields to the SIP message. The first method can be used for headers that are repeatable or can have multiple values, like the Contact header. The second method removes existing headers of this type and then adds a single header value. |
void removeHeader(Header) | Removes existing headers of this type. |
ListIterator getHeaderNames() | Returns all header names. |
ListIterator getUnrecognizedHeaders() | Returns header names for non-standard header types. |
Header getHeader(String) ListIterator getHeaders(String) | Getters for specific headers. The second form returns all values of repeatable headers, or headers with multiple values, like the Contact header. |
void setContent(Object, ContentTypeHeader) | Sets the payload of the message, as well as the Content-Type header. Content-Length is also set if the type is a string, else usevoid setContentLength(ContentLengthHeader). |
byte [] getRawContent() Object getContent() | Retrieves the payload of the message. |
void removeContent() | Empties the payload. |
void setContentLength(ContentLengthHeader) ContentLengthHeader getContentLength() void setContentLanguage(ContentLanguageHeader) ContentLanguageHeader getContentLanguage() void setContentEncoding(ContentEncodingHeader) ContentEncodingHeader getContentEncoding() void setContentDisposition(ContentDispositionHeader) ContentDispositionHeader getContentDisposition() | Special payload-related header accessors. Rarely used. |
void setExpires(ExpiresHeader) ExpiresHeader getExpires() | Manages the Expires header. |
void setSipVersion(String) String getSipVersion() | Accessors for the SIP version element. Rarely used, defaults to SIP/2.0. |
Object clone() | Creates a copy of the message. Rarely used. |
Request
interface
Now let's glance through the Requestinterface (sub-interface of
Messageabove):
MethodDescription
String getMethod() void setMethod(String) | Accessors for the method element. Can be any SIP method, including those in the constants of theRequestinterface: ACK, BYE, CANCEL, INVITE, OPTIONS, REGISTER, NOTIFY, SUBSCRIBE, MESSAGE, REFER, INFO, PRACK, and UPDATE. |
URI getRequestURI() void setRequestURI(URI) | Accessors for the request URI, which is the first line of a SIP request. Typically, this is an instance ofSipURI. |
Response
interface
The Responseinterface also extends the
Messageinterface:
MethodDescription
void setStatusCode() int getStatusCode() | Accessors for the status code. This can be any SIP status code, including those in the constant members of theResponseinterface. Here are a few of them: RINGING (180), OK (200), BAD_REQUEST (400), and so on. |
void setReasonPhrase(String) String getReasonPhrase() | Accessors for the human-readable explanation of the status code. |
相关文章推荐
- An Introduction to the JAIN SIP API
- [转]Introduction to JAIN SIP API part1
- [转]Introduction to JAIN SIP API part2
- An Introduction to the Java Logging API (part)
- VoIP in-depth: An introduction to the SIP protocol, Part 1
- VoIP in-depth: An introduction to the SIP protocol, Part 1-2
- VoIP in-depth: An introduction to the SIP protocol, Part 2
- VoIP in-depth: An introduction to the SIP protocol, Part 2 , 3-4
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- Introduction to Nutch, Part 2: Searching
- The ultimate Managed DirectSound 9 Tutorial. Part 1: a full introduction to Playback
- An basic introduction to SIP
- Introduction to ASP.NET Web API
- Introduction To The Smart Client Software Factory (CAB/SCSF Part 18)
- Introduction to Neural Machine Translation - part 1
- Introduction to Networking: Part 1
- SCSF - Part 3 Introduction to Dependency Injection
- Building Telephony Systems With OpenSIPS---Introduction to SIP
- Recurrent Neural Networks Tutorial, Part 1 – Introduction to RNNs
- Axure RP Pro - 相关问题 - Introduction to Version 5.5 Features: Part 3