내용출처 :
http://www.oracle.com/technology/pub/articles/dev2arch/2007/10/introduction-jain-sip.html
SIP Stack Preparation
Let's start writing the SipLayer class. TextClient must be able to receive asynchronous messages coming from other SIP end points. The observer pattern is used for this: The class implements the SipListener interface to process incoming messages:
public class SipLayer
implements SipListener {
The methods of this interface are:
void processRequest(RequestEvent evt);
void processResponse(ResponseEvent evt);
void processTimeout(TimeoutEvent evt);
void processIOException(IOExceptionEvent evt);
void processTransactionTerminated(TransactionTerminatedEvent evt);
void processDialogTerminated(DialogTerminatedEvent evt);
In this example, the most important methods evidently are processRequest() and processResponse() for processing incoming messages. I'll look at those a bit later.
Next are two fields to store objects needed later. These are not directly related to the SIP API, but you'll need them for the example. The first is a MessageProcessor object as discussed before. You also need to keep the username handy. These two fields have getters and setters which, for brevity, I'm not showing in this article.
private MessageProcessor messageProcessor;
private String username;
Next is the constructor. A typical way to start a JAIN SIP API application and TextClient follows this pattern is to set up a bunch of objects that will be useful later on. I'm talking about a number of factories, and a single SIP stack instance, initialized.
private SipStack sipStack;
private SipFactory sipFactory;
private AddressFactory addressFactory;
private HeaderFactory headerFactory;
private MessageFactory messageFactory;
private SipProvider sipProvider;
public SipLayer(String username, String ip, int port) throws
PeerUnavailableException, TransportNotSupportedException,
InvalidArgumentException, ObjectInUseException,
TooManyListenersException {
setUsername(username);
sipFactory = SipFactory.getInstance();
sipFactory.setPathName("gov.nist");
Properties properties = new Properties();
properties.setProperty("javax.sip.STACK_NAME", "TextClient");
properties.setProperty("javax.sip.IP_ADDRESS", ip);
sipStack = sipFactory.createSipStack(properties);
headerFactory = sipFactory.createHeaderFactory();
addressFactory = sipFactory.createAddressFactory();
messageFactory = sipFactory.createMessageFactory();
...
The SIP factory is used to instantiate a SipStack implementation, but since there could be more than one implementation, you must name the one you want via the setPathName() method. The name "gov.nist" denotes the SIP stack you've got.
The SipStack object takes in a number of properties. At a minimum, you must set the stack name. All other properties are optional. Here I'm setting an IP address to use by the stack, for cases where a single computer has more than one IP address. Note that there are standard properties, which all SIP API implementations must support, and non-standard ones that are dependent on the implementation. See the References section for links to these properties.
The next step is to create a pair of ListeningPoint and SipProvider objects. These objects provide the communication functionality of sending and receiving messages. There's one set of these for TCP and one set for UDP. This is also where you select the SipLayer (this) as a listener of incoming SIP messages:
...
ListeningPoint tcp = sipStack.createListeningPoint(port, "tcp");
ListeningPoint udp = sipStack.createListeningPoint(port, "udp");
sipProvider = sipStack.createSipProvider(tcp);
sipProvider.addSipListener(this);
sipProvider = sipStack.createSipProvider(udp);
sipProvider.addSipListener(this);
}
And this is how the constructor ends. You've just used the JAIN SIP API to create a SipStack instance, a bunch of factories, two ListeningPoints, and a SipProvider. These objects will be needed in the upcoming methods to send and receive messages.