Advertisement
Guest User

yoloyolo

a guest
Mar 25th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package no.ntnu.item.ttm4133.lab2;
  2.  
  3. // You should use javax.servlet namespace for most SIP-related classes
  4. import java.io.IOException;
  5.  
  6. import javax.servlet.sip.Proxy;
  7. import javax.servlet.sip.SipFactory;
  8. import javax.servlet.sip.SipServlet;
  9. import javax.servlet.sip.SipServletRequest;
  10. import javax.servlet.sip.SipURI;
  11. import javax.servlet.sip.TooManyHopsException;
  12.  
  13. @SuppressWarnings("serial")
  14. public class CallerPref extends SipServlet {
  15.  
  16. int count;
  17. long time1;
  18. long time2;
  19. boolean available=true;
  20.  
  21. @Override
  22. public void doInvite(SipServletRequest request) throws IOException {
  23. // get the "from" address
  24. String from = request.getFrom().getValue();
  25. String to = request.getTo().getValue();
  26.  
  27.  
  28. SipFactory sf = (SipFactory)getServletContext().getAttribute("javax.servlet.sip.SipFactory");
  29.  
  30.  
  31. try {
  32. // get the SIP proxy
  33. Proxy proxy = request.getProxy();
  34.  
  35. // useful utility object that can be used to create SIP URIs
  36.  
  37.  
  38. // write code...
  39. SipURI charlieW = sf.createSipURI("99005001","open-ims.test");
  40. SipURI charlieH = sf.createSipURI("99005002","open-ims.test");
  41.  
  42. if (to.contains("sip:99005000@open-ims.test")){
  43. count +=1;
  44. if (count == 1) {
  45. time1 = System.currentTimeMillis();
  46. System.out.print("time1 is now: " + time1);
  47. } else if (count== 2) {
  48. time2 = System.currentTimeMillis();
  49. System.out.print("time2 is now: " + time2);
  50. }
  51.  
  52. if (from.contains("sip:44001000@open-ims.test")) {
  53. if (available) {
  54. request.setRequestURI(charlieW);
  55. request.setHeader("to","sip:99005001@open-ims.test");
  56. proxy.proxyTo(charlieW);
  57. if (count>0){
  58. count=0;}
  59. } else if (available==false && (count ==2) && (Math.abs(time1 - time2) <= 30000)){
  60. System.out.print("current time: " + (Math.abs(time1-time2)));
  61. request.setRequestURI(charlieH);
  62. request.setHeader("to","sip:99005002@open-ims.test");
  63. proxy.proxyTo(charlieH);
  64.  
  65. if (count>0){
  66. count=0;}}
  67. }
  68. else if ((time2-time1) > 30000 && count ==2){
  69. time1 = System.currentTimeMillis();
  70. count=1;
  71. }
  72. else {
  73. request.setRequestURI(charlieH);
  74. request.setHeader("to","sip:99005002@open-ims.test");
  75. proxy.proxyTo(charlieH);
  76. }
  77. }
  78.  
  79.  
  80.  
  81. System.out.println(request.getTo().getValue());
  82.  
  83.  
  84. } catch (TooManyHopsException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public void doPublish(SipServletRequest request) throws IOException {
  90.  
  91. // request.getRawContent() can be used to extract the content part of the SIP message
  92. String content = new String(request.getRawContent(), "UTF-8");
  93. System.out.print("#########################################################");
  94. System.out.print("publishing");
  95. if( content.contains("unavailable") || content.contains("Unavailable"))
  96. {
  97. System.out.print("***********************************************");
  98. available = false;
  99.  
  100. } else { available = true; }
  101.  
  102. }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement