OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
12 | 12 |
13 import android.util.Log; | 13 import android.util.Log; |
14 | 14 |
15 import org.appspot.apprtc.util.LooperExecutor; | 15 import org.appspot.apprtc.util.AppRTCUtils; |
16 | 16 |
17 import java.io.BufferedReader; | 17 import java.io.BufferedReader; |
18 import java.io.IOException; | 18 import java.io.IOException; |
19 import java.io.InputStreamReader; | 19 import java.io.InputStreamReader; |
20 import java.io.PrintWriter; | 20 import java.io.PrintWriter; |
21 import java.net.InetAddress; | 21 import java.net.InetAddress; |
22 import java.net.ServerSocket; | 22 import java.net.ServerSocket; |
23 import java.net.Socket; | 23 import java.net.Socket; |
24 import java.net.UnknownHostException; | 24 import java.net.UnknownHostException; |
25 import java.util.concurrent.ExecutorService; | |
25 | 26 |
26 /** | 27 /** |
27 * Replacement for WebSocketChannelClient for direct communication between two I P addresses. Handles | 28 * Replacement for WebSocketChannelClient for direct communication between two I P addresses. Handles |
28 * the signaling between the two clients using a TCP connection. | 29 * the signaling between the two clients using a TCP connection. |
29 * | 30 * |
30 * <p>All public methods should be called from a looper executor thread | 31 * <p>All public methods should be called from a looper executor thread |
31 * passed in a constructor, otherwise exception will be thrown. | 32 * passed in a constructor, otherwise exception will be thrown. |
32 * All events are dispatched on the same thread. | 33 * All events are dispatched on the same thread. |
33 */ | 34 */ |
34 public class TCPChannelClient { | 35 public class TCPChannelClient { |
35 private static final String TAG = "TCPChannelClient"; | 36 private static final String TAG = "TCPChannelClient"; |
36 | 37 |
37 private final LooperExecutor executor; | 38 private final ExecutorService executor; |
39 private AppRTCUtils.NonThreadSafe executorThreadCheck; | |
magjed_webrtc
2016/05/19 12:07:54
Can you use ThreadUtils.ThreadChecker in webrtc/ba
sakal
2016/05/19 12:39:03
Done.
| |
38 private final TCPChannelEvents eventListener; | 40 private final TCPChannelEvents eventListener; |
39 private TCPSocket socket; | 41 private TCPSocket socket; |
40 | 42 |
41 /** | 43 /** |
42 * Callback interface for messages delivered on TCP Connection. All callbacks are invoked from the | 44 * Callback interface for messages delivered on TCP Connection. All callbacks are invoked from the |
43 * looper executor thread. | 45 * looper executor thread. |
44 */ | 46 */ |
45 public interface TCPChannelEvents { | 47 public interface TCPChannelEvents { |
46 void onTCPConnected(boolean server); | 48 void onTCPConnected(boolean server); |
47 void onTCPMessage(String message); | 49 void onTCPMessage(String message); |
48 void onTCPError(String description); | 50 void onTCPError(String description); |
49 void onTCPClose(); | 51 void onTCPClose(); |
50 } | 52 } |
51 | 53 |
52 /** | 54 /** |
53 * Initializes the TCPChannelClient. If IP is a local IP address, starts a lis tening server on | 55 * Initializes the TCPChannelClient. If IP is a local IP address, starts a lis tening server on |
54 * that IP. If not, instead connects to the IP. | 56 * that IP. If not, instead connects to the IP. |
55 * | 57 * |
56 * @param eventListener Listener that will receive events from the client. | 58 * @param eventListener Listener that will receive events from the client. |
57 * @param ip IP address to listen on or connect to. | 59 * @param ip IP address to listen on or connect to. |
58 * @param port Port to listen on or connect to. | 60 * @param port Port to listen on or connect to. |
59 */ | 61 */ |
60 public TCPChannelClient( | 62 public TCPChannelClient( |
61 LooperExecutor executor, TCPChannelEvents eventListener, String ip, int po rt) { | 63 ExecutorService executor, TCPChannelEvents eventListener, String ip, int p ort) { |
62 this.executor = executor; | 64 this.executor = executor; |
63 this.eventListener = eventListener; | 65 this.eventListener = eventListener; |
64 | 66 |
65 InetAddress address; | 67 InetAddress address; |
66 try { | 68 try { |
67 address = InetAddress.getByName(ip); | 69 address = InetAddress.getByName(ip); |
68 } catch (UnknownHostException e) { | 70 } catch (UnknownHostException e) { |
69 reportError("Invalid IP address."); | 71 reportError("Invalid IP address."); |
70 return; | 72 return; |
71 } | 73 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 eventListener.onTCPError(message); | 112 eventListener.onTCPError(message); |
111 } | 113 } |
112 }); | 114 }); |
113 } | 115 } |
114 | 116 |
115 /** | 117 /** |
116 * Helper method for debugging purposes. | 118 * Helper method for debugging purposes. |
117 * Ensures that TCPChannelClient method is called on a looper thread. | 119 * Ensures that TCPChannelClient method is called on a looper thread. |
118 */ | 120 */ |
119 private void checkIfCalledOnValidThread() { | 121 private void checkIfCalledOnValidThread() { |
120 if (!executor.checkOnLooperThread()) { | 122 if(executorThreadCheck == null) { |
123 executorThreadCheck = new AppRTCUtils.NonThreadSafe(); | |
124 } | |
125 | |
126 if (!executorThreadCheck.calledOnValidThread()) { | |
121 throw new IllegalStateException( | 127 throw new IllegalStateException( |
122 "TCPChannelClient method is not called on valid thread"); | 128 "TCPChannelClient method is not called on valid thread"); |
123 } | 129 } |
124 } | 130 } |
125 | 131 |
126 | 132 |
127 /** | 133 /** |
128 * Base class for server and client sockets. Contains a listening thread that will call | 134 * Base class for server and client sockets. Contains a listening thread that will call |
129 * eventListener.onTCPMessage on new messages. | 135 * eventListener.onTCPMessage on new messages. |
130 */ | 136 */ |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 return null; | 359 return null; |
354 } | 360 } |
355 } | 361 } |
356 | 362 |
357 @Override | 363 @Override |
358 public boolean isServer() { | 364 public boolean isServer() { |
359 return false; | 365 return false; |
360 } | 366 } |
361 } | 367 } |
362 } | 368 } |
OLD | NEW |