本文整理汇总了Java中io.netty.channel.Channel.remoteAddress方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.remoteAddress方法的具体用法?Java Channel.remoteAddress怎么用?Java Channel.remoteAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.Channel
的用法示例。
在下文中一共展示了Channel.remoteAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseChannelRemoteAddr
import io.netty.channel.Channel; //导入方法依赖的package包/类
public static String parseChannelRemoteAddr(final Channel channel) {
if (null == channel) {
return "";
}
final SocketAddress remote = channel.remoteAddress();
final String addr = remote != null ? remote.toString() : "";
if (addr.length() > 0) {
int index = addr.lastIndexOf("/");
if (index >= 0) {
return addr.substring(index + 1);
}
return addr;
}
return "";
}
示例2: getKey
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* Get key.
*
* @param channel the channel
* @return the string
*/
public static String getKey(Channel channel){
InetSocketAddress local = (InetSocketAddress)channel.localAddress();
InetSocketAddress address = (InetSocketAddress)channel.remoteAddress();
StringBuilder sb = new StringBuilder();
sb.append(NetUtils.toIpString(address));
sb.append(":");
sb.append(address.getPort());
sb.append(" --> ");
sb.append(NetUtils.toIpString(local));
sb.append(":");
sb.append(local.getPort());
String key = sb.toString();
return key;
}
示例3: parseChannelRemoteAddr
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 获取Channel的远程IP地址
* @param channel
* @return
*/
public static String parseChannelRemoteAddr(final Channel channel) {
if (null == channel) {
return "";
}
SocketAddress remote = channel.remoteAddress();
final String addr = remote != null ? remote.toString() : "";
if (addr.length() > 0) {
int index = addr.lastIndexOf("/");
if (index >= 0) {
return addr.substring(index + 1);
}
return addr;
}
return "";
}
示例4: parseChannelRemoteAddr
import io.netty.channel.Channel; //导入方法依赖的package包/类
public static String parseChannelRemoteAddr(final Channel channel) {
if (null == channel) {
return "";
}
SocketAddress remote = channel.remoteAddress();
final String addr = remote != null ? remote.toString() : "";
if (addr.length() > 0) {
int index = addr.lastIndexOf("/");
if (index >= 0) {
return addr.substring(index + 1);
}
return addr;
}
return "";
}
示例5: parseRemoteAddr
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 获取Channel的远程IP地址
* @param channel
* @return
*/
public static String parseRemoteAddr(final Channel channel) {
if (null == channel) {
return "";
}
SocketAddress remote = channel.remoteAddress();
final String addr = remote != null ? remote.toString() : "";
if (addr.length() > 0) {
int index = addr.lastIndexOf("/");
if (index >= 0) {
return addr.substring(index + 1);
}
return addr;
}
return "";
}
示例6: getRemoteAddress
import io.netty.channel.Channel; //导入方法依赖的package包/类
/** Returns the remote address on the channel or "<unknown remote>" if none exists. */
public static String getRemoteAddress(Channel channel) {
if (channel != null && channel.remoteAddress() != null) {
return channel.remoteAddress().toString();
}
return "<unknown remote>";
}
示例7: parseChannelRemoteName
import io.netty.channel.Channel; //导入方法依赖的package包/类
public static String parseChannelRemoteName(final Channel channel) {
if (null == channel) {
return "";
}
final InetSocketAddress remote = (InetSocketAddress) channel.remoteAddress();
if (remote != null) {
return remote.getAddress().getHostName();
}
return "";
}
示例8: invokeSyncImpl
import io.netty.channel.Channel; //导入方法依赖的package包/类
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
final int opaque = request.getOpaque();
try {
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
this.responseTable.put(opaque, responseFuture);
final SocketAddress addr = channel.remoteAddress();
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
responseTable.remove(opaque);
responseFuture.setCause(f.cause());
responseFuture.putResponse(null);
PLOG.warn("send a request command to channel <" + addr + "> failed.");
}
});
RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
if (null == responseCommand) {
if (responseFuture.isSendRequestOK()) {
throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis,
responseFuture.getCause());
} else {
throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
}
}
return responseCommand;
} finally {
this.responseTable.remove(opaque);
}
}
示例9: getRemoteIp
import io.netty.channel.Channel; //导入方法依赖的package包/类
public static String getRemoteIp(Channel channel) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.remoteAddress();
if (inetSocketAddress == null) {
return "";
}
final InetAddress inetAddr = inetSocketAddress.getAddress();
return inetAddr != null ? inetAddr.getHostAddress() : inetSocketAddress.getHostName();
}
示例10: setChannel
import io.netty.channel.Channel; //导入方法依赖的package包/类
public void setChannel(Channel channel){
this.channel = channel;
super.remoteAddress = channel.remoteAddress();
super.localAddress = channel.localAddress();
}
示例11: invokeSyncImpl
import io.netty.channel.Channel; //导入方法依赖的package包/类
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
throws InterruptedException,RemotingTimeoutException,RemotingSendRequestException{
final int unique = request.getUnique();
try{
final SocketAddress addr = channel.remoteAddress();
final ResponseFuture responseFuture = new ResponseFuture(unique, timeoutMillis);
this.responseTable.put(unique, responseFuture);
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess()){
responseFuture.setSendRequestOK(true);
return;
}
responseFuture.setSendRequestOK(false);
responseTable.remove(unique);
responseFuture.setCause(future.cause());
responseFuture.putResponse(null);
log.warn("send a request command to channel <" + addr + "> failed.");
}
});
RemotingCommand respose = responseFuture.waitRespose(timeoutMillis);
if(null == respose){
if(responseFuture.isSendRequestOK()){
throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause());
}else {
throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
}
}
return respose;
}finally{
responseTable.remove(unique);
}
}
示例12: getRemoteAddress
import io.netty.channel.Channel; //导入方法依赖的package包/类
private String getRemoteAddress(Channel channel){
InetSocketAddress addr = (InetSocketAddress) channel.remoteAddress();
return addr.getAddress().getHostAddress();
}
示例13: onHandshake
import io.netty.channel.Channel; //导入方法依赖的package包/类
@EventHandler
public void onHandshake(HandshakeEvent event) {
Channel channel = event.getChannel();
PacketHandshake packet = event.getPacket();
String header = "auth";
String version = Cloud.getInstance().getVersion();
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
// checks if address is allowed by the whitelist
if(!Cloud.getInstance().getServer().getWhitelist().allowed(remoteAddress)) {
packet.respond(new PacketRespond(header, version, ResponseStatus.FORBIDDEN));
channel.disconnect();
}
// checks if the instance name is valid
if(!Validation.INSTANCE_NAME.matches(packet.identifier)) {
packet.respond(new PacketRespond(header, version, ResponseStatus.BAD_REQUEST));
channel.disconnect();
return;
}
// checks if already a daemon from this host connected to the server
if(packet.type == ClientType.DAEMON && Cloud.getInstance().getClientManager().contains(remoteAddress)) {
packet.respond(new PacketRespond(header, version, ResponseStatus.FORBIDDEN));
channel.disconnect();
return;
}
//.
packet.respond(new PacketRespond(header, version, ResponseStatus.OK));
// Add client
MooClient client = new MooClient(packet.identifier,
remoteAddress.getAddress().getHostAddress(),
remoteAddress.getPort(), packet.subPort, packet.type, channel);
client.setId(Cloud.getInstance().getClientManager().add(client));
// fire event of client connection
EventExecutor.getInstance().execute(new MooClientConnectedEvent(client));
Cloud.getInstance().getLogger().debug(ConsoleColor.GREEN.toString()
+ client.getType() + " client connected @(" + remoteAddress.getAddress().getHostAddress() + ")");
}
示例14: invokeSyncImpl
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 发送消息,同步等待响应
* @param channel
* @param request
* @param timeoutMillis
* @return
* @throws InterruptedException
* @throws RemotingSendRequestException
* @throws RemotingTimeoutException
*/
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
final int opaque = request.getOpaque();
try {
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
this.responseTable.put(opaque, responseFuture);
final SocketAddress addr = channel.remoteAddress();
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
responseTable.remove(opaque);
responseFuture.setCause(f.cause());
responseFuture.putResponse(null);
plog.warn("send a request command to channel <" + addr + "> failed.");
}
});
// 阻塞等待响应,直到responseFuture.putResponse方法,或者超时
RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
if (null == responseCommand) {
if (responseFuture.isSendRequestOK()) {
throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis,
responseFuture.getCause());
} else {
throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
}
}
return responseCommand;
} finally {
this.responseTable.remove(opaque);
}
}
示例15: invokeSyncImpl
import io.netty.channel.Channel; //导入方法依赖的package包/类
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
//获取请求标识,该标识在RemotingCommand 创建时,便经过 int opaque = requestId.getAndIncrement()生成。
//而RemotingCommand.requestId = new AtomicInteger(0) ,该属性是属于RemotingCommand的类属性,并且getAndIncrement是线程安全的,
//所以,在RemotingCommand 创建实例变量时,便可以生成一个唯一的opaque标识了。
final int opaque = request.getOpaque();
try {
//rmq使用CountDownLatch实现了同步调用的Future模式
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
//step 1->responseTable.put
//放入响应缓存里,key为opaque,可以以responseFuture一一对应
this.responseTable.put(opaque, responseFuture);
final SocketAddress addr = channel.remoteAddress();
//step 2->channel.writeAndFlush(request),正真发起网络请求
//这里使用了netty 的ChannelFutureListener为了监听发送结果,这里使用了闭包的方式实现
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
//step 3-> 注册发送消息结果,ChannelFuture返回true,则说明发送消息成功了。
//但任然需要等待响应。注册该监听器,是为了避免发送失败,但客户端任然在等待,
//直到超时的情况,否则,如果短时间内被调用方不可用,就会导致大量线程在闲置等待响应结果。
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
responseTable.remove(opaque);
responseFuture.setCause(f.cause());
responseFuture.putResponse(null);
PLOG.warn("send a request command to channel <" + addr + "> failed.");
}
});
//step 4->responseFuture.waitResponse,这里同步等待远程调用的响应结果
RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
if (null == responseCommand) {
if (responseFuture.isSendRequestOK()) {
throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis,
responseFuture.getCause());
} else {
throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
}
}
return responseCommand;
} finally {
//这里是为了处理等待超时,我们任然需要移除该次请求。
this.responseTable.remove(opaque);
}
}