use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.
the class SipProviderImpl method sendResponse.
/*
* (non-Javadoc)
*
* @see javax.sip.SipProvider#sendResponse(javax.sip.message.Response)
*/
public void sendResponse(Response response) throws SipException {
if (!sipStack.isAlive())
throw new SipException("Stack is stopped");
SIPResponse sipResponse = (SIPResponse) response;
Via via = sipResponse.getTopmostVia();
if (via == null)
throw new SipException("No via header in response!");
SIPServerTransaction st = (SIPServerTransaction) sipStack.findTransaction((SIPMessage) response, true);
if (st != null && st.getState() != TransactionState.TERMINATED && this.isAutomaticDialogSupportEnabled()) {
throw new SipException("Transaction exists -- cannot send response statelessly");
}
String transport = via.getTransport();
// check to see if Via has "received paramaeter". If so
// set the host to the via parameter. Else set it to the
// Via host.
String host = via.getReceived();
if (host == null)
host = via.getHost();
// Symmetric nat support
int port = via.getRPort();
if (port == -1) {
port = via.getPort();
if (port == -1) {
if (transport.equalsIgnoreCase("TLS"))
port = 5061;
else
port = 5060;
}
}
// for correct management of IPv6 addresses.
if (host.indexOf(":") > 0)
if (host.indexOf("[") < 0)
host = "[" + host + "]";
Hop hop = sipStack.getAddressResolver().resolveAddress(new HopImpl(host, port, transport));
try {
ListeningPointImpl listeningPoint = (ListeningPointImpl) this.getListeningPoint(transport);
if (listeningPoint == null)
throw new SipException("whoopsa daisy! no listening point found for transport " + transport);
MessageChannel messageChannel = sipStack.createRawMessageChannel(this.getListeningPoint(hop.getTransport()).getIPAddress(), listeningPoint.port, hop);
messageChannel.sendMessage(sipResponse);
} catch (IOException ex) {
throw new SipException(ex.getMessage());
}
}
use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.
the class SipProviderImpl method sendRequest.
/*
* (non-Javadoc)
*
* @see javax.sip.SipProvider#sendRequest(javax.sip.message.Request)
*/
public void sendRequest(Request request) throws SipException {
if (!sipStack.isAlive())
throw new SipException("Stack is stopped.");
// message.
if (((SIPRequest) request).getRequestLine() != null && request.getMethod().equals(Request.ACK)) {
Dialog dialog = sipStack.getDialog(((SIPRequest) request).getDialogId(false));
if (dialog != null && dialog.getState() != null) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logWarning("Dialog exists -- you may want to use Dialog.sendAck() " + dialog.getState());
}
}
Hop hop = sipStack.getRouter((SIPRequest) request).getNextHop(request);
if (hop == null)
throw new SipException("could not determine next hop!");
SIPRequest sipRequest = (SIPRequest) request;
// Null request is used to send default proxy keepalive messages.
if ((!sipRequest.isNullRequest()) && sipRequest.getTopmostVia() == null)
throw new SipException("Invalid SipRequest -- no via header!");
try {
/*
* JvB: Via branch should already be OK, dont touch it here? Some
* apps forward statelessly, and then it's not set. So set only when
* not set already, dont overwrite CANCEL branch here..
*/
if (!sipRequest.isNullRequest()) {
Via via = sipRequest.getTopmostVia();
String branch = via.getBranch();
if (branch == null || branch.length() == 0) {
via.setBranch(sipRequest.getTransactionId());
}
}
MessageChannel messageChannel = null;
if (this.listeningPoints.containsKey(hop.getTransport().toUpperCase()))
messageChannel = sipStack.createRawMessageChannel(this.getListeningPoint(hop.getTransport()).getIPAddress(), this.getListeningPoint(hop.getTransport()).getPort(), hop);
if (messageChannel != null) {
messageChannel.sendMessage((SIPMessage) sipRequest, hop);
} else {
throw new SipException("Could not create a message channel for " + hop.toString());
}
} catch (IOException ex) {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logException(ex);
}
throw new SipException("IO Exception occured while Sending Request", ex);
} catch (ParseException ex1) {
InternalErrorHandler.handleException(ex1);
} finally {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logDebug("done sending " + request.getMethod() + " to hop " + hop);
}
}
use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.
the class SipProviderImpl method getNewServerTransaction.
/*
* (non-Javadoc)
*
* @see javax.sip.SipProvider#getNewServerTransaction(javax.sip.message.Request)
*/
public ServerTransaction getNewServerTransaction(Request request) throws TransactionAlreadyExistsException, TransactionUnavailableException {
if (!sipStack.isAlive())
throw new TransactionUnavailableException("Stack is stopped");
SIPServerTransaction transaction = null;
SIPRequest sipRequest = (SIPRequest) request;
try {
sipRequest.checkHeaders();
} catch (ParseException ex) {
throw new TransactionUnavailableException(ex.getMessage(), ex);
}
if (request.getMethod().equals(Request.ACK)) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logError("Creating server transaction for ACK -- makes no sense!");
throw new TransactionUnavailableException("Cannot create Server transaction for ACK ");
}
/*
* Got a notify.
*/
if (sipRequest.getMethod().equals(Request.NOTIFY) && sipRequest.getFromTag() != null && sipRequest.getToTag() == null) {
SIPClientTransaction ct = sipStack.findSubscribeTransaction(sipRequest, (ListeningPointImpl) this.getListeningPoint());
/* Issue 104 */
if (ct == null && !sipStack.deliverUnsolicitedNotify) {
throw new TransactionUnavailableException("Cannot find matching Subscription (and gov.nist.javax.sip.DELIVER_UNSOLICITED_NOTIFY not set)");
}
}
if (!sipStack.acquireSem()) {
throw new TransactionUnavailableException("Transaction not available -- could not acquire stack lock");
}
try {
if (sipStack.isDialogCreated(sipRequest.getMethod())) {
if (sipStack.findTransaction((SIPRequest) request, true) != null)
throw new TransactionAlreadyExistsException("server transaction already exists!");
transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
if (transaction == null)
throw new TransactionUnavailableException("Transaction not available");
if (transaction.getOriginalRequest() == null)
transaction.setOriginalRequest(sipRequest);
try {
sipStack.addTransaction(transaction);
} catch (IOException ex) {
throw new TransactionUnavailableException("Error sending provisional response");
}
// So I can handle timeouts.
transaction.addEventListener(this);
if (isAutomaticDialogSupportEnabled()) {
// If automatic dialog support is enabled then
// this tx gets his own dialog.
String dialogId = sipRequest.getDialogId(true);
SIPDialog dialog = sipStack.getDialog(dialogId);
if (dialog == null) {
dialog = sipStack.createDialog(transaction);
}
transaction.setDialog(dialog, sipRequest.getDialogId(true));
if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
sipStack.putInMergeTable(transaction, sipRequest);
}
dialog.addRoute(sipRequest);
if (dialog.getRemoteTag() != null && dialog.getLocalTag() != null) {
this.sipStack.putDialog(dialog);
}
}
} else {
if (isAutomaticDialogSupportEnabled()) {
/*
* Under automatic dialog support, dialog is tied into a transaction. You cannot
* create a server tx except for dialog creating transactions. After that, all
* subsequent transactions are created for you by the stack.
*/
transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
if (transaction != null)
throw new TransactionAlreadyExistsException("Transaction exists! ");
transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
if (transaction == null)
throw new TransactionUnavailableException("Transaction not available!");
if (transaction.getOriginalRequest() == null)
transaction.setOriginalRequest(sipRequest);
// Map the transaction.
try {
sipStack.addTransaction(transaction);
} catch (IOException ex) {
throw new TransactionUnavailableException("Could not send back provisional response!");
}
// If there is a dialog already assigned then just update the
// dialog state.
String dialogId = sipRequest.getDialogId(true);
SIPDialog dialog = sipStack.getDialog(dialogId);
if (dialog != null) {
dialog.addTransaction(transaction);
dialog.addRoute(sipRequest);
transaction.setDialog(dialog, sipRequest.getDialogId(true));
}
} else {
transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
if (transaction != null)
throw new TransactionAlreadyExistsException("Transaction exists! ");
transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
if (transaction != null) {
if (transaction.getOriginalRequest() == null)
transaction.setOriginalRequest(sipRequest);
// Map the transaction.
sipStack.mapTransaction(transaction);
// If there is a dialog already assigned then just
// assign the dialog to the transaction.
String dialogId = sipRequest.getDialogId(true);
SIPDialog dialog = sipStack.getDialog(dialogId);
if (dialog != null) {
dialog.addTransaction(transaction);
dialog.addRoute(sipRequest);
transaction.setDialog(dialog, sipRequest.getDialogId(true));
}
return transaction;
} else {
// tx does not exist so create the tx.
MessageChannel mc = (MessageChannel) sipRequest.getMessageChannel();
transaction = sipStack.createServerTransaction(mc);
if (transaction == null)
throw new TransactionUnavailableException("Transaction unavailable -- too many servrer transactions");
transaction.setOriginalRequest(sipRequest);
sipStack.mapTransaction(transaction);
// If there is a dialog already assigned then just
// assign the dialog to the transaction.
String dialogId = sipRequest.getDialogId(true);
SIPDialog dialog = sipStack.getDialog(dialogId);
if (dialog != null) {
dialog.addTransaction(transaction);
dialog.addRoute(sipRequest);
transaction.setDialog(dialog, sipRequest.getDialogId(true));
}
return transaction;
}
}
}
return transaction;
} finally {
sipStack.releaseSem();
}
}
Aggregations