use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class NioServerTest method init.
@BeforeMethod
protected void init() throws Exception {
srv = new NioServer(Util.getLoopback(), 0);
srv.sendBufferSize(send_buf_size).receiveBufferSize(recv_buf_size);
srv.start();
client = new NioClient(null, 0, Util.getLoopback(), ((IpAddress) srv.localAddress()).getPort());
client.sendBufferSize(send_buf_size).receiveBufferSize(recv_buf_size);
client.maxSendBuffers(1000);
client.start();
for (int i = 0; i < senders.length; i++) {
senders[i] = new Sender(counter, latch, client);
senders[i].start();
}
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class NioConnection method readPeerAddress.
protected Address readPeerAddress() throws Exception {
while (recv_buf.read(channel)) {
int current_position = recv_buf.position() - 1;
ByteBuffer buf = recv_buf.get(current_position);
if (buf == null)
return null;
// Workaround for JDK8 compatibility
// flip() returns java.nio.Buffer in JDK8, but java.nio.ByteBuffer since JDK9.
((java.nio.Buffer) buf).flip();
switch(current_position) {
case // cookie
0:
byte[] cookie_buf = getBuffer(buf);
if (!Arrays.equals(cookie, cookie_buf))
throw new IllegalStateException("BaseServer.NioConnection.readPeerAddress(): cookie read by " + server.localAddress() + " does not match own cookie; terminating connection");
recv_buf.add(ByteBuffer.allocate(Global.SHORT_SIZE));
break;
case // version
1:
short version = buf.getShort();
if (!Version.isBinaryCompatible(version))
throw new IOException("packet from " + channel.getRemoteAddress() + " has different version (" + Version.print(version) + ") from ours (" + Version.printVersion() + "); discarding it");
recv_buf.add(ByteBuffer.allocate(Global.SHORT_SIZE));
break;
case // length of address
2:
short addr_len = buf.getShort();
recv_buf.add(ByteBuffer.allocate(addr_len));
break;
case // address
3:
byte[] addr_buf = getBuffer(buf);
ByteArrayDataInputStream in = new ByteArrayDataInputStream(addr_buf);
IpAddress addr = new IpAddress();
addr.readFrom(in);
return addr;
default:
throw new IllegalStateException(String.format("position %d is invalid", recv_buf.position()));
}
}
return null;
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class TcpConnection method readPeerAddress.
/**
* Reads the peer's address. First a cookie has to be sent which has to
* match my own cookie, otherwise the connection will be refused
*/
protected Address readPeerAddress(Socket client_sock) throws Exception {
int timeout = client_sock.getSoTimeout();
client_sock.setSoTimeout(server.peerAddressReadTimeout());
try {
// read the cookie first
byte[] input_cookie = new byte[cookie.length];
in.readFully(input_cookie, 0, input_cookie.length);
if (!Arrays.equals(cookie, input_cookie))
throw new SocketException(String.format("%s: BaseServer.TcpConnection.readPeerAddress(): cookie sent by " + "%s:%d does not match own cookie; terminating connection", server.localAddress(), client_sock.getInetAddress(), client_sock.getPort()));
// then read the version
short version = in.readShort();
if (!Version.isBinaryCompatible(version))
throw new IOException("packet from " + client_sock.getInetAddress() + ":" + client_sock.getPort() + " has different version (" + Version.print(version) + ") from ours (" + Version.printVersion() + "); discarding it");
// address length is only needed by NioConnection
in.readShort();
Address client_peer_addr = new IpAddress();
client_peer_addr.readFrom(in);
updateLastAccessed();
return client_peer_addr;
} finally {
client_sock.setSoTimeout(timeout);
}
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class IpAddressTest method testStreamableWithHighPort.
public static void testStreamableWithHighPort() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream oos = new DataOutputStream(bos);
byte[] buf = null;
ByteArrayInputStream bis = null;
DataInputStream dis;
IpAddress x, x2;
x = createStackConformantAddress(65535);
x.writeTo(oos);
buf = bos.toByteArray();
bis = new ByteArrayInputStream(buf);
dis = new DataInputStream(bis);
x2 = new IpAddress();
x2.readFrom(dis);
System.out.println("x: " + x + ", x2: " + x2);
assert x2.getPort() > 0;
Assert.assertEquals(x.getPort(), x2.getPort());
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class IpAddressTest method testEqualityWithDnsRoundRobin.
public static void testEqualityWithDnsRoundRobin() throws UnknownHostException {
IpAddress x1, x2, x3;
StackType type = Util.getIpStackType();
String tmp = type == StackType.IPv6 ? "::1" : "127.0.0.1";
InetAddress addr = InetAddress.getByName(tmp);
byte[] rawAddr = addr.getAddress();
InetAddress inet1 = InetAddress.getByAddress("MyHost1", rawAddr);
InetAddress inet2 = InetAddress.getByAddress("MyHost2", rawAddr);
InetAddress inet3 = InetAddress.getByAddress("MyHost3", rawAddr);
Assert.assertEquals(inet1, inet2);
x1 = new IpAddress(inet1, 5555);
x2 = new IpAddress(inet2, 5555);
x3 = new IpAddress(inet3, 5555);
Assert.assertEquals(x1, x2);
Assert.assertEquals(x3, x1);
Set<Address> s = new HashSet<>();
Collections.addAll(s, x1, x2, x3);
System.out.println("s=" + s);
Assert.assertEquals(1, s.size());
Map<Address, String> m = new HashMap<>();
m.put(x1, "Bela");
m.put(x2, "Michelle");
m.put(x3, "Nicole");
Assert.assertEquals(1, m.size());
Assert.assertEquals("Nicole", m.get(x1));
}
Aggregations