⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gateway.java

📁 First of all, the Applet-phone is a SIP User-Agent with audio and text messaging capabilities. But
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    response.toString());            }            catch (Exception e){                e.printStackTrace();            }        }    }        /** This is a listener method.     */    public void processResponse(ResponseEvent responseEvent) {        try{                        javax.sip.message.Response response = responseEvent.getResponse();            SipProvider sipProvider = (SipProvider) responseEvent.getSource();            ClientTransaction clientTransaction=responseEvent.getClientTransaction();                        GatewayDebug.println            ("\n***************************************************************"+            "\n***************************************************************"+            "\nResponse "+response.getStatusCode() + " "+response.getReasonPhrase()            +" received:\n"+response.toString() );            GatewayDebug.println("Processing Response in progress");                        if (GatewayDebug.debug)                GatewayUtilities.printTransaction(clientTransaction);                        //If BUSY            if(response.getStatusCode()==486){                System.out.println(response.getHeader(ToHeader.NAME).toString());                if(response.getHeader(ToHeader.NAME).toString().indexOf(configuration.serviceGuy)!=-1){                                        System.out.println("/*************************************/");                                        Request request=clientTransaction.getRequest();                                        System.out.println(request.toString());                    System.out.println("/*************************************/");                    Vector targetURIList=null;                    if ( registrar.hasRegistration("sip:secretary@nist.gov")  ) {                        targetURIList=registrar.getContactsURI("sip:secretary@nist.gov");                                                            requestForwarding.forwardRequest(targetURIList,sipProvider,                        request,null);                        return;                    }                                        /***************** PSTN gateway *************************************************/                    else if ( configuration.pstnGateway!=null ) {                                                    GatewayDebug.println                        ("Proxy, processRequest(), we are going to forward to the PSTN gateway...");                                                        AddressFactory addressFactory=getAddressFactory();                        SipURI sipURI=addressFactory.createSipURI(configuration.phoneNumberToCall,configuration.pstnGateway+";user=phone");                        targetURIList=new Vector();                        targetURIList.addElement(sipURI);                            Request requestToModify=(Request)request.clone();                        ToHeader toHeader=(ToHeader)requestToModify.getHeader(ToHeader.NAME);                        URI uri=toHeader.getAddress().getURI();                        if(uri.isSipURI()){                            SipURI toSipURI=(SipURI)uri;                            toSipURI.setUser(configuration.phoneNumberToCall);                            toSipURI.setHost("129.6.50.176:4000;user=phone");                                                       }                        System.out.println(requestToModify);                        requestForwarding.forwardRequest(targetURIList,sipProvider,                        requestToModify,null);                        return;                                             }                    /*********************************************************************************/                                                requestForwarding.forwardRequest(targetURIList,sipProvider,                    request,null);                    return;                                         }            }                                        responseForwarding.forwardResponse(sipProvider, response,clientTransaction);                    }        catch (Exception ex) {            if (GatewayDebug.debug) {                GatewayDebug.println("Proxy, processResponse(), internal error, "+                "exception raised:");                GatewayDebug.logException(ex);            }        }    }            /** JAIN Listener method.     */    public void processTimeout(TimeoutEvent timeOutEvent) {        GatewayDebug.println("TimeoutEvent received");    }                    /***********************  Methods for         ***************     *    starting and stopping the proxy          		*     ************************************************************/        /** Start the proxy, this method has to be called after the init method     * throws Exception that which can be caught by the upper application     */    public void start() throws Exception {        if (configuration!=null        && configuration.isValidConfiguration()) {            Properties properties=new Properties();            // LOGGING property:                        if (configuration.enableDebug) {                GatewayDebug.debug=true;                GatewayDebug.setProxyOutputFile(configuration.outputProxy);                GatewayDebug.println("DEBUG properties set!");                if (configuration.badMessageLogFile!=null)                    properties.setProperty("gov.nist.javax.sip.BAD_MESSAGE_LOG",                    configuration.badMessageLogFile);                if (configuration.debugLogFile!=null) {                    properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",                    configuration.debugLogFile);                }                if (configuration.serverLogFile!=null)                    properties.setProperty("gov.nist.javax.sip.SERVER_LOG",                    configuration.serverLogFile);                            }            else {                System.out.println("DEBUG properties not set!");            }            /*********************************************************************************************  SIP STACKS ******************************************************  ********************************************************************************/                                    SipFactory sipFactory = SipFactory.getInstance();            sipFactory.setPathName("gov.nist");            headerFactory = sipFactory.createHeaderFactory();            addressFactory = sipFactory.createAddressFactory();            messageFactory = sipFactory.createMessageFactory();                        if (configuration.enableRTPGateway) {                GatewayDebug.println("Proxy, RTP gateway enabled between:"                +configuration.minRTPHolePort+" and "+configuration.maxRTPHolePort+                " and a max. packet size of: "+configuration.maxRTPPacketSize);                rtpHolesManager=new RTPHolesManager(configuration.minRTPHolePort,                configuration.maxRTPHolePort,configuration.maxRTPPacketSize);                  }                        // Create SipStack objects:            for (int i=0;i<configuration.stackConfigurationList.size();i++) {                StackConfiguration stackConfiguration=(StackConfiguration)                configuration.stackConfigurationList.elementAt(i);                //System.out.println(stackConfiguration.stackIPAddress);                properties.setProperty("javax.sip.IP_ADDRESS",                    stackConfiguration.stackIPAddress);                properties.setProperty("javax.sip.STACK_NAME",                    stackConfiguration.getStackName());                if (configuration.check(stackConfiguration.outboundProxy))                    properties.setProperty("javax.sip.OUTBOUND_PROXY",                    stackConfiguration.outboundProxy);                if (configuration.check(stackConfiguration.routerPath))                    properties.setProperty("javax.sip.ROUTER_PATH",                    stackConfiguration.routerPath);                         SipStack sipStack = sipFactory.createSipStack(properties);                sipStacksList.addElement(sipStack);                                // We create the Listening points:                Vector lps=stackConfiguration.getListeningPoints();                            for ( int j=0;lps!=null && j<lps.size();j++) {                    Association a=(Association)lps.elementAt(j);                    try {                        GatewayDebug.println("Proxy, transport:" + a.transport);                        GatewayDebug.println("Proxy, port:" +                                    Integer.valueOf(a.port).intValue());                                           ListeningPoint lp=sipStack.createListeningPoint                        (Integer.valueOf(a.port).intValue(),                        a.transport);                                            SipProvider sipProvider = sipStack.createSipProvider(lp);                        sipProvider.addSipListener( this );                    }                    catch(Exception e) {                        GatewayDebug.println                        ("Proxy, ERROR: listening point not created ");                        GatewayDebug.logException(e);                    }                }            }                               }        else {            System.out.println("ERROR: the configuration file is not correct!"+            " Correct the errors first.");        }    }        /** Stop the proxy, this method has to be called after the start method     * throws Exception that which can be caught by the upper application     */    public void stop()  throws Exception {            }            public ViaHeader getStackViaHeader(SipStack sipStack) {        try {            ListeningPoint lp =            (ListeningPoint)sipStack.getListeningPoints().next();            String host = sipStack.getIPAddress();            int port = lp.getPort();            String transport = lp.getTransport();            // branch id is assigned by the transaction layer.            return  headerFactory.createViaHeader            (host,port,transport,null);        }        catch (Exception e) {            if (GatewayDebug.debug) {                GatewayDebug.println("Proxy, getStackViaHeader(), internal error, "+                "exception raised:");                GatewayDebug.logException(e);            }            return null;        }    }        public SipStack getSipStack(String toHostName) {        try {            GatewayDebug.println("GatewayDebug, getSipStack(), we try to use the "+                    " the appropriate stack for the destination:"+toHostName);            for (int i=0;i<sipStacksList.size();i++) {                SipStack sipStack=(SipStack)sipStacksList.elementAt(i);                String stackIPAddress=sipStack.getIPAddress();                if (stackIPAddress.equals(toHostName) ) {                    GatewayDebug.println("GatewayDebug, getSipStack(), we return "+                    " the appropriate stack");                    return sipStack;                }            }            GatewayDebug.println("GatewayDebug, getSipStack(), we return the default stack");            return (SipStack)sipStacksList.firstElement();        }        catch(Exception e) {            if (GatewayDebug.debug) {                GatewayDebug.println("GatewayDebug, getSipStack(), internal error, "+                "exception raised:");                GatewayDebug.logException(e);            }            return null;        }    }          public SipStack getOppositeSipStack(String stackIPAddr) {        try {            GatewayDebug.println("GatewayDebug, getOppositeSipStack(), we try to get "+            " the opposite stack for the destination:"+stackIPAddr);            for (int i=0;i<sipStacksList.size();i++) {                SipStack sipStack=(SipStack)sipStacksList.elementAt(i);                String stackIPAddress=sipStack.getIPAddress();                if (!stackIPAddress.equals(stackIPAddr) ) {                    GatewayDebug.println("GatewayDebug, getOppositeSipStack(), we return "+                    " the opposite stack");                    return sipStack;                }            }            GatewayDebug.println("GatewayDebug, getOppositeSipStack(), we return the default stack");            return ((SipStack)sipStacksList.firstElement());        }        catch(Exception e) {            if (GatewayDebug.debug) {                GatewayDebug.println("GatewayDebug, getOppositeSipStack(), internal error, "+                "exception raised:");                GatewayDebug.logException(e);            }            return null;        }    }            /*************************************************************************/    /************ The main method: to launch the proxy          *************/    /************************************************************************/            public static void main(String args[]) {        try{            // the Proxy:            //String confFile= (String) args[1];                        Gateway gateway=new Gateway(".//gov//nist//examples//busy//gateway//configuration//configuration_gateway.xml");            gateway.start();            GatewayDebug.println("Gateway ready to work");        }        catch(Exception e) {            System.out.println            ("ERROR: Set the configuration file flag: " +            "USE: -cf configuration_file_location.xml"  );            System.out.println("ERROR, the gateway can not be started, " +            " exception raised:\n");            e.printStackTrace();        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -