Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
package ioio.debugger;
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 NetworkClientHandler<T> extends SimpleChannelUpstreamHandler {
42
 
43
	private static final String TAG = NetworkClientHandler.class.getName();
44
 
45
	private NetworkMessageHandler<T> listener_;
46
 
47
	private Channel channel_;
48
 
49
	private static final Logger logger = Logger
50
			.getLogger(NetworkClientHandler.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
			@SuppressWarnings("unchecked")
85
			T msg = (T) e.getMessage();
86
			listener_.received(msg);
87
		} else {
88
			System.err.println(e.getMessage());
89
		}
90
	}
91
 
92
	public void send(NetworkIOIOProtocol.TrackingMessage msg) {
93
		channel_.write(msg);
94
	}
95
 
96
	@Override
97
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
98
		logger.log(Level.WARNING, "Unexpected exception from downstream.",
99
				e.getCause());
100
		e.getChannel().close();
101
 
102
		if(listener_ != null)
103
			listener_.channelException(e.getCause());
104
		else
105
			Log.e(TAG, "Error communicating with server",e.getCause());
106
	}
107
 
108
	public NetworkMessageHandler<T> getListener() {
109
		return listener_;
110
	}
111
 
112
	public void setListener(NetworkMessageHandler<T> listener) {
113
		listener_ = listener;
114
	}
115
 
116
}