Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88ee6961cab27fa0646d4f8c65aa9e2a14190fba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.jsr356.server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint.Basic;
import javax.websocket.Session;

import org.eclipse.jetty.util.BufferUtil;

@ClientEndpoint
public class EchoClientSocket extends TrackingSocket
{
    public final CountDownLatch eventCountLatch;
    private Session session;
    private Basic remote;

    public EchoClientSocket(int expectedEventCount)
    {
        this.eventCountLatch = new CountDownLatch(expectedEventCount);
    }

    public void close() throws IOException
    {
        if (session != null)
        {
            this.session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE,"Test Complete"));
        }
    }

    @OnClose
    public void onClose(CloseReason close)
    {
        this.session = null;
        super.closeReason = close;
        super.closeLatch.countDown();
    }

    @OnError
    public void onError(Throwable t)
    {
        if (t == null)
        {
            addError(new NullPointerException("Throwable should not be null"));
        }
        else
        {
            addError(t);
        }
    }

    @OnOpen
    public void onOpen(Session session)
    {
        this.session = session;
        this.remote = session.getBasicRemote();
        openLatch.countDown();
    }

    @OnMessage
    public void onText(String text)
    {
        addEvent(text);
        eventCountLatch.countDown();
    }

    public boolean awaitAllEvents(long timeout, TimeUnit unit) throws InterruptedException
    {
        return eventCountLatch.await(timeout,unit);
    }

    public void sendObject(Object obj) throws IOException, EncodeException
    {
        remote.sendObject(obj);
    }

    public void sendPartialBinary(ByteBuffer part, boolean fin) throws IOException
    {
        remote.sendBinary(part,fin);
    }

    public void sendPartialText(String part, boolean fin) throws IOException
    {
        remote.sendText(part,fin);
    }
    
    public void sendPing(String message) throws IOException
    {
        remote.sendPing(BufferUtil.toBuffer(message));
    }
    
    public void sendPong(String message) throws IOException
    {
        remote.sendPong(BufferUtil.toBuffer(message));
    }
}

Back to the top