blob: dc7dda19403833e9970df675a9e1715176b1aaf8 [file] [log] [blame]
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.remoting.transport;
import java.io.IOException;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.ChannelHandler;
import com.alibaba.dubbo.remoting.Endpoint;
import com.alibaba.dubbo.remoting.RemotingException;
/**
* AbstractPeer
*
* @author qian.lei
* @author william.liangf
*/
public abstract class AbstractPeer implements Endpoint, ChannelHandler {
private static final Logger logger = LoggerFactory.getLogger(AbstractPeer.class);
private final ChannelHandler handler;
private volatile URL url;
private volatile boolean closed;
public AbstractPeer(URL url, ChannelHandler handler) {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
if (handler == null) {
throw new IllegalArgumentException("handler == null");
}
this.url = url;
this.handler = handler;
}
public void send(Object message) throws RemotingException {
send(message, url.getParameter(Constants.SENT_KEY, false));
}
public void close() {
closed = true;
}
public void close(int timeout) {
close();
}
public URL getUrl() {
return url;
}
protected void setUrl(URL url) {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
this.url = url;
}
public ChannelHandler getChannelHandler() {
return handler;
}
public boolean isClosed() {
return closed;
}
public void connected(Channel ch) throws RemotingException {
if (closed) {
return;
}
handler.connected(ch);
}
public void disconnected(Channel ch) throws RemotingException {
handler.disconnected(ch);
}
public void sent(Channel ch, Object msg) throws RemotingException {
if (closed) {
return;
}
handler.sent(ch, msg);
}
public void received(Channel ch, Object msg) throws RemotingException {
if (closed) {
return;
}
handler.received(ch, msg);
}
public void caught(Channel ch, Throwable ex) throws RemotingException {
if (ex instanceof IOException || ex instanceof RemotingException) {
logger.warn("IOException on channel " + ch, ex);
} else {
logger.error("Exception on channel " + ch, ex);
}
handler.caught(ch, ex);
}
public ChannelHandler getHandler() {
return handler;
}
}