Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.forkedinvite;
  
  
  import java.util.Iterator;
  
 
 import  junit.framework.TestCase;
 
 
 import  test.tck.TestHarness;
 import  test.tck.msgflow.callflows.ProtocolObjects;

A very simple forking proxy server.

Author(s):
M. Ranganathan
 
 public class Proxy implements SipListener {
 
     // private ServerTransaction st;
 
 
     private Hashtable clientTxTable = new Hashtable();
 
     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 static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static String transport = "udp";
 
     private SipStack sipStack;
 
     private int ntargets;
     
     
     private void sendTo(ServerTransaction stRequest requestint targetPortthrows Exception {
         Request newRequest = (Requestrequest.clone();
         
         SipURI sipUri = .createSipURI("UA1""127.0.0.1");
         sipUri.setPort(targetPort);
         sipUri.setLrParam();
         Address address = .createAddress("client1"sipUri);
         RouteHeader rheader = .createRouteHeader(address);
 
         newRequest.addFirst(rheader);
         ViaHeader viaHeader = .createViaHeader(this.null);
         newRequest.addFirst(viaHeader);
         ClientTransaction ct1 = .getNewClientTransaction(newRequest);
         sipUri = .createSipURI("proxy""127.0.0.1");
         address = .createAddress("proxy"sipUri);
         sipUri.setPort(5070);
         sipUri.setLrParam();
         RecordRouteHeader recordRoute = .createRecordRouteHeader(address);
         newRequest.addHeader(recordRoute);
         ct1.setApplicationData(st);
         this..put(new Integer(targetPort), ct1);
        ct1.sendRequest();
    }
    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);
                }
                
                for ( int i = 0; i < i++ ) {
                    this.sendTo(st,request,5080 + i);
                }
             
               
            } 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 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 (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 {
                    // 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
                    this..sendResponse(newResponse);
                }
            } else {
                // this is the OK for the cancel.
                .info("Got a non-invite response " + response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TestCase.fail("unexpected exception");
        }
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        .error("Timeout occured");
        TestCase.fail("unexpected event");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException occured");
        TestCase.fail("unexpected exception io exception");
    }
    public SipProvider createSipProvider() {
        try {
            ListeningPoint listeningPoint = .createListeningPoint();
             = .createSipProvider(listeningPoint);
            return ;
        } catch (Exception ex) {
            .error(ex);
            TestCase.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..values().iterator(); it.hasNext();) {
                if (it.next().equals(ct)) {
                    it.remove();
                }
            }
        } else {
            .info("Server tx terminated! ");
        }
    }
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
        TestCase.fail("unexpected event");
    }
    public Proxy(int myPortint ntargets) {
        this. = myPort;
        this. = ntargets;
        SipObjects sipObjects = new SipObjects(myPort"proxy","off");
         = sipObjects.addressFactory;
         = sipObjects.messageFactory;
         = sipObjects.headerFactory;
        this. = sipObjects.sipStack;
  
    }
    public void stop() {
       this..stop();
    }
New to GrepCode? Check out our FAQ X