Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.dialog.b2bua.reinvite;
  
  
  import java.util.HashSet;
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;
 
 
 public class BackToBackUserAgent implements SipListenerExt {
     
     private HashSet<Dialogdialogs = new HashSet<Dialog>();
     private ListeningPoint[] listeningPoints = new ListeningPoint[2]; 
     private SipProvider[] providers = new SipProvider[2];
     private MessageFactory messageFactory;
     private ProtocolObjects protocolObjects;
     private HeaderFactory headerFactory;
     private AddressFactory addressFactory;
     private boolean inviteOkSeen;
     private boolean dialogTimedOut;
     
     public Dialog getPeer(Dialog dialog) {
         Object[] dialogArray = .toArray();
         if ( dialogArray.length < 2) return null;
         if ( dialogArray[0] == dialogreturn (DialogdialogArray[1];
         else if ( dialogArray[1] == dialogreturn (DialogdialogArray[0];
         else return null;
     }
     
     public SipProvider getPeerProvider (SipProvider provider) {
         if ( [0] == providerreturn [1];
         else return [0];
     }
     
     public void addDialog(Dialog dialog) {
         this..add(dialog);
         ..println("Dialogs  " + this.);
     }
     
     public void forwardRequest(RequestEvent requestEvent
             ServerTransaction serverTransactionthrows SipExceptionParseExceptionInvalidArgumentException  {
       
         SipProvider provider = (SipProviderrequestEvent.getSource();
         Dialog dialog = serverTransaction.getDialog();
         Dialog peerDialog = this.getPeer(dialog);
         Request request = requestEvent.getRequest(); 
         
         ..println("Dialog " + dialog);
         
         Request newRequest = null;
         if ( peerDialog != null ) {
              newRequest = peerDialog.createRequest(request.getMethod());
         } else {
              newRequest = (Requestrequest.clone();
              ((SipURI)newRequest.getRequestURI()).setPort(5090);
              newRequest.removeHeader(.);
              FromHeader fromHeader = (FromHeadernewRequest.getHeader(.);
              fromHeader.setTag(Long.toString(Math.abs(new Random().nextLong())));
              SipProvider peerProvider = getPeerProvider(provider);
              ViaHeader viaHeader = ((ListeningPointExt) ((SipProviderExt)
                      getPeerProvider(provider)).getListeningPoint("udp")).createViaHeader();
              newRequest.setHeader(viaHeader);
             
        }
        ContactHeader contactHeader = ((ListeningPointExt) ((SipProviderExt)
                                    getPeerProvider(provider)).getListeningPoint("udp")).createContactHeader();
        newRequest.setHeader(contactHeader);
        ClientTransaction clientTransaction = provider.getNewClientTransaction(newRequest);
        clientTransaction.setApplicationData(serverTransaction);
        if (request.getMethod().equals(.)) {
            this.addDialog(clientTransaction.getDialog());
        }
        if ( peerDialog != null ) {
            peerDialog.sendRequest(clientTransaction);
        } else {
            clientTransaction.sendRequest();
        }
        
    }
   
    public void processDialogTimeout(DialogTimeoutEvent timeoutEvent) {
        this. = true;
      
    }
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
        
    }
 
     public void processIOException(IOExceptionEvent exceptionEvent) {
        // TODO Auto-generated method stub
        
    }
    public void processRequest(RequestEvent requestEvent) {
        try {
            Request request = requestEvent.getRequest();
            SipProvider provider = (SipProviderrequestEvent.getSource();
            if (request.getMethod().equals(.)) {
                if (requestEvent.getServerTransaction() == null) {
                    try {
                        ServerTransaction serverTx = provider.getNewServerTransaction(request);
                        this.addDialog(serverTx.getDialog());
                        this.forwardRequest(requestEvent,serverTx);
                    } catch (TransactionAlreadyExistsException ex) {
                        ..println("Transaction exists -- ignoring");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        BackToBackUserAgentTest.fail("Unepxected exception");
                    }
                } else {
                    this.forwardRequest(requestEvent,requestEvent.getServerTransaction());
                }
            } else if ( request.getMethod().equals(.)) {
                ServerTransaction serverTransaction = requestEvent.getServerTransaction();
                if ( serverTransaction == null ) {
                    serverTransaction = provider.getNewServerTransaction(request);
                }
                this.forwardRequest(requestEventserverTransaction);
               
            } else if (request.getMethod().equals(.)) {
                Dialog dialog = requestEvent.getDialog();
                Dialog peer = this.getPeer(dialog);
                Response response = this..get(peer);
                CSeqHeader cseqHeader = (CSeqHeaderresponse.getHeader(.);
                String method = cseqHeader.getMethod();
                long seqno = cseqHeader.getSeqNumber();
                Request ack = peer.createAck(seqno);
                peer.sendAck(ack);
            }
           
        } catch ( Exception ex) {
            ex.printStackTrace();
            BackToBackUserAgentTest.fail("Unexpected exception forwarding request");
        }
    }
    
    public void processResponse(ResponseEvent responseEvent) {
        try {
            Response response = responseEvent.getResponse();
            Dialog dialog = responseEvent.getDialog();
            this..put(dialogresponse);
             ServerTransaction serverTransaction = (ServerTransaction)responseEvent.getClientTransaction().getApplicationData();
            if ( serverTransaction != null ) {
                Request stRequest = serverTransaction.getRequest();
                Response newResponse = this..createResponse(response.getStatusCode(),stRequest);
                SipProvider provider = (SipProvider)responseEvent.getSource();
                SipProvider peerProvider = this.getPeerProvider(provider);
                ListeningPoint peerListeningPoint = peerProvider.getListeningPoint("udp");
                ContactHeader peerContactHeader = ((ListeningPointExt)peerListeningPoint).createContactHeader();
                newResponse.setHeader(peerContactHeader);
                serverTransaction.sendResponse(newResponse);
                if ( ((CSeqHeader)response.getHeader(.)).getMethod().equals(.) &&
                        response.getStatusCode() == 200 ) {
                    Request newRequest = dialog.createRequest(.);
                    ListeningPointExt listeningPoint = (ListeningPointExtprovider.getListeningPoint("udp");
                    ContactHeader contact = listeningPoint.createContactHeader();
                    newRequest.setHeader(contact);
                    ClientTransaction clientTransaction = provider.getNewClientTransaction(newRequest);
                    // Send without waiting for ACK.
                    dialog.sendRequest(clientTransaction);
                }
            } else {
                this. = true;
                if ( ((CSeqHeader)response.getHeader(.)).getMethod().equals(.) &&
                        response.getStatusCode() == 200){
                    long cseqno = ((CSeqHeader)response.getHeader(.)).getSeqNumber();
                    Request ack = dialog.createAck(cseqno);
                    dialog.sendAck(ack);
                } else {
                    if ( !((CSeqHeader)response.getHeader(.)).getMethod().equals(.)) {
                        ..println("Unexpected response " + response);
                        BackToBackUserAgentTest.fail("Unexpected response");
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            BackToBackUserAgentTest.fail("Unexpected exception");
        }
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        // TODO Auto-generated method stub
        
    }
    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
         
    }
    
    public BackToBackUserAgent(int port1int port2) {
        SipFactory sipFactory = null;
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        Properties properties = new Properties();
        this. = new ProtocolObjects("backtobackua","gov.nist","udp",true,true);
     
        try {
             = .;
             = .;
             = .;
            SipStack sipStack = .;
            ListeningPoint lp1 = sipStack.createListeningPoint("127.0.0.1"port1"udp");
            ListeningPoint lp2 = sipStack.createListeningPoint("127.0.0.1"port2"udp");
            SipProvider sp1 = sipStack.createSipProvider(lp1);
            SipProvider sp2 = sipStack.createSipProvider(lp2);
            this.[0] = lp1;
            this.[1] = lp2;
            this.[0] = sp1;
            this.[1] = sp2;
            sp1.addSipListener(this);
            sp2.addSipListener(this);
        } catch (Exception ex) {
            
        }
    }
    
    public void checkState() {
        BackToBackUserAgentTest.assertTrue("INVITE OK not seen"this.);
        BackToBackUserAgentTest.assertFalse("Dialog timed out "this.);
    }
New to GrepCode? Check out our FAQ X