|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 package org.chromium.sdk.internal; |
|
6 |
|
7 import java.io.IOException; |
|
8 import java.net.SocketAddress; |
|
9 |
|
10 import org.chromium.sdk.ConnectionLogger; |
|
11 import org.chromium.sdk.internal.transport.Handshaker; |
|
12 import org.chromium.sdk.internal.transport.SocketConnection; |
|
13 import org.chromium.sdk.internal.transport.Connection.NetListener; |
|
14 |
|
15 /** |
|
16 * Factory for socket connections. Extremely simple and straight-forward |
|
17 * implementation. Note that it works only with stateless {@link Handshaker}s |
|
18 * because they are reused for every connection. |
|
19 */ |
|
20 public class SocketConnectionFactory implements ConnectionFactory { |
|
21 private final SocketAddress endpoint; |
|
22 private final int connectionTimeoutMs; |
|
23 private final ConnectionLogger.Factory connectionLoggerFactory; |
|
24 private final Handshaker handshaker; |
|
25 |
|
26 public SocketConnectionFactory(SocketAddress endpoint, int connectionTimeoutMs, |
|
27 ConnectionLogger.Factory connectionLoggerFactory, Handshaker handshaker) { |
|
28 this.endpoint = endpoint; |
|
29 this.connectionTimeoutMs = connectionTimeoutMs; |
|
30 this.connectionLoggerFactory = connectionLoggerFactory; |
|
31 this.handshaker = handshaker; |
|
32 } |
|
33 |
|
34 public SocketConnection newOpenConnection(NetListener netListener) throws IOException { |
|
35 ConnectionLogger connectionLogger; |
|
36 if (connectionLoggerFactory == null) { |
|
37 connectionLogger = null; |
|
38 } else { |
|
39 connectionLogger = connectionLoggerFactory.newConnectionLogger(); |
|
40 } |
|
41 SocketConnection connection = new SocketConnection(endpoint, connectionTimeoutMs, |
|
42 connectionLogger, handshaker); |
|
43 connection.setNetListener(netListener); |
|
44 connection.start(); |
|
45 return connection; |
|
46 } |
|
47 } |