aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/gns/GnsTool.java
blob: 0d33dc11e1c7f84d3fbc8a14f389e93199d36615 (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
package org.gnunet.gns;


import org.gnunet.identity.Identity;
import org.gnunet.identity.IdentityCallback;
import org.gnunet.util.Program;
import org.gnunet.util.getopt.Argument;
import org.gnunet.util.getopt.ArgumentAction;

/**
 * Command line tool for GNS.
 */
public class GnsTool {
    public static void main(String[] args) {
        int ret = new Program() {
            @Argument(
                    shortname = "u",
                    longname = "lookup",
                    action = ArgumentAction.STORE_STRING,
                    description = "Lookup a record for the given name")
            String name;

            @Argument(
                    shortname = "z",
                    longname = "zone",
                    action = ArgumentAction.STORE_STRING,
                    description = "Lookup a record in the given zone")
            String zone = "master-zone";

            @Argument(
                    shortname = "t",
                    longname = "type",
                    action = ArgumentAction.STORE_STRING,
                    description = "Lookup a record of the given type (defaut: A)")
            String type = "A";

            @Override
            protected void run() {
                if (null == name) {
                    System.err.println("no name given");
                    setReturnValue(1);
                    return;
                }

                final long typeId = GnsRecord.getIdFromString(type);
                if (typeId < 0) {
                    System.err.println(String.format("type '%s' not known", type));
                    setReturnValue(1);
                    return;
                }

                System.out.println("looking for records of type id " + typeId);

                Identity.lookup(getConfiguration(), zone, new IdentityCallback() {
                    @Override
                    public void onEgo(Identity.Ego ego) {
                        System.out.println("looking in zone " + ego.getPublicKey());
                        final Gns gns = new Gns(getConfiguration());
                        gns.lookup(name, ego.getPublicKey(), typeId, false, null, new LookupResultProcessor() {
                            @Override
                            public void process(GnsRecord[] records) {
                                System.out.println("got " + records.length + " records");
                                gns.disconnect();
                                for (GnsRecord record : records) {
                                    String s = record.getRecordData().asRecordString();
                                    System.out.println(
                                            String.format("Type: %s, Value: %s", record.recordType, s));
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(String errorMessage) {
                        System.err.println("could not look up zone ego '" + name + "': " + errorMessage);
                    }
                });

            }
        }.start(args);
        System.exit(ret);
    }
}