org.zbus.net.tcp.TcpSession Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zbus Show documentation
Show all versions of zbus Show documentation
lightweight message queue, service bus
package org.zbus.net.tcp;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.zbus.net.Session;
import io.netty.channel.ChannelHandlerContext;
public class TcpSession implements Session {
private ChannelHandlerContext ctx;
private final String id;
private ConcurrentMap attributes = null;
public TcpSession(ChannelHandlerContext ctx) {
this.ctx = ctx;
this.id = UUID.randomUUID().toString();
}
@Override
public String id() {
return id;
}
public String getRemoteAddress() {
ctx.isRemoved();
return ctx.channel().remoteAddress().toString();
}
public String getLocalAddress() {
return ctx.channel().localAddress().toString();
}
public void write(Object msg){
ctx.writeAndFlush(msg);
}
public void writeAndFlush(Object msg){
ctx.writeAndFlush(msg);
}
@Override
public void flush() {
ctx.flush();
}
@Override
public void close() throws IOException {
ctx.close();
}
@Override
public boolean isActive() {
return ctx.channel().isActive();
}
@SuppressWarnings("unchecked")
public V attr(String key) {
if (this.attributes == null) {
return null;
}
return (V) this.attributes.get(key);
}
public void attr(String key, V value) {
if(value == null){
if(this.attributes != null){
this.attributes.remove(key);
}
return;
}
if (this.attributes == null) {
synchronized (this) {
if (this.attributes == null) {
this.attributes = new ConcurrentHashMap();
}
}
}
this.attributes.put(key, value);
}
@Override
public String toString() {
return "Session ["
+ "remote=" + getRemoteAddress()
+ ", active=" + isActive()
+ "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy