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