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 application.

Author(s):
Kathleen McCallum
   main () -> init()
  init()
    createSipStack
    createInvite() -> First
  processResponse()
    if (OK) first
       createInvite() -> second
    else if (OK) second
       ack() -> second
       ack() -> first
 
 
 
 public class Controller 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;
 
     String Secuencia;
 
     String transport = "udp";
 
     protected static final String usageString = "java "
             + "examples.ctd.ctdControll \n"
             + ">>>> is your class path set to the root?";
 
 
     String Auser = "AGuy";
 
     String ASipAddressDomain = "Afirst.com";
 
     String ADisplayName = "The A first";
 
     String Buser = "BGuy";
 
     String BSipAddressDomain = "BSecond.com";
 
     String BDisplayName = "The B second";
 
     String peerHostPortA = "127.0.0.1:5070";
 
     String peerHostPortB = "127.0.0.1:5080";
 
     int first = 0, second = 0;
 
     private static void usage() {
         ..println();
         System.exit(0);
     }
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                 .getServerTransaction();
 
         ..println("\n\nRequest " + request.getMethod()
                 + " received at " + .getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         // We are the UAC so the only request we get is the BYE.
         if (request.getMethod().equals(.))
             processBye(requestserverTransactionId);
 
     }
 
    public void processBye(Request request,
            ServerTransaction serverTransactionId) {
        try {
            ..println("Controller:  got a bye .");
            if (serverTransactionId == null) {
                ..println("Controller:  null TID.");
                return;
            }
            ..println("Create OK para BYE: ");
            // 1: OK BYE
            Response ok = .createResponse(.request);
            serverTransactionId.sendResponse(ok);
            // 2do: BYE for the other side (send a new clientTransaction)
            ..println("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 void processResponse(ResponseEvent responseReceivedEvent) {
        ..println("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        ClientTransaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        ..println("Response received : Status Code = "
                + response.getStatusCode() + " " + cseq);
        if (tid == null) {
            ..println("Stray response -- dropping ");
            return;
        }
        ..println("transaction state is " + tid.getState());
        ..println("Dialog = " + tid.getDialog());
        ..println("Dialog State is " + tid.getDialog().getState());
        try {
            if (response.getStatusCode() == .) {
                if (cseq.getMethod().equals(.)) {
                    if (.equals("first")) {
                        ..println("processResponse FIRST");
                         = responseReceivedEvent;
                        // get call-id
                        String callId = ((CallIdHeaderresponse
                                .getHeader(.)).getCallId();
                        // Create second Invite
                        ++;
                         = "second";
                        Request requestSecond = this.createInvite(,
                                String.valueOf(), callIdnull,
                                );
                        // SDP for second Invite with first response
                        // ContentTypeHeader
                        requestSecond.setContent(response.getContent(),
                                (ContentTypeHeader) (response
                                        .getHeader("Content-Type")));
                         = 
                                .getNewClientTransaction(requestSecond);
                        .sendRequest();
                        ..println("INVITE second sent:\n"
                                + requestSecond);
                    } else if (.equals("second")) {
                        ..println("processResponse SECOND");
                        // send ACK second
                        Dialog dialogSecond = tid.getDialog();
                        Request ackRequest = dialogSecond.createAck(cseq
                                .getSeqNumber());// dialogSecond.createRequest(Request.ACK);
                        ..println("Sending ACK second");
                        dialogSecond.sendAck(ackRequest);// dialogSecond.sendAck(ackRequest);
                        CSeqHeader cseqFirst = (CSeqHeader
                                .getResponse().getHeader(.);
                        Request ackRequestFirst = 
                                .getDialog().createAck(
                                        cseqFirst.getSeqNumber());
                        ackRequestFirst.setContent(response.getContent(),
                                (ContentTypeHeader) (response
                                        .getHeader("Content-Type")));
                        ..println("Sending ACK first");
                        .getDialog().sendAck(ackRequestFirst);
                        // save the dialog of the other side, for the bye...
                        .getDialog().setApplicationData(
                                dialogSecond);
                        dialogSecond.setApplicationData(
                                .getDialog());
                         = "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 = ;
        }
        ..println("CreateInvite ");
        // create >From Header
        SipURI fromAddress = .createSipURI(fromVal,
                fromSipAddressDomain);
        Address fromNameAddress = .createAddress(fromAddress);
        fromNameAddress.setDisplayName(fromDisplayName);
        FromHeader fromHeader = .createFromHeader(fromNameAddress,
                "12345");
        // 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);
        // Add the extension header. To mantain Flow I
        Header extensionHeader = .createHeader(headerName,
                headerValue);
        request.addHeader(extensionHeader);
        return request;
    }
    public void init() {
        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""16");
        try {
             = sipFactory.createSipStack(properties);
            ..println("createSipStack " + );
             = sipFactory.createHeaderFactory();
             = sipFactory.createAddressFactory();
             = sipFactory.createMessageFactory();
             = .createListeningPoint("127.0.0.1",
                    5050, "udp");
            Controller listener = this;
            .addSipListener(listener);
        } catch (PeerUnavailableException e) {
            e.printStackTrace();
            ..println(e.getMessage());
            System.exit(0);
        } catch (Exception e) {
            ..println("Creating Listener Points");
            ..println(e.getMessage());
            e.printStackTrace();
        }
        try {
            ..println("ProcessCTD ");
            ++;
            this. = "first";
            Request request = this.createInvite(, String
                    .valueOf(), ""null);
            // Create the client transaction.
             = .getNewClientTransaction(request);
            // send the request out.
            .sendRequest();
            ..println("INVITE first sent:\n" + request);
        } catch (Exception e) {
            ..println("Creating call CreateInvite()");
            ..println(e.getMessage());
            e.printStackTrace();
        }
    }
    public static void main(String args[]) {
        new Controller().init();
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        ..println("Transaction Time out");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        ..println("IOException happened for "
                + exceptionEvent.getHost() + " port = "
                + exceptionEvent.getPort());
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        ..println("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        ..println("dialogTerminatedEvent");
    }
New to GrepCode? Check out our FAQ X