]> git.basschouten.com Git - openhab-addons.git/blob
39318e958b4c3277201d85a0ed6440895babca40
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.webthing.internal.client;
14
15 import java.io.IOException;
16 import java.net.URI;
17 import java.time.Duration;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.function.Consumer;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.websocket.client.WebSocketClient;
23
24 /**
25  * Factory to create new instances of a WebSocket connection
26  *
27  * @author Gregor Roth - Initial contribution
28  */
29 @NonNullByDefault
30 interface WebSocketConnectionFactory {
31
32     /**
33      * create (and opens) a new WebSocket connection
34      *
35      * @param webSocketURI the websocket uri
36      * @param executor the executor to use
37      * @param errorHandler the error handler
38      * @param pingPeriod the ping period to check the healthiness of the connection
39      * @return the newly opened WebSocket connection
40      * @throws IOException if the web socket connection can not be established
41      */
42     WebSocketConnection create(URI webSocketURI, ScheduledExecutorService executor, Consumer<String> errorHandler,
43             Duration pingPeriod) throws IOException;
44
45     /**
46      * @param webSocketClient the web socket client to use
47      * @return the default instance of the factory
48      */
49     static WebSocketConnectionFactory instance(WebSocketClient webSocketClient) {
50         return (webSocketURI, executor, errorHandler, pingPeriod) -> {
51             var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod);
52             webSocketClient.connect(webSocketConnection, webSocketURI);
53             return webSocketConnection;
54         };
55     }
56 }