Search in sources :

Example 16 with IpAddress

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();
    }
}
Also used : NioServer(org.jgroups.blocks.cs.NioServer) IpAddress(org.jgroups.stack.IpAddress) NioClient(org.jgroups.blocks.cs.NioClient) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 17 with IpAddress

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;
}
Also used : ByteBuffer(java.nio.ByteBuffer) IpAddress(org.jgroups.stack.IpAddress) IOException(java.io.IOException) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 18 with IpAddress

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);
    }
}
Also used : IpAddress(org.jgroups.stack.IpAddress) Address(org.jgroups.Address) IpAddress(org.jgroups.stack.IpAddress)

Example 19 with IpAddress

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) IpAddress(org.jgroups.stack.IpAddress) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 20 with IpAddress

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));
}
Also used : StackType(org.jgroups.util.StackType) IpAddress(org.jgroups.stack.IpAddress) InetAddress(java.net.InetAddress) Address(org.jgroups.Address) IpAddress(org.jgroups.stack.IpAddress) InetAddress(java.net.InetAddress)

Aggregations

IpAddress (org.jgroups.stack.IpAddress)87 InetSocketAddress (java.net.InetSocketAddress)15 Address (org.jgroups.Address)13 InetAddress (java.net.InetAddress)7 Test (org.testng.annotations.Test)7 Event (org.jgroups.Event)6 PhysicalAddress (org.jgroups.PhysicalAddress)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 JChannel (org.jgroups.JChannel)3 ManagedOperation (org.jgroups.annotations.ManagedOperation)3 DefaultSocketFactory (org.jgroups.util.DefaultSocketFactory)3 Container (io.fabric8.kubernetes.api.model.Container)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ServerSocket (java.net.ServerSocket)2