use of javax.sip.address.SipURI 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.address.SipURI 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);
}
}
use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class TestSipListener method processAck.
public void processAck(Request request, ServerTransaction serverTransactionId) {
try {
logger.info("shootist: got a " + request);
logger.info("shootist: got an ACK. ServerTxId = " + serverTransactionId);
ackReceived = true;
// we don't count retransmissions
if (serverTransactionId != null) {
ackCount++;
}
if (testAckViaParam) {
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String param = viaHeader.getParameter("testAckViaParam");
if (param != null) {
abortProcessing = true;
logger.error("the Via Param set in the response shouldn't be present in the ACK");
return;
}
}
if (sendBye) {
Thread.sleep(timeToWaitBeforeBye);
if (serverTransactionId != null && serverTransactionId.getDialog() != null) {
Request byeRequest = serverTransactionId.getDialog().createRequest(Request.BYE);
logger.info("BYE created : " + byeRequest);
ClientTransaction ct = sipProvider.getNewClientTransaction(byeRequest);
logger.info("Sending BYE : " + byeRequest);
serverTransactionId.getDialog().sendRequest(ct);
logger.info("Dialog State = " + serverTransactionId.getDialog().getState());
}
}
if (!joinRequestReceived && sendJoinMessage) {
String fromUser = "join";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "join-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME);
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
JoinHeader joinHeader = (JoinHeader) ((HeaderFactoryExt) protocolObjects.headerFactory).createJoinHeader(callIdHeader.getCallId(), toHeader.getTag(), fromHeader.getTag());
sendSipRequest("MESSAGE", fromAddress, toAddress, joinHeader.toString(), null, false);
}
if (!isReplacesRequestReceived() && sendReplacesMessage) {
String fromUser = "replaces";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "replaces-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME);
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
ReplacesHeader replacesHeader = (ReplacesHeader) ((HeaderFactoryExt) protocolObjects.headerFactory).createReplacesHeader(callIdHeader.getCallId(), toHeader.getTag(), fromHeader.getTag());
sendSipRequest("MESSAGE", fromAddress, toAddress, replacesHeader.toString(), null, false);
}
if (joinRequestReceived) {
sendBye();
sendBye(joinDialog);
}
if (isReplacesRequestReceived()) {
sendBye();
sendBye(replacesDialog);
}
if (sendReinvite && !reinviteSent) {
List<Header> headers = new ArrayList<Header>();
Header reinviteHeader = protocolObjects.headerFactory.createHeader("ReInvite", "true");
headers.add(reinviteHeader);
sendInDialogSipRequest("INVITE", null, null, null, headers, null);
reinviteSent = true;
return;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class TestSipListener method processMessage.
private void processMessage(Request request, ServerTransaction serverTransactionId) {
if (request.toString().contains("408 received")) {
txTimeoutReceived = true;
}
ServerTransaction serverTransaction = null;
messageRequest = request;
try {
serverTransaction = (serverTransactionId == null ? sipProvider.getNewServerTransaction(request) : serverTransactionId);
} catch (javax.sip.TransactionAlreadyExistsException ex) {
ex.printStackTrace();
return;
} catch (javax.sip.TransactionUnavailableException ex1) {
ex1.printStackTrace();
return;
}
ContentTypeHeader contentTypeHeader = (ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
boolean sendInvitewithJoin = false;
boolean sendInvitewithReplaces = false;
if (contentTypeHeader != null) {
if (TEXT_CONTENT_TYPE.equals(contentTypeHeader.getContentType())) {
this.lastMessageContent = new String(request.getRawContent());
allMessagesContent.add(new String(lastMessageContent));
if (lastMessageContent.indexOf("Join : ") != -1) {
this.lastMessageContent = lastMessageContent.substring("Join : ".length());
sendInvitewithJoin = true;
}
if (lastMessageContent.indexOf("Replaces : ") != -1) {
this.lastMessageContent = lastMessageContent.substring("Replaces : ".length());
sendInvitewithReplaces = true;
}
}
} else {
if (request.getHeader("From").toString().contains("b2bua@sip-servlets"))
b2buamessagereceived = true;
}
try {
Response okResponse = protocolObjects.messageFactory.createResponse(Response.OK, request);
ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
if (toHeader.getTag() == null) {
toHeader.setTag(Integer.toString(new Random().nextInt(10000000)));
}
// okResponse.addHeader(contactHeader);
serverTransaction.sendResponse(okResponse);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending OK response to message", ex);
}
if (sendInvitewithJoin) {
try {
String fromUser = "receiver";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "join-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
String[] headerNames = new String[] { "Join" };
String[] headerContents = new String[] { lastMessageContent };
sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending INVITE with Join", ex);
}
}
if (sendInvitewithReplaces) {
try {
String fromUser = "receiver";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "replaces-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
String[] headerNames = new String[] { "Replaces" };
String[] headerContents = new String[] { lastMessageContent };
sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending INVITE with Join", ex);
}
}
}
use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class TestSipListener method sendBye.
/**
* @throws SipException
* @throws TransactionUnavailableException
* @throws TransactionDoesNotExistException
*/
public void sendBye(final Dialog dialog) throws SipException, TransactionUnavailableException, TransactionDoesNotExistException {
Thread th = new Thread() {
public void run() {
try {
if (sendByeInNewThread)
Thread.sleep(600);
Request byeRequest = dialog.createRequest(Request.BYE);
URI uri = ((FromHeader) byeRequest.getHeader(FromHeader.NAME)).getAddress().getURI();
if (uri.isSipURI()) {
((SipURI) uri).removeParameter("fromParam");
}
ClientTransaction ct = sipProvider.getNewClientTransaction(byeRequest);
logger.info("Sending BYE " + byeRequest);
if (!sendSubsequentRequestsThroughSipProvider) {
dialog.sendRequest(ct);
} else {
sipProvider.sendRequest(byeRequest);
}
byeSent = true;
} catch (Exception e) {
e.printStackTrace();
}
}
};
if (sendByeInNewThread) {
th.start();
} else {
th.run();
}
}
Aggregations