/*
 * Copyright 2022 Jérôme de Bretagne
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %ExternalCopyright%
 */

import java.io.IOException;

import com.ericsson.otp.erlang.OtpGenericTransportFactory;
import com.ericsson.otp.erlang.OtpLocalNode;
import com.ericsson.otp.erlang.OtpNode;
import com.ericsson.otp.erlang.OtpPeer;
import com.ericsson.otp.erlang.OtpServerTransport;
import com.ericsson.otp.erlang.OtpSocketTransportFactory;
import com.ericsson.otp.erlang.OtpTransport;
import com.ericsson.otp.erlang.OtpTransportFactory;

public class GenericTransportFactoryTest {

    static boolean serverOk = false;

    /**
     * A minimalist custom generic transport factory working without epmd,
     * using the node name as the identifier of the listening socket port.
     */
    public static class TestFactory extends OtpGenericTransportFactory {

        OtpSocketTransportFactory tf = new OtpSocketTransportFactory();

        public OtpTransport createTransport(final OtpPeer peer)
                throws IOException {
            String addr = "localhost";
            String peerName = peer.alive();
            int port = 0;
            try {
                port = Integer.parseInt(peerName);
            } catch (NumberFormatException e) {
            }
            System.out.println("Creating transport to " + addr + ", " + port);
            return tf.createTransport(addr, port);
        }

        public OtpServerTransport createServerTransport(final OtpLocalNode node)
                throws IOException {
            String nodeName = node.alive();
            int port = 0;
            try {
                port = Integer.parseInt(nodeName);
                serverOk = true;
            } catch (NumberFormatException e) {
                serverOk = false;
            }
            System.out.println("Creating server transport to " + port);
            return tf.createServerTransport(port);
        }

    }

    public static void main(final String[] args) throws IOException {

        OtpTransportFactory customFactory = new TestFactory();

        // The 2 test nodes with arbitrary socket port numbers
        String nodeName1 = "65432";
        String nodeName2 = "54321";

        // Create the first node
        final OtpNode node1 = new OtpNode(nodeName1, customFactory);
        if (!serverOk) {
            fail("Custom server transport 1 was not created");
        }
        System.out.println("Node 1 accepting connections on " + node1.port());

        // Create the second node
        final OtpNode node2 = new OtpNode(nodeName2, customFactory);
        if (!serverOk) {
            fail("Custom server transport 2 was not created");
        }
        System.out.println("Node 2 accepting connections on " + node2.port());

        // Finally test the connection between the 2 nodes
        if (!node1.ping(nodeName2, 2000)) {
            fail("The 2 nodes couldn't connect");
        }

    }

    private static void fail(final String string) {
        System.err.println(string);
        System.exit(1);
    }
}

@@ -1090,46 +1090,8 @@
  %7B%0A%0A
-    static boolean serverOk = false;%0A%0A
     
@@ -1389,32 +1389,79 @@
 nsportFactory();
+%0A%09%09String fake_epmd_name;%0A%09%09int fake_epmd_port;
 %0A%0A        public
@@ -1660,151 +1660,152 @@
  0;%0A
-            try %7B%0A                port = Integer.parseInt(peerName);%0A            %7D catch (NumberFormatException e) %7B%0A            %7D%0A            
+%09%09%09if (peerName == fake_epmd_name) %7B%0A%09%09%09%09port = fake_epmd_port;%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09fail(%22Fake epmd don't know port nr of %22 + peerName);%0A%09%09%09%7D%0A%09%09%09
 Syst
@@ -2043,34 +2043,27 @@
 Exception %7B%0A
-          
+%09%09%09
   String nod
@@ -2088,353 +2088,378 @@
 ();%0A
-            int port = 0;%0A            try %7B%0A                port = Integer.parseInt(nodeName);%0A                serverOk = true;%0A            %7D catch (NumberFormatException e) %7B%0A                serverOk = false;%0A            %7D%0A            System.out.println(%22Creating server transport to %22 + port);%0A            return tf.createServerTransport(
+%09%09%09  OtpServerTransport transport = tf.createServerTransport(0);%0A%09%09%09  System.out.println(%22Creating server transport for node %22%0A%09%09%09%09%09%09%09%09 + nodeName + %22 to port %22%0A%09%09%09%09%09%09%09%09 + transport.getLocalPort());%0A%0A%09%09%09  // Store last created server node in our fake %22epmd database%22.%0A%09%09%09  fake_epmd_name = nodeName;%0A%09%09%09  fake_epmd_port = transport.getLocalPort();%0A%0A%09%09%09  return trans
 port
-)
 ;%0A  
@@ -2704,13 +2704,13 @@
  = %22
-65432
+Alice
 %22;%0A 
@@ -2740,13 +2740,11 @@
  = %22
-54321
+Bob
 %22;%0A%0A
@@ -2849,463 +2849,109 @@
 y);%0A
-        if (!serverOk) %7B%0A            fail(%22Custom server transport 1 was not created%22);%0A        %7D%0A        System.out.println(%22Node 1 accepting connections on %22 + node1.port());%0A%0A        // Create the second node%0A        final OtpNode node2 = new OtpNode(nodeName2, customFactory);%0A        if (!serverOk) %7B%0A            fail(%22Custom server transport 2 was not created%22);%0A        %7D%0A        System.out.println(%22Node 2 accepting connections on %22 + node2.port()
+%0A        // Create the second node%0A        final OtpNode node2 = new OtpNode(nodeName2, customFactory
 );%0A%0A

Fix test generic_transport_factory