Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package no.ntnu.item.ttm4133.lab2;
- // You should use javax.servlet namespace for most SIP-related classes
- import java.io.IOException;
- import javax.servlet.sip.Proxy;
- import javax.servlet.sip.SipFactory;
- import javax.servlet.sip.SipServlet;
- import javax.servlet.sip.SipServletRequest;
- import javax.servlet.sip.SipURI;
- import javax.servlet.sip.TooManyHopsException;
- @SuppressWarnings("serial")
- public class CallerPref extends SipServlet {
- int count;
- long time1;
- long time2;
- boolean available=true;
- @Override
- public void doInvite(SipServletRequest request) throws IOException {
- // get the "from" address
- String from = request.getFrom().getValue();
- String to = request.getTo().getValue();
- SipFactory sf = (SipFactory)getServletContext().getAttribute("javax.servlet.sip.SipFactory");
- try {
- // get the SIP proxy
- Proxy proxy = request.getProxy();
- // useful utility object that can be used to create SIP URIs
- // write code...
- SipURI charlieW = sf.createSipURI("99005001","open-ims.test");
- SipURI charlieH = sf.createSipURI("99005002","open-ims.test");
- if (to.contains("sip:99005000@open-ims.test")){
- count +=1;
- if (count == 1) {
- time1 = System.currentTimeMillis();
- System.out.print("time1 is now: " + time1);
- } else if (count== 2) {
- time2 = System.currentTimeMillis();
- System.out.print("time2 is now: " + time2);
- }
- if (from.contains("sip:44001000@open-ims.test")) {
- if (available) {
- request.setRequestURI(charlieW);
- request.setHeader("to","sip:99005001@open-ims.test");
- proxy.proxyTo(charlieW);
- if (count>0){
- count=0;}
- } else if (available==false && (count ==2) && (Math.abs(time1 - time2) <= 30000)){
- System.out.print("current time: " + (Math.abs(time1-time2)));
- request.setRequestURI(charlieH);
- request.setHeader("to","sip:99005002@open-ims.test");
- proxy.proxyTo(charlieH);
- if (count>0){
- count=0;}}
- }
- else if ((time2-time1) > 30000 && count ==2){
- time1 = System.currentTimeMillis();
- count=1;
- }
- else {
- request.setRequestURI(charlieH);
- request.setHeader("to","sip:99005002@open-ims.test");
- proxy.proxyTo(charlieH);
- }
- }
- System.out.println(request.getTo().getValue());
- } catch (TooManyHopsException e) {
- e.printStackTrace();
- }
- }
- public void doPublish(SipServletRequest request) throws IOException {
- // request.getRawContent() can be used to extract the content part of the SIP message
- String content = new String(request.getRawContent(), "UTF-8");
- System.out.print("#########################################################");
- System.out.print("publishing");
- if( content.contains("unavailable") || content.contains("Unavailable"))
- {
- System.out.print("***********************************************");
- available = false;
- } else { available = true; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement