Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.forkedinvite482;
  
  import java.util.HashSet;
  import java.util.Iterator;
  
 
 
 import  test.tck.TestHarness;
 import  test.tck.msgflow.callflows.ProtocolObjects;

A very simple forking proxy server.

Author(s):
M. Ranganathan
 
 public class Proxy extends TestHarness implements SipListener {
 
     // private ServerTransaction st;
 
 
     private HashSet clientTxTable = new HashSet<ClientTransaction>();
 
     private static String host = "127.0.0.1";
 
     private int port = 5070;
 
     private SipProvider sipProvider;
 
     private static String unexpectedException = "Unexpected exception";
 
     private static Logger logger = Logger.getLogger(Proxy.class);
 
     private ProtocolObjects protocolObjects;
 
     private boolean loopDetectedSeen;
 
     public void processRequest(RequestEvent requestEvent) {
         try {
             Request request = requestEvent.getRequest();
             SipProvider sipProvider = (SipProviderrequestEvent.getSource();
             this. = sipProvider;
             if (request.getMethod().equals(.)) {
 
                 ListeningPoint lp = sipProvider.getListeningPoint(.);
                 String host = lp.getIPAddress();
                 int port = lp.getPort();
 
                 ServerTransaction st = null;
                 if (requestEvent.getServerTransaction() == null) {
                     st = sipProvider.getNewServerTransaction(request);
 
                     Request newRequest = (Requestrequest.clone();
                     SipURI sipUri = ..createSipURI("UA1",
                             "127.0.0.1");
                     sipUri.setPort(5080);
                     sipUri.setLrParam();
                     Address address = ..createAddress("client1",
                             sipUri);
                     RouteHeader rheader = .
                             .createRouteHeader(address);
 
                     newRequest.addFirst(rheader);
                     ViaHeader viaHeader = ..createViaHeader(host,
                             port.null);
                     newRequest.addFirst(viaHeader);
                     
                     ClientTransaction ct1 = sipProvider.getNewClientTransaction(newRequest);
                     sipUri = ..createSipURI("proxy""127.0.0.1");
                     address = ..createAddress("proxy"sipUri);
                     sipUri.setPort(5080);
                     sipUri.setLrParam();
                     RecordRouteHeader recordRoute = .
                             .createRecordRouteHeader(address);
                     newRequest.addHeader(recordRoute);
                     ct1.setApplicationData(st);
                    this..add(ct1);
                    Thread.sleep((int) ( Math.abs((Math.random() * 100 ))));
                    newRequest = (Requestrequest.clone();
                    sipUri = ..createSipURI("UA2""127.0.0.1");
                    sipUri.setLrParam();
                    sipUri.setPort(5080);
                    address = ..createAddress("client2"sipUri);
                    rheader = ..createRouteHeader(address);
                    newRequest.addFirst(rheader);
                    viaHeader = ..createViaHeader(hostport,
                            .null);
                    newRequest.addFirst(viaHeader);
                    sipUri = ..createSipURI("proxy""127.0.0.1");
                    sipUri.setPort(5080);
                    sipUri.setLrParam();
                    sipUri.setTransportParam(.);
                    address = ..createAddress("proxy"sipUri);
                    recordRoute = ..createRecordRouteHeader(address);
                    newRequest.addHeader(recordRoute);
                    ClientTransaction ct2 = sipProvider.getNewClientTransaction(newRequest);
                    ct2.setApplicationData(st);
                    this..add(ct2);
                    // Send the requests out to the two listening points of the
                    // client.
                    ct2.sendRequest();
                    ct1.sendRequest();
                }
            } else {
                // Remove the topmost route header
                // The route header will make sure it gets to the right place.
                .info("proxy: Got a request " + request.getMethod());
                Request newRequest = (Requestrequest.clone();
                newRequest.removeFirst(.);
                sipProvider.sendRequest(newRequest);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void checkState() {
        assertTrue("Should see LOOP DETECTED");
    }
    public synchronized void processResponse(ResponseEvent responseEvent) {
        try {
            Response response = responseEvent.getResponse();
            CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
            .info("ClientTxID = " + responseEvent.getClientTransaction() + " client tx id "
                    + ((ViaHeaderresponse.getHeader(.)).getBranch()
                    + " CSeq header = " + response.getHeader(.) + " status code = "
                    + response.getStatusCode());
            // JvB: stateful proxy MUST NOT forward 100 Trying
            if (response.getStatusCode() == 100)
                return;
            if (response.getStatusCode() == .) {
                this. = true;
            }
            if (cseq.getMethod().equals(.)) {
                ClientTransaction ct = responseEvent.getClientTransaction();
                if (ct != null) {
                    ServerTransaction st = (ServerTransactionct.getApplicationData();
                    // Strip the topmost via header
                    Response newResponse = (Responseresponse.clone();
                    newResponse.removeFirst(.);
                    // The server tx goes to the terminated state.
                    st.sendResponse(newResponse);
                } else {
                    .debug("Discarding response - no transaction found!");
                }
            } else {
                // this is the OK for the cancel.
                .info("Got a non-invite response " + response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception");
        }
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        .error("Timeout occured");
        fail("unexpected event");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException occured");
        fail("unexpected exception io exception");
    }
    public SipProvider createSipProvider() {
        try {
            ListeningPoint listeningPoint = ..createListeningPoint(,
                    .);
             = ..createSipProvider(listeningPoint);
            .setAutomaticDialogSupportEnabled(false);
            return ;
        } catch (Exception ex) {
            .error(ex);
            fail();
            return null;
        }
    }
    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event occured -- cleaning up");
        if (!transactionTerminatedEvent.isServerTransaction()) {
            ClientTransaction ct = transactionTerminatedEvent.getClientTransaction();
            for (Iterator it = this..iterator(); it.hasNext();) {
                if (it.next().equals(ct)) {
                    it.remove();
                }
            }
        } else {
            .info("Server tx terminated! ");
        }
    }
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
        fail("unexpected event");
    }
    public Proxy(int myPort, ProtocolObjects protocolObjects) {
        this. = myPort;
        this. = protocolObjects;
    }
New to GrepCode? Check out our FAQ X