Skip to content

Commit 424593f

Browse files
Changed to NewWebSocket implementation
Change-Id: I99713746584140e7f2bd9a524b94bd9270b3d011
1 parent ab72584 commit 424593f

9 files changed

Lines changed: 1191 additions & 681 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2012 Jacek Marchwicki <jacek.marchwicki@gmail.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License
15+
*/
16+
17+
package com.appunite.websocket;
18+
19+
import com.appunite.websocket.internal.SecureRandomProvider;
20+
import com.appunite.websocket.internal.SecureRandomProviderImpl;
21+
import com.appunite.websocket.internal.SocketProvider;
22+
import com.appunite.websocket.internal.SocketProviderImpl;
23+
24+
import java.io.IOException;
25+
import java.net.InetSocketAddress;
26+
import java.net.Socket;
27+
import java.net.URI;
28+
import java.net.UnknownHostException;
29+
30+
import javax.annotation.Nonnull;
31+
32+
import static com.appunite.websocket.tools.Preconditions.checkNotNull;
33+
34+
/**
35+
* Allows user to connect to websocket
36+
*
37+
* @author Jacek Marchwicki <jacek.marchwicki@gmail.com>
38+
*
39+
*/
40+
public class NewWebSocket {
41+
42+
// Not need to be locked
43+
@Nonnull
44+
private final SecureRandomProvider secureRandomProvider;
45+
@Nonnull
46+
private final SocketProvider socketProvider;
47+
48+
/**
49+
* Create instance of WebSocket
50+
*/
51+
public NewWebSocket() {
52+
this(new SecureRandomProviderImpl(), new SocketProviderImpl());
53+
}
54+
55+
/**
56+
* Create instance of WebSocket
57+
*
58+
* @param secureRandomProvider
59+
* provider for random values
60+
* @param socketProvider
61+
* provider for socket
62+
*/
63+
public NewWebSocket(@Nonnull SecureRandomProvider secureRandomProvider,
64+
@Nonnull SocketProvider socketProvider) {
65+
checkNotNull(secureRandomProvider, "secureRandomProvider cannot be null");
66+
checkNotNull(socketProvider, "socketProvider cannot be null");
67+
this.secureRandomProvider = secureRandomProvider;
68+
this.socketProvider = socketProvider;
69+
}
70+
71+
/**
72+
* This method will be alive until error occur or interrupt was executed.
73+
* This method always throws some exception. (not thread safe)
74+
*
75+
* @param uri
76+
* uri of websocket
77+
* @throws UnknownHostException
78+
* when could not connect to selected host
79+
* @throws IOException
80+
* thrown when I/O exception occur
81+
*/
82+
public WebSocketConnection create(@Nonnull URI uri,
83+
@Nonnull WebSocketListener listener) throws IOException {
84+
checkNotNull(uri, "Uri cannot be null");
85+
86+
final Socket socket = socketProvider.getSocket(uri);
87+
return new WebSocketConnection(socket, listener, uri, secureRandomProvider);
88+
}
89+
90+
91+
92+
93+
}

0 commit comments

Comments
 (0)