Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.tpcc;
  
  import javax.sip.*;
  import javax.sip.header.*;
  
 
 import java.util.*;
The Click to dial third party call controller flow IV application.

Author(s):
Kathleen McCallum
    main () -> init()
   init()
     createSipStack
     createInvite() -> First
   processResponse()
     if (OK) first
       ack() first
      createInvite() -> second no SDP
     else if (OK) second
        ack() -> second
        ack() -> first
 
 
 
 public class Controller4 implements SipListener {
 
     private static SipProvider sipProvider;
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint udpListeningPoint;
 
     protected ClientTransaction inviteFirst;
 
     protected ClientTransaction inviteSecond;
 
     private String currentState;
 
     String transport = "udp";
 
 
     protected static final String usageString = "java "
             + "examples.ctd.ctdControll \n"
             + ">>>> is your class path set to the root?";
 
 
     private Dialog firstDialog;
 
     private Dialog secondDialog;
     private static Logger logger = Logger.getLogger(Controller4.class);
 
     private String auser = "AGuy";
 
     private String aSipAddressDomain = "Afirst.com";
 
     private String aDisplayName = "The A first";
 
     private String buser = "BGuy";
 
     private String bSipAddressDomain = "BSecond.com";
 
     private String bDisplayName = "The B second";
 
     private String peerHostPortA = "127.0.0.1:5070";
 
     private String peerHostPortB = "127.0.0.1:5080";
 
     int first = 0, second = 0;
 
     private Response secondDialogOK;
 
 
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                .getServerTransaction();
        .info("\n\nRequest " + request.getMethod() + " received at "
                + .getStackName() + " with server transaction id "
                + serverTransactionId);
        // We are the Controller so the only request we get is the BYE.
        if (request.getMethod().equals(.))
            processBye(requestserverTransactionId);
    }
    public void processBye(Request request,
            ServerTransaction serverTransactionId) {
        try {
            .info("Controller4:  got a bye .");
            if (serverTransactionId == null) {
                .info("Controller4:  null TID.");
                return;
            }
            .info("Create OK para BYE: ");
            // 1: OK BYE
            Response ok = .createResponse(.request);
            serverTransactionId.sendResponse(ok);
            // 2do: BYE for the other side (send a new clientTransaction)
            .info("Send BYE in new clientTransaction");
            Dialog secondBye = (Dialog) (serverTransactionId.getDialog()
                    .getApplicationData());
            Request requestBye = secondBye.createRequest(.);
            ClientTransaction clientTransaction = null;
            clientTransaction = .getNewClientTransaction(requestBye);
            secondBye.sendRequest(clientTransaction);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public synchronized void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        ClientTransaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        .info("Response received : Status Code = "
                + response.getStatusCode() + " " + cseq);
        if (tid == null) {
            .info("Stray response -- dropping ");
            return;
        }
        .info("transaction state is " + tid.getState());
        .info("Dialog = " + tid.getDialog());
        .info("Dialog State is " + tid.getDialog().getState());
        try {
            if (response.getStatusCode() == .) {
                if (cseq.getMethod().equals(.)) {
                    if (.equals("first")) {
                        .info("processResponse FIRST");
                        // send ACK
                        this. = tid.getDialog();
                        Request ackRequest = .createAck(cseq
                                .getSeqNumber());
                        .info("Sending ACK firtInvite no media");
                        .sendAck(ackRequest);
                        // invite second no SDP
                        // get call-id
                        String callId = ((CallIdHeaderresponse
                                .getHeader(.)).getCallId();
                        // Create second Invite
                        ++;
                         = "second";
                        Request requestSecond = this.createInvite(,
                                String.valueOf(), callIdnull,
                                );
                         = 
                                .getNewClientTransaction(requestSecond);
                        .sendRequest();
                        .info("INVITE second sent:\n" + requestSecond);
                    } else if (.equals("second")) {
                        .info("processResponse SECOND");
                        // get offer of second
                        byte[] content = response.getRawContent();
                        ContentTypeHeader cth = (ContentTypeHeaderresponse
                                .getHeader(.);
                        // reinvite First
                        Request reinvite = .createRequest(.);
                        reinvite.removeContent();
                        reinvite.setContent(contentcth);
                        // Re-Invte offer2'
                        ClientTransaction ct = 
                                .getNewClientTransaction(reinvite);
                        .sendRequest(ct);
                        this. = tid.getDialog();
                        this. = response;
                        .info("RE-INVITE sent:\n" + reinvite);
                         = "re-invite";
                    } else if (.equals("re-invite")) {
                        .info("processResponse re-invite");
                        // send ack
                        CSeqHeader cseq2 = (CSeqHeaderthis..getHeader(.);
                        Request ackRequest = .createAck(cseq2
                                .getSeqNumber());// secondDialog.createRequest(Request.ACK);
                        .info("Sending ACK second " + );
                        .sendAck(ackRequest);// secondDialog.sendAck(ackRequest);
                        Request ackRequestFirst = this..createAck(
                                        cseq.getSeqNumber());
                        ackRequestFirst.setContent(response.getContent(),
                                (ContentTypeHeader) (response
                                        .getHeader("Content-Type")));
                        .info("Sending ACK first");
                        .sendAck(ackRequestFirst);
                        // save the dialog of the other side, for the bye...
                        .setApplicationData();
                        .setApplicationData();
                         = "fin";
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public Request createInvite(String headerNameString headerValue,
            String callerIdString tagValString peerHostPort)
            throws ParseExceptionInvalidArgumentException {
        String fromSipAddressDomain = ""toSipAddressDomain = "";
        String fromDisplayName = "";
        String toDisplayName = "";
        String fromVal = ""toVal = "";
        if (headerName.equals("first")) {
            fromVal = ;
            fromSipAddressDomain = ;
            fromDisplayName = ;
            toVal = ;
            toSipAddressDomain = ;
            toDisplayName = ;
        } else if (headerName.equals("second")) {
            fromVal = ;
            fromSipAddressDomain = ;
            fromDisplayName = ;
            toVal = ;
            toSipAddressDomain = ;
            toDisplayName = ;
        }
        .info("CreateInvite ");
        // create >From Header
        SipURI fromAddress = .createSipURI(fromVal,
                fromSipAddressDomain);
        Address fromNameAddress = .createAddress(fromAddress);
        fromNameAddress.setDisplayName(fromDisplayName);
        FromHeader fromHeader = .createFromHeader(fromNameAddress,
                new Integer((int) (Math.random() * 10000)).toString());
        // create To Header
        SipURI toAddress = .createSipURI(toVal,
                toSipAddressDomain);
        Address toNameAddress = .createAddress(toAddress);
        toNameAddress.setDisplayName(toDisplayName);
        ToHeader toHeader = .createToHeader(toNameAddressnull);
        // create Request URI
        SipURI requestURI = .createSipURI(toValpeerHostPort);
        // Create ViaHeaders
        ArrayList viaHeaders = new ArrayList();
        ViaHeader viaHeader = .createViaHeader("127.0.0.1",
                .getListeningPoint().getPort(), ,
                null);
        viaHeaders.add(viaHeader);
        // Create a new CallId header
        CallIdHeader callIdHeader = null;
        if (callerId == null) {
            callIdHeader = .getNewCallId();
        } else {
            callIdHeader = .createCallIdHeader(callerId);
        }
        // Create a new Cseq header
        CSeqHeader cSeqHeader = .createCSeqHeader(Long
                .parseLong(headerValue), .);
        // Create a new MaxForwardsHeader
        MaxForwardsHeader maxForwards = 
                .createMaxForwardsHeader(70);
        // Create the request.
        Request request = .createRequest(requestURI,
                .callIdHeadercSeqHeaderfromHeadertoHeader,
                viaHeadersmaxForwards);
        // Create contact headers
        String host = "127.0.0.1";
        SipURI contactUrl = .createSipURI(fromValhost);
        contactUrl.setPort(.getPort());
        // Create the contact name address.
        SipURI contactURI = .createSipURI(fromValhost);
        contactURI.setPort(.getListeningPoint().getPort());
        Address contactAddress = .createAddress(contactURI);
        // Add the contact address.
        contactAddress.setDisplayName(fromVal);
         = .createContactHeader(contactAddress);
        request.addHeader();
        // Allow header. With PUBLISH, to indicate that we'd like to have an
        // server-sided PA
        String methods = . + ", " + . + ", "
                + . + ", " + . + ", " + .
                + ", " + . + ", " + . + ", "
                + . + ", " + . + ", "
                + .;
        AllowHeader allowHeader = .createAllowHeader(methods);
        request.addHeader(allowHeader);
        return request;
    }
    public void init() throws IOException {
        SipFactory sipFactory = null;
         = null;
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        Properties properties = new Properties();
        // This one is optional so I remove it, since I will call 2 parts
        // properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort + "/"
        // + transport);
        properties.setProperty("javax.sip.STACK_NAME""Controller");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "controllerdebug.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "controllerlog.txt");
        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""DEBUG");
        properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT""true");
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        .addAppender(new FileAppender(new SimpleLayout(),
                "controllerconsolelog.txt"));
        try {
             = sipFactory.createSipStack(properties);
            .info("createSipStack " + );
             = sipFactory.createHeaderFactory();
             = sipFactory.createAddressFactory();
             = sipFactory.createMessageFactory();
             = .createListeningPoint("127.0.0.1",
                    5050, "udp");
            Controller4 listener = this;
            .addSipListener(listener);
        } catch (PeerUnavailableException e) {
            e.printStackTrace();
            ..println(e.getMessage());
            System.exit(0);
        } catch (Exception e) {
            .info("Creating Listener Points");
            .info(e.getMessage());
            e.printStackTrace();
        }
        try {
            .info("ProcessCTD ");
            ++;
            this. = "first";
            Request request = this.createInvite(, String
                    .valueOf(), nullnull);
            // Create ContentTypeHeader, !no media type!
            ContentTypeHeader contentTypeHeader = 
                    .createContentTypeHeader("application""sdp");
            String sdpData = "v=0\r\n"
                    + "o=4855 13760799956958020 13760799956958020"
                    + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n"
                    + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n"
                    + "t=0 0\r\n" + "a=rtpmap:0 PCMU/8000\r\n"
                    + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n"
                    + "a=ptime:20\r\n";
            byte[] contents = sdpData.getBytes();
            request.setContent(contentscontentTypeHeader);
            // Create the client transaction.
             = .getNewClientTransaction(request);
            // send the request out.
            .sendRequest();
            .info("INVITE first sent:\n" + request);
        } catch (Exception e) {
            .info("Creating call CreateInvite()");
            .info(e.getMessage());
            e.printStackTrace();
        }
    }
    public static void main(String args[]) throws Exception {
        new Controller4().init();
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException happened for " + exceptionEvent.getHost()
                + " port = " + exceptionEvent.getPort());
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("dialogTerminatedEvent");
    }
New to GrepCode? Check out our FAQ X