aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/testbed/ControllerProc.java
blob: 9eaca2ca674a65a433b9bd103c1b4ea5e3b2dd4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.gnunet.testbed;

import com.google.common.base.Charsets;
import org.gnunet.util.Helper;
import org.gnunet.util.RunaboutMessageReceiver;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

/**
 * A controller process.
 * The controller process is either a local helper process, or an ssh process that starts and controls
 * the testbed helper on a remote machine.
 */
public class ControllerProc {
    final Helper helper;


    public class ControllerProcReceiver extends RunaboutMessageReceiver {
        public void visit(HelperReplyMessage m) {

        }
        @Override
        public void handleError() {

        }
    }

    /**
     * Starts a controller process at the given host.  The given host's configration
     * is used as a Template configuration to use for the remote controller; the
     * remote controller will be started with a slightly modified configuration
     * (port numbers, unix domain sockets and service home values are changed as per
     * TESTING library on the remote host).  The modified configuration replaces the
     * host's existing configuration before signalling success through the
     * GNUNET_TESTBED_ControllerStatusCallback()
     *
     * @param trustedIP the ip address of the controller which will be set as TRUSTED
     *          HOST(all connections form this ip are permitted by the testbed) when
     *          starting testbed controller at host. This can either be a single ip
     *          address or a network address in CIDR notation.
     * @param host the host where the controller has to be started.  CANNOT be NULL.
     * @param cb function called when the controller is successfully started or
     *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
     *          called if cb is called with GNUNET_SYSERR as status. Will never be
     *          called in the same task as 'GNUNET_TESTBED_controller_start'
     *          (synchronous errors will be signalled by returning NULL). This
     *          parameter cannot be NULL.
     */
    public ControllerProc(String trustedIP, Host host, ControllerStatusCallback cb) {
        if (host.isLocal()) {
            helper = new Helper(false, "gnunet-testbed-helper", null, new ControllerProcReceiver());
        } else {
            throw new AssertionError("not implemented yet");
        }
    }


    private HelperInitMessage makeHelperInitMessage(String trustedIP, Host host) {
        HelperInitMessage m = new HelperInitMessage();
        if (host.hostname == null) {
            m.hostname = null;
            m.hostname_size = 0;
        } else {
            m.hostname_size =  host.hostname.length();
            m.hostname = host.hostname.getBytes(Charsets.UTF_8);
        }
        m.trusted_ip_size = trustedIP.length();
        m.trusted_ip = trustedIP;

        byte[] serialized_config = host.cfg.serialize().getBytes();

        Deflater compresser = new Deflater();
        compresser.setInput(serialized_config);
        compresser.finish();

        ByteArrayOutputStream s = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        while (!compresser.finished()) {
            int n = compresser.deflate(buf);
            s.write(buf, 0, n);
        }

        m.compressed_config = s.toByteArray();
        m.config_size = serialized_config.length;

        return m;
    }
}