| 90 |
Kevin |
1 |
/**************************************************************************
|
|
|
2 |
* Copyright 2011 Jules White
|
|
|
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 ioio.debugger;
|
|
|
18 |
|
|
|
19 |
import java.net.InetSocketAddress;
|
|
|
20 |
import java.util.concurrent.Executors;
|
|
|
21 |
|
|
|
22 |
import org.jboss.netty.bootstrap.ClientBootstrap;
|
|
|
23 |
import org.jboss.netty.channel.Channel;
|
|
|
24 |
import org.jboss.netty.channel.ChannelFuture;
|
|
|
25 |
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
public class NetworkClient {
|
|
|
29 |
|
|
|
30 |
@SuppressWarnings("rawtypes")
|
|
|
31 |
public static NetworkClientHandler connect(String host, int port) throws Exception {
|
|
|
32 |
|
|
|
33 |
// Configure the client.
|
|
|
34 |
ClientBootstrap bootstrap = new ClientBootstrap(
|
|
|
35 |
new NioClientSocketChannelFactory(
|
|
|
36 |
Executors.newCachedThreadPool(),
|
|
|
37 |
Executors.newCachedThreadPool()));
|
|
|
38 |
|
|
|
39 |
// Configure the pipeline factory.
|
|
|
40 |
bootstrap.setPipelineFactory(new NetworkClientPipelineFactory());
|
|
|
41 |
|
|
|
42 |
// Start the connection attempt.
|
|
|
43 |
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
|
|
44 |
|
|
|
45 |
// Wait until the connection attempt succeeds or fails.
|
|
|
46 |
Channel channel = future.awaitUninterruptibly().getChannel();
|
|
|
47 |
if (!future.isSuccess()) {
|
|
|
48 |
future.getCause().printStackTrace();
|
|
|
49 |
bootstrap.releaseExternalResources();
|
|
|
50 |
return null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return channel.getPipeline().get(NetworkClientHandler.class);
|
|
|
54 |
}
|
|
|
55 |
}
|