Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.recroute;
  
  
 
 
A very simple Record-Routing proxy server.

Author(s):
M. Ranganathan
Jeroen van Bemmel
 
 public class Proxy extends TestHarness implements SipListener {
 
     // private ServerTransaction st;
 
     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("test.tck");
 
     private ProtocolObjects protocolObjects;
 
     private boolean ackSeen;
 
     private boolean inviteSeen;
 
     private boolean byeSeen;
 
     private int infoCount = 0;
 
 
     public void checkState() {
         TestHarness.assertTrue("INVITE should be seen by proxy");
         TestHarness.assertTrue("Should see two INFO messages" == 2);
         TestHarness.assertTrue("BYE should be seen by proxy");
         TestHarness.assertTrue("ACK should be seen by proxy");
     }
 
     public void processRequest(RequestEvent requestEvent) {
         try {
             Request request = requestEvent.getRequest();
             SipProvider sipProvider = (SipProviderrequestEvent.getSource();
             if (request.getMethod().equals(.)) {
                  = true;
 
                 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();
 
                 //
                 // Add a Route: header to 5080
                 //
                 SipURI sipUri = ..createSipURI(
                         "UA1""127.0.0.1");
                 sipUri.setPort(5080);
                 sipUri.setLrParam();
                 sipUri.setTransportParam. );
                 Address address = ..createAddress(
                         "client1"sipUri);
                 RouteHeader rheader = .
                         .createRouteHeader(address);
                newRequest.addFirst(rheader);
                //
                // Add a Via header + Record-Route
                //
                ViaHeader viaHeader = .
                        .createViaHeader(hostport.,
                                null);
                newRequest.addFirst(viaHeader);
                ClientTransaction ct1 = sipProvider
                        .getNewClientTransaction(newRequest);
                sipUri = ..createSipURI("proxy",
                        "127.0.0.1");
                address = ..createAddress("proxy",
                        sipUri);
                sipUri.setPort(5070);
                sipUri.setLrParam();
                sipUri.setTransportParam(.);
                RecordRouteHeader recordRoute = .
                        .createRecordRouteHeader(address);
                newRequest.addHeader(recordRoute);
                ct1.setApplicationData(st);
                // Send the request out to the two listening point of the
                // client.
                ct1.sendRequest();
                TestHarness.assertNull(ct1.getDialog());
            } else if (request.getMethod().equals(.)) {
                Request newRequest = (Requestrequest.clone();
                newRequest.removeFirst(.);
                ViaHeader viaHeader = .
                        .createViaHeader(.,
                                null);
                newRequest.addFirst(viaHeader);
                this. = true;
                .debug("PROXY : sendingAck "  + newRequest);
                sipProvider.sendRequest(newRequest);
            } else {
                // Remove the topmost route header
                // The route header will make sure it gets to the right place.
                .debug("proxy: Got a request\n" + request);
                if (request.getMethod().equals(.)) {
                    this. = true;
                }
                if (request.getMethod().equals(.)) {
                    this.++;
                }
                if (requestEvent.getServerTransaction() == null) {
                    TestHarness.assertNull("Dialog should be null",
                            requestEvent.getDialog());
                    ServerTransaction stx = sipProvider
                            .getNewServerTransaction(request);
                    Request newRequest = (Requestrequest.clone();
                    newRequest.removeFirst(.);
                    ViaHeader viaHeader = .
                            .createViaHeader(,
                                    .null);
                    newRequest.addFirst(viaHeader);
                    ClientTransaction ctx = sipProvider
                            .getNewClientTransaction(newRequest);
                    ctx.setApplicationData(stx);
                    stx.setApplicationData(ctx);
                    ctx.sendRequest();
                } else {
                    .debug("Saw a retransmission? State = "
                            + requestEvent.getServerTransaction().getState());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            .error("Unexpected error forwarding request"ex);
            TestHarness.fail("Unexpected exception forwarding request");
        }
    }
    public void processResponse(ResponseEvent responseEvent) {
        try {
            Response response = responseEvent.getResponse();
            CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
            .debug("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;
            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.
                if ( st.getState() != .)
                 st.sendResponse(newResponse);
                TestHarness.assertNull(st.getDialog());
            } else {
                // Client tx has already terminated but the UA is
                // retransmitting
                // just forward the response statelessly.
                // Strip the topmost via header
                Response newResponse = (Responseresponse.clone();
                newResponse.removeFirst(.);
                // Send the retransmission statelessly
                SipProvider sipProvider = (SipProviderresponseEvent
                        .getSource();
                sipProvider.sendResponse(newResponse);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TestHarness.fail("unexpected exception",ex);
        }
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        .error("Timeout occured");
        fail("unexpected event");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("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) {
        .debug("Transaction terminated event occured -- cleaning up");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        fail("unexpected event");
    }
    public Proxy(int myPortProtocolObjects protocolObjects) {
        this. = myPort;
        this. = protocolObjects;
    }
New to GrepCode? Check out our FAQ X