use of javax.sip.SipProvider in project load-balancer by RestComm.
the class TestSipListener method processRefer.
public void processRefer(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
try {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
logger.info("shootist: got a refer . ServerTxId = " + serverTransactionId);
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
inviteServerTid = st;
Dialog dialog = st.getDialog();
this.dialogCount++;
this.dialog = dialog;
logger.info("Shootme: dialog = " + dialog);
Response response = protocolObjects.messageFactory.createResponse(referResponseToSend, request);
sipETag = Integer.toString(new Random().nextInt(10000000));
st.sendResponse(response);
logger.info("shootist: Sending " + referResponseToSend);
List<Header> headers = new ArrayList<Header>();
EventHeader eventHeader = (EventHeader) protocolObjects.headerFactory.createHeader(EventHeader.NAME, "Refer");
headers.add(eventHeader);
if (sendNotifyForRefer) {
if (!referReceived) {
referReceived = true;
SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
headers.add(subscriptionStateHeader);
allMessagesContent.add("SIP/2.0 100 Trying");
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Trying", "message", "sipfrag;version=2.0", headers, null);
Thread.sleep(1000);
headers.remove(subscriptionStateHeader);
subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "terminated;reason=noresource");
headers.add(subscriptionStateHeader);
if (inviteRequest == null) {
ExtensionHeader extensionHeader = (ExtensionHeader) protocolObjects.headerFactory.createHeader("Out-Of-Dialog", "true");
headers.add(extensionHeader);
}
allMessagesContent.add("SIP/2.0 200 OK");
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 200 OK", "message", "sipfrag;version=2.0", headers, null);
} else {
SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
headers.add(subscriptionStateHeader);
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Subsequent", "message", "sipfrag;version=2.0", headers, null);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of javax.sip.SipProvider in project load-balancer by RestComm.
the class TestSipListener method processSubscribe.
/**
* Process the invite request.
*/
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
try {
logger.info("notifier: got an Subscribe sending OK");
logger.info("notifier: " + request);
logger.info("notifier : dialog = " + requestEvent.getDialog());
EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
// this.gotSubscribeRequest = true;
// Always create a ServerTransaction, best as early as possible in the code
Response response = null;
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
// Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
boolean isInitial = requestEvent.getDialog() == null;
if (isInitial) {
// JvB: need random tags to test forking
String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
response = protocolObjects.messageFactory.createResponse(202, request);
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
// Sanity check: to header should not ahve a tag. Else the dialog
// should have matched
// Application is supposed to set.
toHeader.setTag(toTag);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
} else {
response = protocolObjects.messageFactory.createResponse(202, request);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
}
// Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
else
address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
response.addHeader(contactHeader);
// Expires header is mandatory in 2xx responses to SUBSCRIBE
ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
if (expires == null) {
// rather short
expires = protocolObjects.headerFactory.createExpiresHeader(30);
}
response.addHeader(expires);
/*
* JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2:
* "[...] a NOTIFY message is always sent immediately after any 200-
* class response to a SUBSCRIBE request"
*
* Do this before creating the NOTIFY request below
*/
st.sendResponse(response);
/*
* NOTIFY requests MUST contain a "Subscription-State" header with a
* value of "active", "pending", or "terminated". The "active" value
* indicates that the subscription has been accepted and has been
* authorized (in most cases; see section 5.2.). The "pending" value
* indicates that the subscription has been received, but that
* policy information is insufficient to accept or deny the
* subscription at this time. The "terminated" value indicates that
* the subscription is not active.
*/
if (sendNotify) {
Request notifyRequest = dialog.createRequest("NOTIFY");
// Mark the contact header, to check that the remote contact is updated
// ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
// Initial state is pending, second time we assume terminated (Expires==0)
SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
allSubscriptionStates.add(sstate.getState().toLowerCase());
// Need a reason for terminated
if (sstate.getState().equalsIgnoreCase("terminated")) {
sstate.setReasonCode("deactivated");
}
notifyRequest.addHeader(sstate);
notifyRequest.setHeader(eventHeader);
notifyRequest.setHeader(contactHeader);
// notifyRequest.setHeader(routeHeader);
ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
Thread.sleep(timeToWaitBetweenSubsNotify);
}
// Let the other side know that the tx is pending acceptance
//
dialog.sendRequest(ct);
logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
logger.info("Dialog " + dialog);
logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
if (expires.getExpires() != 0) {
Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
myEventSource.start();
}
}
} catch (Throwable ex) {
logger.info(ex.getMessage(), ex);
}
}
use of javax.sip.SipProvider in project load-balancer by RestComm.
the class TestSipListener method processCancel.
private void processCancel(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
try {
cancelReceived = true;
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
Response response = protocolObjects.messageFactory.createResponse(Response.OK, request);
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
Dialog dialog = st.getDialog();
logger.info("Shootme: dialog = " + dialog);
st.sendResponse(response);
response = protocolObjects.messageFactory.createResponse(Response.REQUEST_TERMINATED, inviteRequest);
inviteServerTid.sendResponse(response);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending CANCEL responses", ex);
}
}
use of javax.sip.SipProvider in project load-balancer by RestComm.
the class TestSipListener method processNotify.
public void processNotify(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
SipProvider provider = (SipProvider) requestEvent.getSource();
Request notify = requestEvent.getRequest();
try {
logger.info("subscriber: got a notify count " + this.notifyCount++);
if (serverTransactionId == null) {
logger.info("subscriber: null TID.");
serverTransactionId = provider.getNewServerTransaction(notify);
}
Dialog dialog = serverTransactionId.getDialog();
// if ( dialog != subscriberDialog ) {
// if (forkedDialog == null) {
// forkedDialog = dialog;
// } else {
// AbstractSubsnotifyTestCase.assertTrue("Dialog should be either the subscriber dialog ",
// forkedDialog == dialog);
// }
// }
//
// this.dialogs.add(dialog);
logger.info("Dialog State = " + dialog.getState());
Response response = protocolObjects.messageFactory.createResponse(200, notify);
// SHOULD add a Contact
ContactHeader contact = (ContactHeader) contactHeader.clone();
((SipURI) contact.getAddress().getURI()).setParameter("id", "sub");
response.addHeader(contact);
logger.info("Transaction State = " + serverTransactionId.getState());
serverTransactionId.sendResponse(response);
logger.info("Dialog State = " + dialog.getState());
SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);
// Subscription is terminated?
String state = subscriptionState.getState();
allSubscriptionStates.add(state.toLowerCase());
if (notify.getRawContent() != null) {
this.lastMessageContent = new String(notify.getRawContent());
allMessagesContent.add(new String(lastMessageContent));
}
if (state.equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
if (subscriptionState.getReasonCode() == null) {
dialog.delete();
}
} else if (state.equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
if ("reg".equalsIgnoreCase(((EventHeader) notify.getHeader(EventHeader.NAME)).getEventType())) {
if (sendByeBeforeTerminatingNotify) {
dialog.terminateOnBye(false);
sendBye();
Thread.sleep(1000);
}
logger.info("Subscriber: sending unSUBSCRIBE");
// Else we end it ourselves
Request unsubscribe = dialog.createRequest(Request.SUBSCRIBE);
logger.info("dialog created:" + unsubscribe);
// SHOULD add a Contact (done by dialog), lets mark it to test updates
// ((SipURI) dialog.getLocalParty().getURI()).setParameter( "id", "unsub" );
ExpiresHeader expires = protocolObjects.headerFactory.createExpiresHeader(0);
unsubscribe.addHeader(expires);
// JvB note : stack should do this!
// copy
unsubscribe.addHeader(notify.getHeader(EventHeader.NAME));
// event
// header
logger.info("Sending Unsubscribe : " + unsubscribe);
logger.info("unsubscribe dialog " + dialog);
ClientTransaction ct = sipProvider.getNewClientTransaction(unsubscribe);
dialog.sendRequest(ct);
if (sendByeAfterTerminatingNotify) {
Thread.sleep(1000);
sendBye();
}
} else if (sendByeBeforeTerminatingNotify) {
sendBye();
}
} else {
// pending
logger.info("Subscriber: state now " + state);
}
} catch (Exception ex) {
logger.error("Unexpected exception", ex);
}
}
use of javax.sip.SipProvider in project load-balancer by RestComm.
the class TestSipListener method processInvite.
/**
* Process the invite request.
*/
public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
inviteReceived = true;
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
inviteRequest = request;
logger.info("shootme: got an Invite " + request);
try {
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
inviteServerTid = st;
Dialog dialog = st.getDialog();
if (request.getHeader(JoinHeader.NAME) != null) {
setJoinRequestReceived(true);
this.joinDialog = dialog;
} else if (request.getHeader(ReplacesHeader.NAME) != null) {
setReplacesRequestReceived(true);
this.replacesDialog = dialog;
} else {
this.dialogCount++;
this.dialog = dialog;
}
logger.info("Shootme: dialog = " + dialog);
this.inviteRequest = request;
boolean sendReliably = false;
RequireHeader requireHeader = (RequireHeader) request.getHeader(RequireHeader.NAME);
if (requireHeader != null && "100rel".equalsIgnoreCase(requireHeader.getOptionTag().trim())) {
sendReliably = true;
}
if (respondWithError == null)
for (int provisionalResponseToSend : provisionalResponsesToSend) {
Thread.sleep(getTimeToWaitBetweenProvisionnalResponse());
Response response = protocolObjects.messageFactory.createResponse(provisionalResponseToSend, request);
if (provisionalResponseToSend >= Response.TRYING && provisionalResponseToSend < Response.OK) {
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
if (provisionalResponseToSend != Response.TRYING && toHeader.getTag() == null) {
// Application is supposed to set.
toHeader.setTag(TO_TAG);
}
if (sendReliably && provisionalResponseToSend != Response.TRYING) {
requireHeader = protocolObjects.headerFactory.createRequireHeader("100rel");
response.addHeader(requireHeader);
Header rseqHeader = protocolObjects.headerFactory.createRSeqHeader(rseqNumber.getAndIncrement());
response.addHeader(rseqHeader);
dialog.sendReliableProvisionalResponse(response);
} else {
st.sendResponse(response);
eventListener.uasAfterResponse(response.getStatusCode(), appServer);
}
}
}
if (respondWithError != null && !sendReliably) {
Response response = protocolObjects.messageFactory.createResponse(respondWithError, request);
response.setReasonPhrase("Unable to setup media services");
st.sendResponse(response);
eventListener.uasAfterResponse(response.getStatusCode(), appServer);
return;
}
ContactHeader contactHeader = (ContactHeader) request.getHeader(ContactHeader.NAME);
if (contactHeader != null && "0.0.0.0".equals(((SipURI) contactHeader.getAddress().getURI()).getHost())) {
abortProcessing = true;
throw new IllegalArgumentException("we received a contact header with 0.0.0.0 in an INVITE !");
}
if (!waitForCancel) {
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Shootme <sip:127.0.0.1:" + myPort + ";transport=" + protocolObjects.transport + ">");
else
address = protocolObjects.addressFactory.createAddress("Shootme <sip:[::1]:" + myPort + ";transport=" + protocolObjects.transport + ">");
contactHeader = protocolObjects.headerFactory.createContactHeader(address);
setFinalResponse(protocolObjects.messageFactory.createResponse(finalResponseToSend, request));
if (testAckViaParam) {
ViaHeader viaHeader = (ViaHeader) getFinalResponse().getHeader(ViaHeader.NAME);
viaHeader.setParameter("testAckViaParam", "true");
}
ToHeader toHeader = (ToHeader) getFinalResponse().getHeader(ToHeader.NAME);
if (toHeader.getTag() == null) {
// Application is supposed to set.
toHeader.setTag(TO_TAG);
}
getFinalResponse().addHeader(contactHeader);
if (!sendReliably) {
Thread.sleep(2000);
st.sendResponse(getFinalResponse());
eventListener.uasAfterResponse(getFinalResponse().getStatusCode(), appServer);
}
} else {
logger.info("Waiting for CANCEL, stopping the INVITE processing ");
return;
}
if (("join").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
sendJoinMessage = true;
}
if (("replaces").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
sendReplacesMessage = true;
}
} catch (Exception ex) {
logger.error("unexpected exception", ex);
throw new RuntimeException(ex);
}
}
Aggregations