Revision: 16405
Author: [email protected]
Date: Thu Jan 13 03:44:12 2011
Log: Added code comments
http://code.google.com/p/mobicents/source/detail?r=16405

Modified:
/branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleSipServlet.java /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleWebServlet.java /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/StatusServlet.java /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/sipapp/index.html

=======================================
--- /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleSipServlet.java Wed Jan 12 09:29:51 2011 +++ /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleSipServlet.java Thu Jan 13 03:44:12 2011
@@ -241,6 +241,10 @@
                resp.send();
        }

+       /*
+ * Any SIP Servlets method that wish to trigger an update to the client's page will use this method. + * For example doRegister use it to update the page every time a new user registers.
+        */
        protected void notifyStatus(String event){
BlockingQueue<String> eventsQueue = (LinkedBlockingQueue<String>)getServletContext().getAttribute("eventsQueue"); // if (eventsQueue == null) eventsQueue = new LinkedBlockingQueue<String>();
=======================================
--- /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleWebServlet.java Wed Jan 12 09:29:51 2011 +++ /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/SimpleWebServlet.java Thu Jan 13 03:44:12 2011
@@ -129,12 +129,14 @@
         response.setContentType("text/html");
         out = response.getWriter();

-        // Just redirect to the index
-// out.println("<HTML><META HTTP-EQUIV=\"Refresh\"CONTENT=\"0; URL=index.html\"><HEAD><TITLE></HTML>");
         out.println("success");
         out.close();
     }

+       /*
+ * Use the notifyStatus(String event) method in order to trigger an update at the client's page when needed for any new request.
+        * Here will be used for Bye or new Call requests.
+        */
        protected void notifyStatus(String event){
BlockingQueue<String> eventsQueue = (LinkedBlockingQueue<String>)getServletContext().getAttribute("eventsQueue"); // if (eventsQueue == null) eventsQueue = new LinkedBlockingQueue<String>();
=======================================
--- /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/StatusServlet.java Wed Jan 12 09:33:39 2011 +++ /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/java/org/mobicents/servlet/sip/example/StatusServlet.java Thu Jan 13 03:44:12 2011
@@ -39,6 +39,12 @@

        private Thread notifierThread = null;

+       /*
+ * Start a new thread that using the LinkedBlockingQueue, every time there is a new element in the collection + * will retrieve the last updated users HashMap and calls container and will use them to produce and dispatch a new repsonse
+        * to the clients in the queue.
+ * Important here is the fact that we never close the stream and we just flush the response so it can be used later when needed.
+        */
        @Override
        public void init(final ServletConfig config) throws ServletException {
                eventsQueue = new LinkedBlockingQueue<String>();
@@ -55,11 +61,13 @@
                                                event = eventsQueue.take();
                                                for (AsyncContext ac : queue) {
                                                        try {
+                                                               //Retrieve the 
Updated users and calls here
users = (HashMap<String, String>) config.getServletContext().getAttribute("registeredUsersMap");
                                                                if(users == null) 
users = new HashMap<String, String>();
calls = (CallStatusContainer) config.getServletContext().getAttribute("activeCalls");
                                                                if(calls == 
null) calls = new CallStatusContainer();
-
+
+                                                               //Create and 
dispatch the response. Keep the stream open though.
                                                                PrintWriter 
acWriter = ac.getResponse().getWriter();
                                                                
acWriter.println(printTable(users, calls));
                                                                
acWriter.flush();
@@ -78,7 +86,11 @@
                notifierThread = new Thread(notifierRunnable);
                notifierThread.start();
        }
-
+       /*
+ * doGet will will start an AsyncContext, store the new AsyncContext in order for this to get processed later and last
+        * update the client's page with the current data.
+ * Also sets a timeout in the AsyncContext, and registers an AsyncListener in order to handle the events needed.
+        */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                PrintWriter writer = response.getWriter();
@@ -109,7 +121,9 @@
                        public void onStartAsync(AsyncEvent event) throws 
IOException {
                        }
                });
+ //Add this to the queue for further processing when there is a response to dispatch
                queue.add(ac);
+               //First time request will get the current data
                firstTimeReq(ac);
        }

@@ -119,6 +133,9 @@
                notifierThread.interrupt();
        }

+       /*
+        * Create the response here
+        */
protected String printTable(HashMap<String, String> users, CallStatusContainer calls) {
                StringBuffer tableData = new StringBuffer();
                StringBuffer result = new StringBuffer();
@@ -160,6 +177,7 @@
tableData.append("<br/><button type=\"button\" onclick=\"byeAll()\">Close all calls</button>");
                }

+               //Wrap the response in a JSONP
                result.append(BEGIN_SCRIPT_TAG);
result.append("parseJsonp({ msg: \"" + tableData.toString().replace("\"", "\\\"") + "\"});\n");
                result.append(END_SCRIPT_TAG);
=======================================
--- /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/sipapp/index.html Thu Jan 13 01:37:04 2011 +++ /branches/servers/sip-servlets/2.0/sip-servlets-examples/click-to-call-servlet3.0/src/main/sipapp/index.html Thu Jan 13 03:44:12 2011
@@ -12,6 +12,7 @@
                 });
        });

+       //First time issue a AJAX GET request to the --status-- servlet
        function update() {
//Make the AJAX call with jQuery to avoid compatibility issues with browsers
                var xhr = $.ajax({url: '/Click2CallAsync/status',
@@ -21,7 +22,7 @@
                }});

//Check the state of the XmlHttpRequest object. Since the PrintWriter just flushes and doesn't close
-               //the stream, the XmlHttpRequest readyState = 3.
+               //the stream, the XmlHttpRequest readyState = 3.
                xhr.onreadystatechange = function() {
                  if (xhr.readyState == 3 && xhr.status == 200) {
                          $("#text").html(xhr.responseText);
@@ -33,10 +34,11 @@
                update();
        });

-
+//Parse the JSONP response from the --status-- servlet
 function parseJsonp(resp){
        $("#text").html(resp.msg);
 };
+//Use the functions below to issue AJAX GET requests for a new call or a bye requests to the --call-- servlet (ignore the response though)
 function call(firstToCall, secondToCall){
        $.get("/Click2CallAsync/call", { to: firstToCall, from: secondToCall });
 };

Reply via email to