• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / phoebus / #84

21 Sep 2023 02:26AM UTC coverage: 16.568% (+0.009%) from 16.559%
#84

push

lorenzo-gomez-windhover
-Add packets viewer. WIP.

129 of 129 new or added lines in 3 files covered. (100.0%)

17798 of 107426 relevant lines covered (16.57%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/commander-core/src/main/java/com/windhoverlabs/yamcs/core/WebSocketClient.java
1
/*
2
 * Copyright 2014 The Netty Project
3
 *
4
 * The Netty Project licenses this file to you under the Apache License,
5
 * version 2.0 (the "License"); you may not use this file except in compliance
6
 * with the License. You may obtain a copy of the License at:
7
 *
8
 *   https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations
14
 * under the License.
15
 */
16
package com.windhoverlabs.yamcs.core;
17

18
import io.netty.bootstrap.Bootstrap;
19
import io.netty.buffer.Unpooled;
20
import io.netty.channel.Channel;
21
import io.netty.channel.ChannelInitializer;
22
import io.netty.channel.ChannelPipeline;
23
import io.netty.channel.EventLoopGroup;
24
import io.netty.channel.nio.NioEventLoopGroup;
25
import io.netty.channel.socket.SocketChannel;
26
import io.netty.channel.socket.nio.NioSocketChannel;
27
import io.netty.handler.codec.http.DefaultHttpHeaders;
28
import io.netty.handler.codec.http.HttpClientCodec;
29
import io.netty.handler.codec.http.HttpObjectAggregator;
30
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
31
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
32
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
33
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
34
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
35
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
36
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
37
import io.netty.handler.ssl.SslContext;
38
import java.io.BufferedReader;
39
import java.io.IOException;
40
import java.io.InputStreamReader;
41
import java.net.URI;
42
import java.net.URISyntaxException;
43

44
/**
45
 * This is an example of a WebSocket client.
46
 *
47
 * <p>In order to run this example you need a compatible WebSocket server. Therefore you can either
48
 * start the WebSocket server from the examples by running {@link
49
 * io.netty.example.http.websocketx.server.WebSocketServer} or connect to an existing WebSocket
50
 * server such as <a href="https://www.websocket.org/echo.html">ws://echo.websocket.org</a>.
51
 *
52
 * <p>The client will attempt to connect to the URI passed to it as the first argument. You don't
53
 * have to specify any arguments if you want to connect to the example WebSocket server, as this is
54
 * the default.
55
 */
56
public class WebSocketClient {
57

58
  static final String URL = System.getProperty("url", "ws://127.0.0.1:8090/websocket");
×
59

60
  public WebSocketClient() {
×
61
    URI uri = null;
×
62
    try {
63
      uri = new URI(URL);
×
64
    } catch (URISyntaxException e) {
×
65
      // TODO Auto-generated catch block
66
      e.printStackTrace();
×
67
    }
×
68
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
×
69
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
×
70
    final int port;
71
    if (uri.getPort() == -1) {
×
72
      if ("ws".equalsIgnoreCase(scheme)) {
×
73
        port = 80;
×
74
      } else if ("wss".equalsIgnoreCase(scheme)) {
×
75
        port = 443;
×
76
      } else {
77
        port = -1;
×
78
      }
79
    } else {
80
      port = uri.getPort();
×
81
    }
82

83
    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
×
84
      System.err.println("Only WS(S) is supported.");
×
85
      return;
×
86
    }
87

88
    SslContext sslCtx = null;
×
89

90
    EventLoopGroup group = new NioEventLoopGroup();
×
91
    try {
92
      // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
93
      // If you change it to V00, ping is not supported and remember to change
94
      // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
95
      final WebSocketClientHandler handler =
×
96
          new WebSocketClientHandler(
97
              WebSocketClientHandshakerFactory.newHandshaker(
×
98
                  uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));
99

100
      Bootstrap b = new Bootstrap();
×
101
      b.group(group)
×
102
          .channel(NioSocketChannel.class)
×
103
          .handler(
×
104
              new ChannelInitializer<SocketChannel>() {
×
105
                @Override
106
                protected void initChannel(SocketChannel ch) {
107
                  ChannelPipeline p = ch.pipeline();
×
108
                  if (sslCtx != null) {
×
109
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
×
110
                  }
111
                  p.addLast(
×
112
                      new HttpClientCodec(),
113
                      new HttpObjectAggregator(8192),
114
                      WebSocketClientCompressionHandler.INSTANCE,
115
                      handler);
116
                }
×
117
              });
118

119
      Channel ch = null;
×
120
      try {
121
        ch = b.connect(uri.getHost(), port).sync().channel();
×
122
      } catch (InterruptedException e) {
×
123
        // TODO Auto-generated catch block
124
        e.printStackTrace();
×
125
      }
×
126
      try {
127
        handler.handshakeFuture().sync();
×
128
      } catch (InterruptedException e) {
×
129
        // TODO Auto-generated catch block
130
        e.printStackTrace();
×
131
      }
×
132

133
      BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
×
134
      while (true) {
135
        String msg = null;
×
136
        try {
137
          msg = console.readLine();
×
138
        } catch (IOException e) {
×
139
          // TODO Auto-generated catch block
140
          e.printStackTrace();
×
141
        }
×
142
        if (msg == null) {
×
143
          break;
×
144
        } else if ("bye".equals(msg.toLowerCase())) {
×
145
          ch.writeAndFlush(new CloseWebSocketFrame());
×
146
          try {
147
            ch.closeFuture().sync();
×
148
          } catch (InterruptedException e) {
×
149
            // TODO Auto-generated catch block
150
            e.printStackTrace();
×
151
          }
×
152
          break;
×
153
        } else if ("ping".equals(msg.toLowerCase())) {
×
154
          WebSocketFrame frame =
×
155
              new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] {8, 1, 8, 1}));
×
156
          ch.writeAndFlush(frame);
×
157
        } else {
×
158
          WebSocketFrame frame = new TextWebSocketFrame(msg);
×
159
          ch.writeAndFlush(frame);
×
160
        }
161
      }
×
162
    } finally {
163
      group.shutdownGracefully();
×
164
    }
165
  }
×
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc