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
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@238
    26
import java.util.ArrayList;
jaroslav@238
    27
import java.util.List;
jaroslav@238
    28
import java.util.logging.Logger;
jaroslav@238
    29
import org.apidesign.demo.chat.shared.Message;
jaroslav@240
    30
import org.glassfish.grizzly.websockets.WebSocket;
jaroslav@240
    31
import org.glassfish.grizzly.websockets.WebSocketApplication;
jaroslav@238
    32
jaroslav@238
    33
/** Server side of the chat application.*/
jaroslav@240
    34
public final class ChatServerResource extends WebSocketApplication {
jaroslav@238
    35
    private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
jaroslav@238
    36
    private static final long started = System.currentTimeMillis() - 10;
jaroslav@238
    37
    
jaroslav@238
    38
    private List<Message> msgs = new ArrayList<>();
jaroslav@238
    39
    {
jaroslav@238
    40
        Message welcome = new Message();
jaroslav@238
    41
        welcome.setUser("system");
jaroslav@238
    42
        welcome.setComment("Welcome and enjoy!");
jaroslav@238
    43
        welcome.setSince(10);
jaroslav@238
    44
        msgs.add(welcome);
jaroslav@238
    45
    }
jaroslav@240
    46
jaroslav@240
    47
jaroslav@240
    48
/*
jaroslav@238
    49
    private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
jaroslav@238
    50
    
jaroslav@238
    51
    @Produces(MediaType.APPLICATION_JSON)
jaroslav@238
    52
    @GET public synchronized void getResources(
jaroslav@238
    53
        @QueryParam("since") @DefaultValue("0") long since,
jaroslav@238
    54
        @Suspended AsyncResponse ar
jaroslav@238
    55
    ) {
jaroslav@238
    56
        Query q = new Query();
jaroslav@238
    57
        for (Message m : msgs) {
jaroslav@238
    58
            if (m.getSince()>= since) {
jaroslav@238
    59
                q.getMessages().add(m);
jaroslav@238
    60
            }
jaroslav@238
    61
        }
jaroslav@238
    62
        if (!q.getMessages().isEmpty()) {
jaroslav@238
    63
            ar.resume(q);
jaroslav@238
    64
        } else {
jaroslav@238
    65
            awaiting.put(ar, since);
jaroslav@238
    66
        }
jaroslav@238
    67
    }
jaroslav@238
    68
    
jaroslav@238
    69
    private void handleAwaiting(long newest) {
jaroslav@238
    70
        assert Thread.holdsLock(this);
jaroslav@238
    71
        AGAIN: for (;;) {
jaroslav@238
    72
            for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
jaroslav@238
    73
                AsyncResponse ar = entry.getKey();
jaroslav@238
    74
                Long since = entry.getValue();
jaroslav@238
    75
                if (since <= newest) {
jaroslav@238
    76
                    awaiting.remove(ar);
jaroslav@238
    77
                    getResources(since, ar);
jaroslav@238
    78
                    continue AGAIN;
jaroslav@238
    79
                }
jaroslav@238
    80
            }
jaroslav@238
    81
            return;
jaroslav@238
    82
        }
jaroslav@238
    83
    }
jaroslav@238
    84
    
jaroslav@238
    85
    @POST @Consumes(value = MediaType.APPLICATION_JSON)
jaroslav@238
    86
    public synchronized Message publish(Message msg) {
jaroslav@238
    87
        msg.setSince(System.currentTimeMillis() - started);
jaroslav@238
    88
        msgs.add(msg);
jaroslav@238
    89
        handleAwaiting(msg.getSince());
jaroslav@238
    90
        return msg;
jaroslav@238
    91
    }
jaroslav@240
    92
*/
jaroslav@240
    93
jaroslav@240
    94
    @Override
jaroslav@240
    95
    public void onMessage(WebSocket socket, String text) {
jaroslav@240
    96
        super.onMessage(socket, text);
jaroslav@240
    97
    }
jaroslav@238
    98
}