/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.sip;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.sip.*;
import javax.sip.address.*;
import javax.sip.header.*;
import javax.sip.message.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
/**
* Contains all functionality that has anything to do with registering and
* maintaining registrations with a SIP Registrar.
*
* @todo make sure that every time we change our state to unregistered we put
* the proper state code.
*
* @author Emil Ivov
*/
public class SipRegistrarConnection
implements SipListener
{
private static final Logger logger =
Logger.getLogger(SipRegistrarConnection.class);
/**
* A reference to the sip provider that created us.
*/
private ProtocolProviderServiceSipImpl sipProvider = null;
/**
* The SipURI containing the address port and transport of our registrar
* server.
*/
private SipURI registrarURI = null;
/**
* The InetAddress of the registrar we are connecting to.
*/
private InetAddress registrarAddress = null;
/**
* The default amount of time (in seconds) that registration take to
* expire or otherwise put - the number of seconds we wait before re-
* registering.
*/
public static final int DEFAULT_REGISTRATION_EXPIRATION = 3600;
/**
* The amount of time (in seconds) that registration take to expire or
* otherwise put - the number of seconds we wait before re-registering.
*/
private int registrationsExpiration = DEFAULT_REGISTRATION_EXPIRATION;
/**
* Keeps our current registration state.
*/
private RegistrationState currentRegistrationState
= RegistrationState.UNREGISTERED;
/**
* The timer we use for rescheduling registrations.
*/
private Timer reRegisterTimer = new Timer();
/**
* A copy of our last sent register request. (used when unregistering)
*/
private Request registerRequest = null;
/**
* The next long to use as a cseq header velue.
*/
private long nextCSeqValue = 1;
/**
* The client transaction that we used for sending the last REGISTER
* request.
*/
ClientTransaction regTrans = null;
/**
* Creates a new instance of this class.
*
* @param registrarAddress the ip address or FQDN of the registrar we will
* be registering with.
* @param registrarPort the port on which the specified registrar is
* accepting connections.
* @param registrationTransport the transport to use when sending our
* REGISTER request to the server.
* @param expirationTimeout the number of seconds to wait before
* reregistering.
* @param sipProviderCallback a reference to the
* ProtocolProviderServiceSipImpl instance that crated us.
*
* @throws ParseException in case the specified registrar address is not a
* valid reigstrar address.
*/
public SipRegistrarConnection(InetAddress registrarAddress,
int registrarPort,
String registrationTransport,
int expirationTimeout,
ProtocolProviderServiceSipImpl sipProviderCallback)
throws ParseException
{
this.sipProvider = sipProviderCallback;
this.registrarAddress = registrarAddress;
registrarURI = sipProvider.getAddressFactory().createSipURI(
null, registrarAddress.getHostName());
if(registrarPort != ListeningPoint.PORT_5060)
registrarURI.setPort(registrarPort);
registrarURI.setTransportParam(registrationTransport);
this.registrationsExpiration = expirationTimeout;
//now let's register ourselves as processor for REGISTER related
//messages.
sipProviderCallback.registerMethodProcessor(Request.REGISTER, this);
}
/**
* Sends the REGISTER request to the server speciied in the constructor.
*
* @throws OperationFailedException with the corresponding error code
* if registration or construction of the Register request fail.
*/
void register()
throws OperationFailedException
{
setRegistrationState(RegistrationState.REGISTERING,
RegistrationStateChangeEvent.REASON_NOT_SPECIFIED,
null);
//From
FromHeader fromHeader = null;
try
{
fromHeader = sipProvider.getHeaderFactory().createFromHeader(
sipProvider.getOurSipAddress(), ProtocolProviderServiceSipImpl
.generateLocalTag());
}
catch (ParseException ex)
{
//this should never happen so let's just log and bail.
logger.error(
"Failed to generate a from header for our register request."
, ex);
setRegistrationState(RegistrationState.CONNECTION_FAILED
, RegistrationStateChangeEvent.REASON_INTERNAL_ERROR
, ex.getMessage());
throw new OperationFailedException(
"Failed to generate a from header for our register request."
, OperationFailedException.INTERNAL_ERROR
, ex);
}
//Call ID Header
CallIdHeader callIdHeader
= this.getRegistrarJainSipProvider().getNewCallId();
//CSeq Header
CSeqHeader cSeqHeader = null;
try
{
cSeqHeader = sipProvider.getHeaderFactory().createCSeqHeader(
getNextCSeqValue(), Request.REGISTER);
}
catch (ParseException ex)
{
//Should never happen
logger.error("Corrupt Sip Stack", ex);
setRegistrationState(RegistrationState.CONNECTION_FAILED
, RegistrationStateChangeEvent.REASON_INTERNAL_ERROR
, ex.getMessage());
throw new OperationFailedException(
"Failed to generate a from header for our register request."
, OperationFailedException.INTERNAL_ERROR
, ex);
}
catch (InvalidArgumentException ex)
{
//Should never happen
logger.error("The application is corrupt", ex);
setRegistrationState(RegistrationState.CONNECTION_FAILED
, RegistrationStateChangeEvent.REASON_INTERNAL_ERROR
, ex.getMessage());
throw new OperationFailedException(
"Failed to generate a from header for our register request."
, OperationFailedException.INTERNAL_ERROR
, ex);
}
//To Header (Equal to the from header in a registration message.)
ToHeader toHeader = null;
try
{
toHeader = sipProvider.getHeaderFactory().createToHeader(
sipProvider.getOurSipAddress(), null);
}
catch (ParseException ex)
{
logger.error("Could not create a To header for address:"
+ fromHeader.getAddress(),
ex);
//throw was missing - reported by Eero Vaarnas
setRegistrationState(RegistrationState.CONNECTION_FAILED
, RegistrationStateChangeEvent.REASON_INTERNAL_ERROR
, ex.getMessage());
throw new Ope
Sip注册 Java实现
4星 · 超过85%的资源 需积分: 49 63 浏览量
2009-04-08
16:10:15
上传
评论 5
收藏 14KB RAR 举报
strive4java
- 粉丝: 2
- 资源: 10
- 1
- 2
- 3
- 4
- 5
- 6
前往页