FileDocCategorySizeDatePackage
RegistrationHelper.javaAPI DocphoneME MR2 API (J2ME)12001Wed May 02 18:00:42 BST 2007gov.nist.siplite

RegistrationHelper

public class RegistrationHelper extends Object implements SipListener, Runnable
A helper class that runs a registration call flow.

Fields Summary
private SipStack
sipStack
Current SIP stack context.
private SipProvider
sipProvider
Current SIP provider.
private MessageFactory
messageFactory
Message factory.
private HeaderFactory
headerFactory
HJeader factory.
private String
userName
Current user name.
private String
userAddress
Current user address.
private ListeningPoint
lp
Current event listening filter.
private Thread
myThread
Local thread for asynchoronous processing.
protected boolean
successfulRegistration
Flag indicating successful registraion.
private AuthenticationListener
authenticationListener
Credentials listener for authentication requests.
private int
countReoriginateRequest
Count of authorization requests (RFC 2617, 3.2.2).
Constructors Summary
public RegistrationHelper(SipStack myStack, String userName, String userAddress, ListeningPoint lp)
Constructor.

param
myStack current SIP stack context
param
userName current user name
param
userAddress current user address
param
lp listening point event filter

    
                                
      
                
              
        this.sipStack = myStack;
        this.userName = userName;
        this.userAddress   = userAddress;
        this.messageFactory  = new MessageFactory();
        this.headerFactory = new HeaderFactory();
        this.lp = lp;
        myThread = new Thread(this);
        // authenticationListener=new DigestClientAuthentication();
    
Methods Summary
public voiddoRegister()
Performs the session registration.

        myThread.start();
        //  Wait to register ourselves so we can receive messages.
        synchronized (this) {
            try {
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                        "WAIT");
                }
                
                this.wait();
                
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                        "WAKE UP");
                }
            } catch (InterruptedException ex) {
                return;
            }
        }
    
public voidprocessRequest(RequestEvent requestEvent)
Processes a request. (always logs a message that request is ignored)

param
requestEvent the current request

        // System.out.println("Ignoring request");
        
    
public voidprocessResponse(ResponseEvent responseEvent)
Process a response message.

param
responseEvent the transition event to be processed.

        Response response = responseEvent.getResponse();
        if (response.getStatusCode() == Response.OK) {
            this.successfulRegistration = true;
            
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "Registration listener : sending notify!");
            }
            
            synchronized (this) {
                // System.out.println("NOTIFY");
                this.notify(); //  Authentication done!
            }
        } else {
            //  Need to call out here to the Authentication listener.
            //  check if 401 or 407
            if (response.getStatusCode() == Response.
                    PROXY_AUTHENTICATION_REQUIRED ||
                    response.getStatusCode() == Response.UNAUTHORIZED) {
                Exception ex = null;
                try {
                    ClientTransaction clientTransac = responseEvent.
                            getClientTransaction();
                    
                    Request newRequest =
                            authenticationListener
                            .createNewRequest(sipStack,
                            clientTransac.getRequest(),
                            response, countReoriginateRequest);
                    
                    if (newRequest == null) {
                        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                            Logging.report(Logging.INFORMATION,
                                LogChannels.LC_JSR180,
                                "Authentication failed...");
                        }
                        return;
                    }
                    countReoriginateRequest ++;
                    
                    ClientTransaction ct =
                            sipProvider.getNewClientTransaction(newRequest);
                            
                    if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                        Logging.report(Logging.INFORMATION,
                            LogChannels.LC_JSR180,
                            "Got client Transaction " + ct);
                    }
                    
                    ct.sendRequest();
                    
                    if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                        Logging.report(Logging.INFORMATION,
                            LogChannels.LC_JSR180,
                            "RegistrationHelper: request sent:\n" + newRequest);
                    }
                    
                } catch (SipException se) {
                    ex = se;
                } catch (IOException ioe) {
                    ex = ioe;
                }
                if (ex != null) {
                    if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                        Logging.report(Logging.WARNING, LogChannels.LC_JSR180,
                            "RegistrationHelper: processResponse(),"
                                + " exception raised: "
                                + ex.getMessage());
                    }
                }
            }
        }
    
public voidprocessTimeout(TimeoutEvent timeoutEvent)
Process a tiomeout event.

param
timeoutEvent termination event based on timeout condition

        synchronized (this) {
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "NOTIFY");
            }
            
            this.notify(); //  Authentication done!
        }
        
    
public voidrun()
Starts asynchronous processing int separate thread.

        try {
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "starting registration thread");
            }
            
            sipStack.stackInitialized = false;
            Hop hop = sipStack.getRouter().getOutboundProxy();
            
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "got listening point");
            }
            
            sipProvider = lp.getProvider();
            StringBuffer requestLine =
                    new StringBuffer("REGISTER sip:")
                    .append(sipStack.getNextHop().getHost())
                    .append(":")
                    .append(sipStack.getNextHop().getPort())
                    .append(";transport="+hop.getTransport())
                    .append(" SIP/2.0\r\n");
            StringBuffer from  =
                    new StringBuffer("From: <sip:")
                    .append(userName)
                    .append("@")
                    .append(userAddress)
                    .append(">;tag=1234\r\n");
            StringBuffer to =
                    new StringBuffer("To: <sip:")
                    .append(userName)
                    .append("@")
                    .append(userAddress)
                    .append(">\r\n");
            String via =
                    lp.messageProcessor.getViaHeader().toString();
            int port = lp.getPort();
            
            StringBuffer contact = new
                    StringBuffer(
                    "Contact: <sip:"
                    + userName
                    + "@" + sipStack.getIPAddress()
                    + ":" + port
                    + ";transport=" + hop.getTransport()
                    + ">\r\n");
            
            CallIdHeader callId = sipProvider.getNewCallId();
            
            CSeqHeader cseq = new CSeqHeader();
            cseq.setMethod(Request.REGISTER);
            cseq.setSequenceNumber(1);
            
            MaxForwardsHeader maxForwards = new MaxForwardsHeader();
            maxForwards.setMaxForwards(1);
            
            String registerRequest =
                    new StringBuffer().append(requestLine)
                    .append(via).append(callId.toString())
                    .append(cseq.toString())
                    .append(maxForwards.toString())
                    .append(from).append(to).append(contact).
                    toString();
            
            // System.out.println(registerRequest);
            
            Request request = messageFactory.createRequest
                    (registerRequest);
            ClientTransaction ct =
                    sipProvider.getNewClientTransaction(request);
                    
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "Got client Transaction " + ct);
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "SENDING REGISTER TO THE PROXY");
            }
            
            ct.sendRequest();
        } catch (Exception ex) {
            if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                Logging.report(Logging.WARNING, LogChannels.LC_JSR180,
                    "Exception: " + ex);
            }
            
            synchronized (this) {
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                        "NOTIFY");
                }
                this.notify();
            }
        }