java获取chanel的ip,Java Channel.remoteAddress方法代碼示例

本文整理匯總了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

​點讚 4

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 "";

}

開發者ID:jiumao-org,項目名稱:wechat-mall,代碼行數:19,

示例2: getKey

​點讚 3

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;

}

開發者ID:tiglabs,項目名稱:jsf-sdk,代碼行數:22,

示例3: parseChannelRemoteAddr

​點讚 3

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 "";

}

開發者ID:ChinaLHR,項目名稱:JavaQuarkBBS,代碼行數:24,

示例4: parseChannelRemoteAddr

​點讚 3

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 "";

}

開發者ID:lirenzuo,項目名稱:rocketmq-rocketmq-all-4.1.0-incubating,代碼行數:19,

示例5: parseRemoteAddr

​點讚 3

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 "";

}

開發者ID:beyondfengyu,項目名稱:DistributedID-SDK,代碼行數:24,

示例6: getRemoteAddress

​點讚 2

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 "";

}

開發者ID:Tencent,項目名稱:angel,代碼行數:8,

示例7: parseChannelRemoteName

​點讚 2

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 "";

}

開發者ID:jiumao-org,項目名稱:wechat-mall,代碼行數:11,

示例8: invokeSyncImpl

​點讚 2

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 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);

}

}

開發者ID:lirenzuo,項目名稱:rocketmq-rocketmq-all-4.1.0-incubating,代碼行數:41,

示例9: getRemoteIp

​點讚 2

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();

}

開發者ID:lirenzuo,項目名稱:rocketmq-rocketmq-all-4.1.0-incubating,代碼行數:9,

示例10: setChannel

​點讚 2

import io.netty.channel.Channel; //導入方法依賴的package包/類

public void setChannel(Channel channel){

this.channel = channel;

super.remoteAddress = channel.remoteAddress();

super.localAddress = channel.localAddress();

}

開發者ID:tiglabs,項目名稱:jsf-sdk,代碼行數:6,

示例11: invokeSyncImpl

​點讚 2

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 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);

}

}

開發者ID:yanghuijava,項目名稱:elephant,代碼行數:35,

示例12: getRemoteAddress

​點讚 2

import io.netty.channel.Channel; //導入方法依賴的package包/類

private String getRemoteAddress(Channel channel){

InetSocketAddress addr = (InetSocketAddress) channel.remoteAddress();

return addr.getAddress().getHostAddress();

}

開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:5,

示例13: onHandshake

​點讚 2

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() + ")");

}

開發者ID:Superioz,項目名稱:MooProject,代碼行數:45,

示例14: invokeSyncImpl

​點讚 2

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 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);

}

}

開發者ID:beyondfengyu,項目名稱:ConfigCenter,代碼行數:51,

示例15: invokeSyncImpl

​點讚 2

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 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);

}

}

開發者ID:lyy4j,項目名稱:rmq4note,代碼行數:56,

注:本文中的io.netty.channel.Channel.remoteAddress方法示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值