PageRenderTime 152ms CodeModel.GetById 53ms app.highlight 4ms RepoModel.GetById 94ms app.codeStats 0ms

/ucengine/src/controllers/async/uce_async_lp.erl

http://github.com/AF83/ucengine
Erlang | 50 lines | 30 code | 3 blank | 17 comment | 0 complexity | 1fcf73a371a3ea8da66ba57a59a9924d MD5 | raw file
 1%%
 2%%  U.C.Engine - Unified Collaboration Engine
 3%%  Copyright (C) 2011 af83
 4%%
 5%%  This program is free software: you can redistribute it and/or modify
 6%%  it under the terms of the GNU Affero General Public License as published by
 7%%  the Free Software Foundation, either version 3 of the License, or
 8%%  (at your option) any later version.
 9%%
10%%  This program is distributed in the hope that it will be useful,
11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13%%  GNU Affero General Public License for more details.
14%%
15%%  You should have received a copy of the GNU Affero General Public License
16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17%%
18-module(uce_async_lp).
19
20-export([wait/8]).
21
22-include("uce.hrl").
23
24wait(Domain, Uid, Location, Search, From, Types, Parent, []) ->
25    Self = self(),
26    spawn(fun() ->
27                  uce_meeting:subscribe(self(), Domain, Uid, Location, From, Types, Parent),
28                  {ok, Event} = uce_async:listen(Domain,
29                                                 Location,
30                                                 Search,
31                                                 From,
32                                                 Types,
33                                                 Parent,
34                                                 (config:get(connection_timeout) * 1000)),
35                  Event2 = case Event of
36                               [] ->
37                                   [];
38                               Event ->
39                                   [Event]
40                           end,
41                  JSONEvents = mochijson:encode({struct,
42                                                 [{result,
43                                                   json_helpers:to_json(Domain, Event2)}]}),
44                  yaws_api:stream_chunk_deliver(Self, list_to_binary(JSONEvents)),
45                  yaws_api:stream_chunk_end(Self),
46                  uce_meeting:unsubscribe(self())
47          end),
48    {streamcontent_with_timeout, "application/json", <<>>, infinity};
49wait(Domain, _Uid, _Location, _Search, _From, _Types, _Parent, PreviousEvents) ->
50    json_helpers:json(Domain, PreviousEvents).