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