callprocessing.java

来自「java 开发的sip软电话 源码 jain sip」· Java 代码 · 共 1,497 行 · 第 1/4 页

JAVA
1,497
字号
            try {
                dialog.sendRequest(clientTransaction);
                    
            }
            catch (SipException ex1) {
                throw new CommunicationsException("Failed to send the INFO request");
            }
        }
        finally
        {
            
        }

    }  
    // send message
    private void sendInfoMessage(Dialog dialog, String body) throws CommunicationsException
    {
        try
        {
            
            Request request = dialog.getFirstTransaction().getRequest();
            Request info = null;
            String contentType="application/dtmd-relay";
            String[] contentTypeTab = contentType.split("/");
            ContentTypeHeader contentTypeHeader = null;
            try {
                info = dialog.createRequest(Request.INFO);
                try {
                contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
                info.setContent(body,contentTypeHeader);
                }catch (ParseException ex) {
                
                throw new CommunicationsException("ContentType Header must look like type/subtype!", ex);
                }
            }
            catch (SipException ex) {
                
                throw new CommunicationsException("Failed to create bye request!", ex);
            }
            ClientTransaction clientTransaction = null;
            try {
                clientTransaction =
                    sipManCallback.sipProvider.getNewClientTransaction(info);
            }
            catch (TransactionUnavailableException ex) {
                
                throw new CommunicationsException("Failed to construct a client transaction from the INFO request", ex);
            }
            try {
                dialog.sendRequest(clientTransaction);                   
            }
            catch (SipException ex1) {
                throw new CommunicationsException("Failed to send the INFO request");
            }
        }
        finally
        {
            
        }

    }  // send message
    //Bye
    private void sayBye(Dialog dialog) throws CommunicationsException
    {
        try
        {
            
            Request request = dialog.getFirstTransaction().getRequest();
            Request bye = null;
            try {
                bye = dialog.createRequest(Request.BYE);
            }
            catch (SipException ex) {
                throw new CommunicationsException(
                    "Failed to create bye request!",
                    ex);
            }
            ClientTransaction clientTransaction = null;
            try {
                clientTransaction =
                    sipManCallback.sipProvider.getNewClientTransaction(bye);
            }
            catch (TransactionUnavailableException ex) {
                throw new CommunicationsException(
                    "Failed to construct a client transaction from the BYE request",
                    ex);
            }
            try {
                dialog.sendRequest(clientTransaction);
                    
            }
            catch (SipException ex1) {
                throw new CommunicationsException("Failed to send the BYE request");
            }
        }
        finally
        {
            
        }

    } //bye

    //cancel
    private void sayCancel(Dialog dialog) throws CommunicationsException
    {
        try
        {
            

            Request request = dialog.getFirstTransaction().getRequest();
            if (dialog.isServer()) {
                
                throw new CommunicationsException(
                    "Cannot cancel a server transaction");
            }
            ClientTransaction clientTransaction =
                (ClientTransaction) dialog.getFirstTransaction();
            try {
                Request cancel = clientTransaction.createCancel();
                ClientTransaction cancelTransaction =
                    sipManCallback.sipProvider.getNewClientTransaction(cancel);
                cancelTransaction.sendRequest();
            }
            catch (SipException ex) {
                
                throw new CommunicationsException(
                    "Failed to send the CANCEL request", ex);
            }
        }
        finally
        {
            
        }

    } //cancel

    //busy here
    private void sayBusyHere(Dialog dialog) throws CommunicationsException
    {
        try
        {
            

            Request request = dialog.getFirstTransaction().getRequest();
            Response busyHere = null;
            try {
                busyHere = sipManCallback.
                    messageFactory.createResponse(Response.BUSY_HERE, request);
                sipManCallback.attachToTag(busyHere, dialog);
            }
            catch (ParseException ex) {
                
                throw new CommunicationsException(
                    "Failed to create the BUSY_HERE response!", ex);
            }
            if (!dialog.isServer()) {
                
                throw new CommunicationsException(
                    "Cannot send BUSY_HERE in a client transaction");
            }
            ServerTransaction serverTransaction =
                (ServerTransaction) dialog.getFirstTransaction();
            try {
                serverTransaction.sendResponse(busyHere);
                    
            }
            catch (SipException ex) {
                
                throw new CommunicationsException(
                    "Failed to send the BUSY_HERE response", ex);
            }
        }
        finally
        {
            
        }

    } //busy here

    //------------------ say ok
    public void sayOK(int callID, String sdpContent) throws
        CommunicationsException
    {
        try
        {
            

            Call call = callDispatcher.getCall(callID);
            if (call == null) {
                
                throw new CommunicationsException(
                    "Failed to find call with id=" + callID);
            }
            Dialog dialog = call.getDialog();
            if (dialog == null) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "Failed to extract call's associated dialog! Ending Call!");
            }
            Transaction transaction = dialog.getFirstTransaction();
            if (transaction == null  || !dialog.isServer()) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "Failed to extract a ServerTransaction "
                    +"from the call's associated dialog!");
            }
            ServerTransaction serverTransaction = (ServerTransaction) transaction;
            Response ok = null;
            try {
                ok = sipManCallback.messageFactory.createResponse(
                    Response.OK,
                    dialog.getFirstTransaction().getRequest());
                sipManCallback.attachToTag(ok, dialog);
            }
            catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "Failed to construct an OK response to an INVITE request",
                    ex);
            }
            //Content
            ContentTypeHeader contentTypeHeader = null;
            try {
                //content type should be application/sdp (not applications)
                //reported by Oleg Shevchenko (Miratech)
                contentTypeHeader =
                    sipManCallback.headerFactory.createContentTypeHeader(
                    "application", "sdp");
            }
            catch (ParseException ex) {
                //Shouldn't happen
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "Failed to create a content type header for the OK request",
                    ex);
            }
            try {
                ok.setContent(sdpContent, contentTypeHeader);
            }
            catch (NullPointerException ex) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "No sdp data was provided for the ok response to an INVITE request!",
                    ex);
            }
            catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                    "Failed to parse sdp data while creating invite request!", ex);
            }
            //TODO This is here provisionally as my remote user agent that I am using for
            //testing is not doing it. It is not correct from the protocol point of view
            //and should probably be removed
            if ( ( (ToHeader) ok.getHeader(ToHeader.NAME)).getTag() == null) {
                try {
                    ( (ToHeader) ok.getHeader(ToHeader.NAME)).setTag(Integer.
                        toString(dialog.hashCode()));
                }
                catch (ParseException ex) {
                    call.setState(Call.DISCONNECTED);
                    throw new CommunicationsException(
                        "Unable to set to tag",
                        ex
                        );
                }
            }
            ContactHeader contactHeader = sipManCallback.getContactHeader();
            ok.addHeader(contactHeader);
            try {
                serverTransaction.sendResponse(ok);
                    
            }
            catch (SipException ex) {
                call.setState(Call.DISCONNECTED);
               
                throw new CommunicationsException(
                    "Failed to send an OK response to an INVITE request",
                    ex
                    );
            }
        }
        finally
        {
            
        }

    } //answer call

    //------------------ Internal Error
    void sayInternalError(int callID) throws CommunicationsException
    {
        try
        {
            

            Call call = callDispatcher.getCall(callID);
            if (call == null) {
                
                throw new CommunicationsException(
                    "Failed to find call with id=" + callID
                    );
            }
            Dialog dialog = call.getDialog();
            if (dialog == null) {
                call.setState(Call.DISCONNECTED);
                 throw new CommunicationsException(
                    "Failed to extract call's associated dialog! Ending Call!"
                    );
            }
            Transaction transaction = dialog.getFirstTransaction();
            if (transaction == null || !dialog.isServer()) {
                call.setState(Call.DISCONNECTED);
                  throw new CommunicationsException(
                    "Failed to extract a transaction from the call's associated dialog!"
                    );
            }
            ServerTransaction serverTransaction = (ServerTransaction) transaction;
            Response internalError = null;
            try {
                internalError = sipManCallback.messageFactory.createResponse(
                    Response.SERVER_INTERNAL_ERROR,
                    dialog.getFirstTransaction().getRequest());
                sipManCallback.attachToTag(internalError, dialog);
            }
            catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                 throw new CommunicationsException(
                    "Failed to construct an OK response to an INVITE request",
                    ex);
            }
            ContactHeader contactHeader = sipManCallback.getContactHeader();
            internalError.addHeader(contactHeader);
            try {
                serverTransaction.sendResponse(internalError);
                    
            }
            catch (SipException ex) {
                call.setState(Call.DISCONNECTED);
                 throw new CommunicationsException(
                    "Failed to send an OK response to an INVITE request",
                    ex
                    );
            }
        }
        finally
        {
            
        }

    } //internal error

    CallDispatcher getCallDispatcher()
    {
        return callDispatcher;
    }

    //The following method is currently being implemented and tested
    protected void processReInvite(ServerTransaction serverTransaction, Request request)
    {
        try
        {
            

            
        }finally
        {
            
        }

    }
}

⌨️ 快捷键说明

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