use of gov.nist.javax.sip.ListeningPointImpl in project XobotOS by xamarin.
the class SIPDialog method createAck.
/*
* (non-Javadoc) The UAC core MUST generate an ACK request for each 2xx received from the
* transaction layer. The header fields of the ACK are constructed in the same way as for any
* request sent within a dialog (see Section 12) with the exception of the CSeq and the header
* fields related to authentication. The sequence number of the CSeq header field MUST be the
* same as the INVITE being acknowledged, but the CSeq method MUST be ACK. The ACK MUST
* contain the same credentials as the INVITE. If the 2xx contains an offer (based on the
* rules above), the ACK MUST carry an answer in its body. If the offer in the 2xx response is
* not acceptable, the UAC core MUST generate a valid answer in the ACK and then send a BYE
* immediately.
*
* Note that for the case of forked requests, you can create multiple outgoing invites each
* with a different cseq and hence you need to supply the invite.
*
* @see javax.sip.Dialog#createAck(long)
*/
public Request createAck(long cseqno) throws InvalidArgumentException, SipException {
// then send INVITE+ACK later on
if (!method.equals(Request.INVITE))
throw new SipException("Dialog was not created with an INVITE" + method);
if (cseqno <= 0)
throw new InvalidArgumentException("bad cseq <= 0 ");
else if (cseqno > ((((long) 1) << 32) - 1))
throw new InvalidArgumentException("bad cseq > " + ((((long) 1) << 32) - 1));
if (this.remoteTarget == null) {
throw new SipException("Cannot create ACK - no remote Target!");
}
if (this.sipStack.isLoggingEnabled()) {
this.sipStack.getStackLogger().logDebug("createAck " + this + " cseqno " + cseqno);
}
// out of order ACK sending. Old ACKs seqno's can always be ACKed.
if (lastInviteOkReceived < cseqno) {
if (sipStack.isLoggingEnabled()) {
this.sipStack.getStackLogger().logDebug("WARNING : Attempt to crete ACK without OK " + this);
this.sipStack.getStackLogger().logDebug("LAST RESPONSE = " + this.lastResponse);
}
throw new SipException("Dialog not yet established -- no OK response!");
}
try {
// JvB: Transport from first entry in route set, or remote Contact
// if none
// Only used to find correct LP & create correct Via
SipURI uri4transport = null;
if (this.routeList != null && !this.routeList.isEmpty()) {
Route r = (Route) this.routeList.getFirst();
uri4transport = ((SipURI) r.getAddress().getURI());
} else {
// should be !=null, checked above
uri4transport = ((SipURI) this.remoteTarget.getURI());
}
String transport = uri4transport.getTransportParam();
if (transport == null) {
// JvB fix: also support TLS
transport = uri4transport.isSecure() ? ListeningPoint.TLS : ListeningPoint.UDP;
}
ListeningPointImpl lp = (ListeningPointImpl) sipProvider.getListeningPoint(transport);
if (lp == null) {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logError("remoteTargetURI " + this.remoteTarget.getURI());
sipStack.getStackLogger().logError("uri4transport = " + uri4transport);
sipStack.getStackLogger().logError("No LP found for transport=" + transport);
}
throw new SipException("Cannot create ACK - no ListeningPoint for transport towards next hop found:" + transport);
}
SIPRequest sipRequest = new SIPRequest();
sipRequest.setMethod(Request.ACK);
sipRequest.setRequestURI((SipUri) getRemoteTarget().getURI().clone());
sipRequest.setCallId(this.callIdHeader);
sipRequest.setCSeq(new CSeq(cseqno, Request.ACK));
List<Via> vias = new ArrayList<Via>();
// Via via = lp.getViaHeader();
// The user may have touched the sentby for the response.
// so use the via header extracted from the response for the ACK =>
// https://jain-sip.dev.java.net/issues/show_bug.cgi?id=205
// strip the params from the via of the response and use the params from the
// original request
Via via = this.lastResponse.getTopmostVia();
via.removeParameters();
if (originalRequest != null && originalRequest.getTopmostVia() != null) {
NameValueList originalRequestParameters = originalRequest.getTopmostVia().getParameters();
if (originalRequestParameters != null && originalRequestParameters.size() > 0) {
via.setParameters((NameValueList) originalRequestParameters.clone());
}
}
// new branch
via.setBranch(Utils.getInstance().generateBranchId());
vias.add(via);
sipRequest.setVia(vias);
From from = new From();
from.setAddress(this.localParty);
from.setTag(this.myTag);
sipRequest.setFrom(from);
To to = new To();
to.setAddress(this.remoteParty);
if (hisTag != null)
to.setTag(this.hisTag);
sipRequest.setTo(to);
sipRequest.setMaxForwards(new MaxForwards(70));
if (this.originalRequest != null) {
Authorization authorization = this.originalRequest.getAuthorization();
if (authorization != null)
sipRequest.setHeader(authorization);
}
// ACKs for 2xx responses
// use the Route values learned from the Record-Route of the 2xx
// responses.
this.updateRequest(sipRequest);
return sipRequest;
} catch (Exception ex) {
InternalErrorHandler.handleException(ex);
throw new SipException("unexpected exception ", ex);
}
}
use of gov.nist.javax.sip.ListeningPointImpl in project XobotOS by xamarin.
the class SIPDialog method sendAck.
/**
* Sends ACK Request to the remote party of this Dialogue.
*
*
* @param request the new ACK Request message to send.
* @param throwIOExceptionAsSipException - throws SipException if IOEx encountered. Otherwise,
* no exception is propagated.
* @param releaseAckSem - release ack semaphore.
* @throws SipException if implementation cannot send the ACK Request for any other reason
*
*/
private void sendAck(Request request, boolean throwIOExceptionAsSipException) throws SipException {
SIPRequest ackRequest = (SIPRequest) request;
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logDebug("sendAck" + this);
if (!ackRequest.getMethod().equals(Request.ACK))
throw new SipException("Bad request method -- should be ACK");
if (this.getState() == null || this.getState().getValue() == EARLY_STATE) {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logError("Bad Dialog State for " + this + " dialogID = " + this.getDialogId());
}
throw new SipException("Bad dialog state " + this.getState());
}
if (!this.getCallId().getCallId().equals(((SIPRequest) request).getCallId().getCallId())) {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logError("CallID " + this.getCallId());
sipStack.getStackLogger().logError("RequestCallID = " + ackRequest.getCallId().getCallId());
sipStack.getStackLogger().logError("dialog = " + this);
}
throw new SipException("Bad call ID in request");
}
try {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logDebug("setting from tag For outgoing ACK= " + this.getLocalTag());
sipStack.getStackLogger().logDebug("setting To tag for outgoing ACK = " + this.getRemoteTag());
sipStack.getStackLogger().logDebug("ack = " + ackRequest);
}
if (this.getLocalTag() != null)
ackRequest.getFrom().setTag(this.getLocalTag());
if (this.getRemoteTag() != null)
ackRequest.getTo().setTag(this.getRemoteTag());
} catch (ParseException ex) {
throw new SipException(ex.getMessage());
}
Hop hop = sipStack.getNextHop(ackRequest);
// Hop hop = defaultRouter.getNextHop(ackRequest);
if (hop == null)
throw new SipException("No route!");
try {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logDebug("hop = " + hop);
ListeningPointImpl lp = (ListeningPointImpl) this.sipProvider.getListeningPoint(hop.getTransport());
if (lp == null)
throw new SipException("No listening point for this provider registered at " + hop);
InetAddress inetAddress = InetAddress.getByName(hop.getHost());
MessageChannel messageChannel = lp.getMessageProcessor().createMessageChannel(inetAddress, hop.getPort());
boolean releaseAckSem = false;
long cseqNo = ((SIPRequest) request).getCSeq().getSeqNumber();
if (!this.isAckSent(cseqNo)) {
releaseAckSem = true;
}
this.setLastAckSent(ackRequest);
messageChannel.sendMessage(ackRequest);
// Sent atleast one ACK.
this.isAcknowledged = true;
this.highestSequenceNumberAcknowledged = Math.max(this.highestSequenceNumberAcknowledged, ((SIPRequest) ackRequest).getCSeq().getSeqNumber());
if (releaseAckSem && this.isBackToBackUserAgent) {
this.releaseAckSem();
} else {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logDebug("Not releasing ack sem for " + this + " isAckSent " + releaseAckSem);
}
}
} catch (IOException ex) {
if (throwIOExceptionAsSipException)
throw new SipException("Could not send ack", ex);
this.raiseIOException(hop.getHost(), hop.getPort(), hop.getTransport());
} catch (SipException ex) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logException(ex);
throw ex;
} catch (Exception ex) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logException(ex);
throw new SipException("Could not create message channel", ex);
}
if (this.dialogDeleteTask != null) {
this.dialogDeleteTask.cancel();
this.dialogDeleteTask = null;
}
this.ackSeen = true;
}
use of gov.nist.javax.sip.ListeningPointImpl in project jain-sip.ha by RestComm.
the class SimpleB2BUAHandler method createRequest.
@SuppressWarnings("unchecked")
private Request createRequest(Request origRequest, int peerPort) throws SipException {
final SIPRequest request = (SIPRequest) origRequest.clone();
try {
long l = new AtomicLong().incrementAndGet();
request.getFromHeader().setTag(Long.toString(l));
} catch (ParseException e1) {
throw new SipException("failed to set local tag", e1);
}
final String transport = request.getTopmostViaHeader().getTransport();
final ListeningPointImpl listeningPointImpl = (ListeningPointImpl) sipProvider.getListeningPoint(transport);
final ViaList viaList = new ViaList();
viaList.add((Via) listeningPointImpl.createViaHeader());
request.setVia(viaList);
try {
request.setHeader(headerFactory.createMaxForwardsHeader(70));
} catch (InvalidArgumentException e) {
throw new SipException("Failed to create max forwards header", e);
}
request.setHeader((Header) sipProvider.getNewCallId());
// set contact if the original response had it
if (origRequest.getHeader(ContactHeader.NAME) != null) {
request.setHeader(listeningPointImpl.createContactHeader());
}
/*
* Route header fields of the upstream request MAY be copied in the
* downstream request, except the topmost Route header if it is under
* the responsibility of the B2BUA. Additional Route header fields MAY
* also be added to the downstream request.
*/
if (getOutgoingDialog() == null || getOutgoingDialog().getState() == null) {
// first request, no route available
final RouteList routeList = request.getRouteHeaders();
if (routeList != null) {
final RouteHeader topRoute = routeList.get(0);
final URI topRouteURI = topRoute.getAddress().getURI();
if (topRouteURI.isSipURI()) {
final SipURI topRouteSipURI = (SipURI) topRouteURI;
if (topRouteSipURI.getHost().equals(listeningPointImpl.getIPAddress()) && topRouteSipURI.getPort() == listeningPointImpl.getPort()) {
if (routeList.size() > 1) {
routeList.remove(0);
} else {
request.removeHeader(RouteHeader.NAME);
}
}
}
}
} else {
// replace route in orig request with the one in dialog
request.removeHeader(RouteHeader.NAME);
final RouteList routeList = new RouteList();
for (Iterator<Route> it = getOutgoingDialog().getRouteSet(); it.hasNext(); ) {
Route route = it.next();
routeList.add(route);
}
if (!routeList.isEmpty()) {
request.addHeader(routeList);
}
}
/*
* Record-Route header fields of the upstream request are not copied in
* the new downstream request, as Record-Route is only meaningful for
* the upstream dialog.
*/
request.removeHeader(RecordRouteHeader.NAME);
((SipURI) request.getRequestURI()).setPort(peerPort);
return request;
}
use of gov.nist.javax.sip.ListeningPointImpl in project XobotOS by xamarin.
the class SIPDialog method createRequest.
/**
* The method that actually does the work of creating a request.
*
* @param method
* @param response
* @return
* @throws SipException
*/
private Request createRequest(String method, SIPResponse sipResponse) throws SipException {
if (method == null || sipResponse == null)
throw new NullPointerException("null argument");
if (method.equals(Request.CANCEL))
throw new SipException("Dialog.createRequest(): Invalid request");
if (this.getState() == null || (this.getState().getValue() == TERMINATED_STATE && !method.equalsIgnoreCase(Request.BYE)) || (this.isServer() && this.getState().getValue() == EARLY_STATE && method.equalsIgnoreCase(Request.BYE)))
throw new SipException("Dialog " + getDialogId() + " not yet established or terminated " + this.getState());
SipUri sipUri = null;
if (this.getRemoteTarget() != null)
sipUri = (SipUri) this.getRemoteTarget().getURI().clone();
else {
sipUri = (SipUri) this.getRemoteParty().getURI().clone();
sipUri.clearUriParms();
}
CSeq cseq = new CSeq();
try {
cseq.setMethod(method);
cseq.setSeqNumber(this.getLocalSeqNumber());
} catch (Exception ex) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logError("Unexpected error");
InternalErrorHandler.handleException(ex);
}
/*
* Add a via header for the outbound request based on the transport of the message
* processor.
*/
ListeningPointImpl lp = (ListeningPointImpl) this.sipProvider.getListeningPoint(sipResponse.getTopmostVia().getTransport());
if (lp == null) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logError("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
throw new SipException("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
}
Via via = lp.getViaHeader();
From from = new From();
from.setAddress(this.localParty);
To to = new To();
to.setAddress(this.remoteParty);
SIPRequest sipRequest = sipResponse.createRequest(sipUri, via, cseq, from, to);
if (SIPRequest.isTargetRefresh(method)) {
ContactHeader contactHeader = ((ListeningPointImpl) this.sipProvider.getListeningPoint(lp.getTransport())).createContactHeader();
((SipURI) contactHeader.getAddress().getURI()).setSecure(this.isSecure());
sipRequest.setHeader(contactHeader);
}
try {
/*
* Guess of local sequence number - this is being re-set when the request is actually
* dispatched
*/
cseq = (CSeq) sipRequest.getCSeq();
cseq.setSeqNumber(this.localSequenceNumber + 1);
} catch (InvalidArgumentException ex) {
InternalErrorHandler.handleException(ex);
}
if (method.equals(Request.SUBSCRIBE)) {
if (eventHeader != null)
sipRequest.addHeader(eventHeader);
}
try {
if (this.getLocalTag() != null) {
from.setTag(this.getLocalTag());
} else {
from.removeTag();
}
if (this.getRemoteTag() != null) {
to.setTag(this.getRemoteTag());
} else {
to.removeTag();
}
} catch (ParseException ex) {
InternalErrorHandler.handleException(ex);
}
// get the route list from the dialog.
this.updateRequest(sipRequest);
return sipRequest;
}
Aggregations