jtulach@6
|
1 |
/**
|
jtulach@6
|
2 |
* The MIT License (MIT)
|
jtulach@6
|
3 |
*
|
jtulach@6
|
4 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@6
|
5 |
*
|
jtulach@6
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
jtulach@6
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
jtulach@6
|
8 |
* in the Software without restriction, including without limitation the rights
|
jtulach@6
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
jtulach@6
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
jtulach@6
|
11 |
* furnished to do so, subject to the following conditions:
|
jtulach@6
|
12 |
*
|
jtulach@6
|
13 |
* The above copyright notice and this permission notice shall be included in
|
jtulach@6
|
14 |
* all copies or substantial portions of the Software.
|
jtulach@6
|
15 |
*
|
jtulach@6
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jtulach@6
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jtulach@6
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jtulach@6
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jtulach@6
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
jtulach@6
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
jtulach@6
|
22 |
* THE SOFTWARE.
|
jtulach@6
|
23 |
*/
|
jtulach@13
|
24 |
package org.apidesign.bck2brwsr.demo.chatserver.impl;
|
jtulach@6
|
25 |
|
jtulach@6
|
26 |
import java.util.ArrayList;
|
jtulach@11
|
27 |
import java.util.IdentityHashMap;
|
jtulach@6
|
28 |
import java.util.List;
|
jtulach@11
|
29 |
import java.util.Map;
|
jtulach@11
|
30 |
import java.util.logging.Logger;
|
jtulach@11
|
31 |
import javax.inject.Singleton;
|
jtulach@11
|
32 |
import javax.ws.rs.DefaultValue;
|
jtulach@6
|
33 |
import javax.ws.rs.GET;
|
jtulach@6
|
34 |
import javax.ws.rs.Path;
|
jtulach@6
|
35 |
import javax.ws.rs.Produces;
|
jtulach@11
|
36 |
import javax.ws.rs.QueryParam;
|
jtulach@10
|
37 |
import javax.ws.rs.container.AsyncResponse;
|
jtulach@10
|
38 |
import javax.ws.rs.container.Suspended;
|
jtulach@6
|
39 |
import javax.ws.rs.core.MediaType;
|
jtulach@6
|
40 |
import net.java.html.json.Context;
|
jtulach@13
|
41 |
import org.apidesign.html.chatserver.model.Message;
|
jtulach@13
|
42 |
import org.apidesign.html.chatserver.model.Query;
|
jtulach@6
|
43 |
|
jtulach@11
|
44 |
/** Server side of the chat application.*/
|
jtulach@13
|
45 |
@Path("/chat/") @Singleton
|
jtulach@6
|
46 |
public final class ChatServerResource {
|
jtulach@11
|
47 |
private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
|
jtulach@13
|
48 |
private static final long started = System.currentTimeMillis() - 10;
|
jtulach@6
|
49 |
|
jtulach@6
|
50 |
private List<Message> msgs = new ArrayList<>();
|
jtulach@6
|
51 |
{
|
jtulach@6
|
52 |
Message welcome = new Message(Context.findDefault(Message.class));
|
jtulach@6
|
53 |
welcome.setUser("system");
|
jtulach@6
|
54 |
welcome.setComment("Welcome and enjoy!");
|
jtulach@13
|
55 |
welcome.setSince(10);
|
jtulach@6
|
56 |
msgs.add(welcome);
|
jtulach@6
|
57 |
}
|
jtulach@6
|
58 |
|
jtulach@11
|
59 |
private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
|
jtulach@11
|
60 |
|
jtulach@6
|
61 |
@Produces(MediaType.APPLICATION_JSON)
|
jtulach@11
|
62 |
@GET public synchronized void getResources(
|
jtulach@11
|
63 |
@QueryParam("since") @DefaultValue("0") long since,
|
jtulach@11
|
64 |
@Suspended AsyncResponse ar
|
jtulach@11
|
65 |
) {
|
jtulach@11
|
66 |
Query q = new Query(Context.findDefault(ChatServerResource.class));
|
jtulach@11
|
67 |
for (Message m : msgs) {
|
jtulach@13
|
68 |
if (m.getSince()>= since) {
|
jtulach@11
|
69 |
q.getMessages().add(m);
|
jtulach@11
|
70 |
}
|
jtulach@11
|
71 |
}
|
jtulach@11
|
72 |
if (!q.getMessages().isEmpty()) {
|
jtulach@11
|
73 |
ar.resume(q);
|
jtulach@11
|
74 |
} else {
|
jtulach@11
|
75 |
awaiting.put(ar, since);
|
jtulach@11
|
76 |
}
|
jtulach@6
|
77 |
}
|
jtulach@6
|
78 |
|
jtulach@11
|
79 |
private void handleAwaiting(long newest) {
|
jtulach@11
|
80 |
assert Thread.holdsLock(this);
|
jtulach@11
|
81 |
AGAIN: for (;;) {
|
jtulach@11
|
82 |
for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
|
jtulach@11
|
83 |
AsyncResponse ar = entry.getKey();
|
jtulach@11
|
84 |
Long since = entry.getValue();
|
jtulach@11
|
85 |
if (since <= newest) {
|
jtulach@11
|
86 |
awaiting.remove(ar);
|
jtulach@11
|
87 |
getResources(since, ar);
|
jtulach@11
|
88 |
continue AGAIN;
|
jtulach@11
|
89 |
}
|
jtulach@11
|
90 |
}
|
jtulach@11
|
91 |
return;
|
jtulach@6
|
92 |
}
|
jtulach@6
|
93 |
}
|
jtulach@11
|
94 |
|
jtulach@11
|
95 |
@Path("addComment") @GET
|
jtulach@11
|
96 |
public synchronized Message publish(
|
jtulach@11
|
97 |
@QueryParam("user") String user,
|
jtulach@11
|
98 |
@QueryParam("comment") String comment
|
jtulach@11
|
99 |
) {
|
jtulach@11
|
100 |
Message msg = new Message(Context.findDefault(ChatServerResource.class));
|
jtulach@11
|
101 |
msg.setUser(user);
|
jtulach@11
|
102 |
msg.setComment(comment);
|
jtulach@13
|
103 |
msg.setSince(System.currentTimeMillis() - started);
|
jtulach@11
|
104 |
msgs.add(msg);
|
jtulach@13
|
105 |
handleAwaiting(msg.getSince());
|
jtulach@11
|
106 |
return msg;
|
jtulach@11
|
107 |
}
|
jtulach@6
|
108 |
}
|