| 86 |
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 |
package org.vt.ece4564.latmb;
|
|
|
17 |
|
|
|
18 |
import java.util.logging.Level;
|
|
|
19 |
import java.util.logging.Logger;
|
|
|
20 |
|
|
|
21 |
import org.jboss.netty.channel.Channel;
|
|
|
22 |
import org.jboss.netty.channel.ChannelEvent;
|
|
|
23 |
import org.jboss.netty.channel.ChannelFuture;
|
|
|
24 |
import org.jboss.netty.channel.ChannelFutureListener;
|
|
|
25 |
import org.jboss.netty.channel.ChannelHandlerContext;
|
|
|
26 |
import org.jboss.netty.channel.ChannelStateEvent;
|
|
|
27 |
import org.jboss.netty.channel.ExceptionEvent;
|
|
|
28 |
import org.jboss.netty.channel.MessageEvent;
|
|
|
29 |
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
|
|
30 |
|
|
|
31 |
import android.util.Log;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Handles a client-side channel.
|
|
|
35 |
*
|
|
|
36 |
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
|
|
37 |
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
|
|
38 |
*
|
|
|
39 |
* @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
|
|
|
40 |
*/
|
|
|
41 |
public class ClientHandler<T> extends SimpleChannelUpstreamHandler {
|
|
|
42 |
|
|
|
43 |
private static final String TAG = ClientHandler.class.getName();
|
|
|
44 |
|
|
|
45 |
private NetworkMessageHandler<T> listener_;
|
|
|
46 |
|
|
|
47 |
private Channel channel_;
|
|
|
48 |
|
|
|
49 |
private static final Logger logger = Logger
|
|
|
50 |
.getLogger(ClientHandler.class.getName());
|
|
|
51 |
|
|
|
52 |
public void disconnect(){
|
|
|
53 |
channel_.disconnect();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
@Override
|
|
|
57 |
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
|
|
|
58 |
throws Exception {
|
|
|
59 |
if (e instanceof ChannelStateEvent) {
|
|
|
60 |
logger.info(e.toString());
|
|
|
61 |
}
|
|
|
62 |
super.handleUpstream(ctx, e);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// @Override
|
|
|
66 |
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
|
|
|
67 |
throws Exception {
|
|
|
68 |
|
|
|
69 |
channel_ = e.getChannel();
|
|
|
70 |
|
|
|
71 |
channel_.getCloseFuture().addListener(new ChannelFutureListener() {
|
|
|
72 |
|
|
|
73 |
@Override
|
|
|
74 |
public void operationComplete(ChannelFuture future)
|
|
|
75 |
throws Exception {
|
|
|
76 |
listener_.channelClosed(channel_);
|
|
|
77 |
}
|
|
|
78 |
});
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Override
|
|
|
82 |
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
|
|
|
83 |
if (listener_ != null) {
|
|
|
84 |
T msg = (T) e.getMessage();
|
|
|
85 |
listener_.received(msg);
|
|
|
86 |
} else {
|
|
|
87 |
System.err.println(e.getMessage());
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public void send(LATMBProtocol.TrackingMessage msg) {
|
|
|
92 |
channel_.write(msg);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
@Override
|
|
|
96 |
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
|
|
|
97 |
logger.log(Level.WARNING, "Unexpected exception from downstream.",
|
|
|
98 |
e.getCause());
|
|
|
99 |
e.getChannel().close();
|
|
|
100 |
|
|
|
101 |
if(listener_ != null)
|
|
|
102 |
listener_.channelException(e.getCause());
|
|
|
103 |
else
|
|
|
104 |
Log.e(TAG, "Error communicating with server",e.getCause());
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public NetworkMessageHandler<T> getListener() {
|
|
|
108 |
return listener_;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public void setListener(NetworkMessageHandler<T> listener) {
|
|
|
112 |
listener_ = listener;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|