8 import java.io.Writer; |
8 import java.io.Writer; |
9 import java.util.concurrent.Callable; |
9 import java.util.concurrent.Callable; |
10 import java.util.concurrent.ExecutionException; |
10 import java.util.concurrent.ExecutionException; |
11 import java.util.concurrent.Future; |
11 import java.util.concurrent.Future; |
12 import java.util.concurrent.FutureTask; |
12 import java.util.concurrent.FutureTask; |
13 import java.util.concurrent.RunnableFuture; |
|
14 |
13 |
|
14 import org.chromium.sdk.ChromiumIOException; |
15 import org.chromium.sdk.LineReader; |
15 import org.chromium.sdk.LineReader; |
16 import org.chromium.sdk.internal.transport.Message.MalformedMessageException; |
16 import org.chromium.sdk.internal.transport.Message.MalformedMessageException; |
17 |
17 |
18 /** |
18 /** |
19 * Handshaker handles "handshake" part of communication. It may write and read whatever it needs |
19 * Handshaker handles "handshake" part of communication. It may write and read whatever it needs |
83 |
83 |
84 public Future<RemoteInfo> getRemoteInfo() { |
84 public Future<RemoteInfo> getRemoteInfo() { |
85 return runnableFuture; |
85 return runnableFuture; |
86 } |
86 } |
87 |
87 |
88 private final RunnableFuture<RemoteInfo> runnableFuture = |
88 private final FutureTask<RemoteInfo> runnableFuture = |
89 new FutureTask<RemoteInfo>(new HandshakeTaks()); |
89 new FutureTask<RemoteInfo>(new HandshakeTaks()); |
90 |
90 |
91 private LineReader input = null; |
91 private LineReader input = null; |
92 |
92 |
93 public void perform(LineReader input, Writer output) throws IOException { |
93 public void perform(LineReader input, Writer output) throws IOException { |
98 try { |
98 try { |
99 runnableFuture.get(); |
99 runnableFuture.get(); |
100 } catch (InterruptedException e) { |
100 } catch (InterruptedException e) { |
101 throw new RuntimeException(e); |
101 throw new RuntimeException(e); |
102 } catch (ExecutionException e) { |
102 } catch (ExecutionException e) { |
103 throw new IOException("Failed to perform handshake", e); |
103 throw new ChromiumIOException("Failed to perform handshake", e); |
104 } |
104 } |
105 |
105 |
106 } |
106 } |
107 |
107 |
108 class HandshakeTaks implements Callable<RemoteInfo> { |
108 class HandshakeTaks implements Callable<RemoteInfo> { |
109 public RemoteInfo call() throws IOException { |
109 public RemoteInfo call() throws IOException { |
110 final Message message; |
110 final Message message; |
111 try { |
111 try { |
112 message = Message.fromBufferedReader(input); |
112 message = Message.fromBufferedReader(input); |
113 } catch (MalformedMessageException e) { |
113 } catch (MalformedMessageException e) { |
114 throw new IOException("Unrecognized handshake message from remote", e); |
114 throw new ChromiumIOException("Unrecognized handshake message from remote", e); |
115 } |
115 } |
116 if (message == null) { |
116 if (message == null) { |
117 throw new IOException("End of stream"); |
117 throw new IOException("End of stream"); |
118 } |
118 } |
119 final String protocolVersion = message.getHeader("Protocol-Version", null); |
119 final String protocolVersion = message.getHeader("Protocol-Version", null); |