]> granicus.if.org Git - ejabberd/commitdiff
* src/web/ejabberd_http_poll.erl: Completed
authorAlexey Shchepin <alexey@process-one.net>
Sun, 7 Mar 2004 21:15:12 +0000 (21:15 +0000)
committerAlexey Shchepin <alexey@process-one.net>
Sun, 7 Mar 2004 21:15:12 +0000 (21:15 +0000)
SVN Revision: 210

ChangeLog
src/web/Makefile.in
src/web/ejabberd_http_poll.erl

index c3cb53e7de0c9898cb7f051979127e38c5be9213..7f89328bd88250c04f7fe9b8540930de8c0db441 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2004-03-07  Alexey Shchepin  <alexey@sevcom.net>
+
+       * src/web/ejabberd_http_poll.erl: Completed
+
 2004-03-06  Alexey Shchepin  <alexey@sevcom.net>
 
        * src/web/: Support for HTTP Polling (JEP-0025) (almost complete)
index 806bbed1acc1037a8dfca44bcd7f13ebbb27478c..fd8ec5deb8226fab53ed97a78dcca329f2547d83 100644 (file)
@@ -17,7 +17,8 @@ OUTDIR = ..
 EFLAGS = -I .. -pz ..
 OBJS   = \
        $(OUTDIR)/ejabberd_http.beam \
-       $(OUTDIR)/ejabberd_web.beam
+       $(OUTDIR)/ejabberd_web.beam  \
+       $(OUTDIR)/ejabberd_http_poll.beam
 
 all:    $(OBJS)
 
index 78cd4078d156223ac3a8c6bf7182e25bc98e93e1..41b17fef5e7136fba035ea68bc61db2ca929fe48 100644 (file)
@@ -33,7 +33,8 @@
                key,
                output = "",
                input = "",
-               waiting_input = false}).
+               waiting_input = false,
+               timer}).
 
 %-define(DBGFSM, true).
 
@@ -43,6 +44,7 @@
 -define(FSMOPTS, []).
 -endif.
 
+-define(HTTP_POLL_TIMEOUT, 300000).
 
 %%%----------------------------------------------------------------------
 %%% API
@@ -63,7 +65,7 @@ recv({http_poll, FsmRef}, _Length, Timeout) ->
     gen_fsm:sync_send_all_state_event(FsmRef, recv, Timeout).
 
 close({http_poll, FsmRef}) ->
-    gen_fsm:sync_send_all_state_event(FsmRef, close).
+    catch gen_fsm:sync_send_all_state_event(FsmRef, close).
 
 
 process_request(#request{path = [],
@@ -120,7 +122,10 @@ init([ID, Key]) ->
     ?INFO_MSG("started: ~p", [{ID, Key}]),
     Opts = [], % TODO
     ejabberd_c2s:start({?MODULE, {http_poll, self()}}, Opts),
-    {ok, loop, #state{id = ID, key = Key}}.
+    Timer = erlang:start_timer(?HTTP_POLL_TIMEOUT, self(), []),
+    {ok, loop, #state{id = ID,
+                     key = Key,
+                     timer = Timer}}.
 
 %%----------------------------------------------------------------------
 %% Func: StateName/2
@@ -176,6 +181,10 @@ handle_sync_event(recv, From, StateName, StateData) ->
                                                      waiting_input = false}}
     end;
 
+handle_sync_event(stop, From, StateName, StateData) ->
+    Reply = ok,
+    {stop, normal, Reply, StateData};
+
 handle_sync_event({http_put, Key, NewKey, Packet},
                  From, StateName, StateData) ->
     Allow = case StateData#state.key of
@@ -201,10 +210,13 @@ handle_sync_event({http_put, Key, NewKey, Packet},
                                                              key = NewKey}};
                Receiver ->
                    gen_fsm:reply(Receiver, {ok, list_to_binary(Packet)}),
+                   cancel_timer(StateData#state.timer),
+                   Timer = erlang:start_timer(?HTTP_POLL_TIMEOUT, self(), []),
                    Reply = ok,
                    {reply, Reply, StateName,
                     StateData#state{waiting_input = false,
-                                    key = NewKey}}
+                                    key = NewKey,
+                                    timer = Timer}}
            end;
        true ->
            Reply = {error, bad_key},
@@ -228,6 +240,10 @@ code_change(OldVsn, StateName, StateData, Extra) ->
 %%          {next_state, NextStateName, NextStateData, Timeout} |
 %%          {stop, Reason, NewStateData}                         
 %%----------------------------------------------------------------------
+handle_info({timeout, Timer, _}, StateName,
+           #state{timer = Timer} = StateData) ->
+    {stop, normal, StateData};
+
 handle_info(_, StateName, StateData) ->
     {next_state, StateName, StateData}.
 
@@ -288,3 +304,12 @@ parse_request(Data) ->
     {ok, ID, Key, NewKey, Packet}.
 
 
+cancel_timer(Timer) ->
+    erlang:cancel_timer(Timer),
+    receive
+       {timeout, Timer, _} ->
+           ok
+    after 0 ->
+           ok
+    end.
+