Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.forked.invite;
  
  
  import java.util.HashSet;
  import java.util.Iterator;
  
 
 import  junit.framework.TestCase;
 
A very simple forking proxy server.

Author(s):
M. Ranganathan
 
 public class Proxy extends TestCase implements SipListener {
 
 
 
 
     //private ServerTransaction st;
 
 
     private Hashtable clientTxTable = new Hashtable();
 
     private static String transport = "udp";
 
     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);
 
     static {
         try {
             .addAppender(new FileAppender(new SimpleLayout(),
                     ProtocolObjects.logFileDirectory + "proxlog.txt"));
         } catch (Exception ex) {
             throw new RuntimeException("could not open shootistconsolelog.txt");
         }
     }
 
     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 = ProtocolObjects.addressFactory.createSipURI("UA1""127.0.0.1");
                 sipUri.setPort(5080);
                sipUri.setLrParam();
                Address address = ProtocolObjects.addressFactory.createAddress("client1",
                        sipUri);
                RouteHeader rheader = ProtocolObjects.headerFactory.createRouteHeader(address);
                newRequest.addFirst(rheader);
                ViaHeader viaHeader = ProtocolObjects.headerFactory.createViaHeader(hostport,
                        null);
                newRequest.addFirst(viaHeader);
                ClientTransaction ct1 = sipProvider
                        .getNewClientTransaction(newRequest);
                sipUri = ProtocolObjects.addressFactory.createSipURI("proxy""127.0.0.1");
                address = ProtocolObjects.addressFactory.createAddress("proxy",sipUri);
                sipUri.setPort(5070);
                sipUri.setLrParam();
                RecordRouteHeader recordRoute = ProtocolObjects.headerFactory.createRecordRouteHeader(address);
                newRequest.addHeader(recordRoute);
                ct1.setApplicationData(st);
                this..put(new Integer(5080), ct1);
                newRequest = (Requestrequest.clone();
                sipUri = ProtocolObjects.addressFactory.createSipURI("UA2""127.0.0.1");
                sipUri.setLrParam();
                sipUri.setPort(5090);
                address = ProtocolObjects.addressFactory.createAddress("client2"sipUri);
                rheader = ProtocolObjects.headerFactory.createRouteHeader(address);
                newRequest.addFirst(rheader);
                viaHeader = ProtocolObjects.headerFactory.createViaHeader(hostport,
                        null);
                newRequest.addFirst(viaHeader);
                sipUri = ProtocolObjects.addressFactory.createSipURI("proxy""127.0.0.1");
                sipUri.setPort(5070);
                sipUri.setLrParam();
                sipUri.setTransportParam();
                address = ProtocolObjects.addressFactory.createAddress("proxy",sipUri);
                recordRoute = ProtocolObjects.headerFactory.createRecordRouteHeader(address);
                newRequest.addHeader(recordRoute);
                ClientTransaction ct2 = sipProvider
                        .getNewClientTransaction(newRequest);
                ct2.setApplicationData(st);
                this..put(new Integer(5090), 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 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 {
                // Can be BYE due to Record-Route
                .info("Got a non-invite response " + response);
                SipProvider p = (SipProviderresponseEvent.getSource();
                response.removeFirst. );
                p.sendResponseresponse );
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        .info("Timeout occured");
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException occured");
    }
    public SipProvider createSipProvider() {
        try {
            ListeningPoint listeningPoint = ProtocolObjects.sipStack.createListeningPoint(
                    );
             = ProtocolObjects.sipStack
                    .createSipProvider(listeningPoint);
            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..values().iterator(); it
                    .hasNext();) {
                if (it.next().equals(ct)) {
                    it.remove();
                }
            }
        } else {
            .info("Server tx terminated! ");
        }
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("HUH!! why do i see this event??");
    }
    public static void main(String[] argsthrows Exception {
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        ProtocolObjects.init("proxy",false);
        Proxy proxy = new Proxy();
        proxy.createSipProvider();
        proxy.sipProvider.addSipListener(proxy);
    }
New to GrepCode? Check out our FAQ X