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