gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r32297 - in gnunet-java/src/main/java/org/gnunet/gns: . cal


From: gnunet
Subject: [GNUnet-SVN] r32297 - in gnunet-java/src/main/java/org/gnunet/gns: . callbacks records
Date: Tue, 11 Feb 2014 12:16:15 +0100

Author: dold
Date: 2014-02-11 12:16:15 +0100 (Tue, 11 Feb 2014)
New Revision: 32297

Added:
   gnunet-java/src/main/java/org/gnunet/gns/Gns.java
   gnunet-java/src/main/java/org/gnunet/gns/GnsTool.java
   gnunet-java/src/main/java/org/gnunet/gns/RecordFlags.java
   gnunet-java/src/main/java/org/gnunet/gns/callbacks/
   gnunet-java/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java
   gnunet-java/src/main/java/org/gnunet/gns/records/
   gnunet-java/src/main/java/org/gnunet/gns/records/ARecordData.java
   gnunet-java/src/main/java/org/gnunet/gns/records/PkeyRecordData.java
   gnunet-java/src/main/java/org/gnunet/gns/records/RecordData.java
   gnunet-java/src/main/java/org/gnunet/gns/records/UnknownRecordData.java
Log:
- missing GNS classes


Added: gnunet-java/src/main/java/org/gnunet/gns/Gns.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/Gns.java                           
(rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/Gns.java   2014-02-11 11:16:15 UTC 
(rev 32297)
@@ -0,0 +1,137 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns;
+
+import org.gnunet.gns.callbacks.LookupResultProcessor;
+import org.gnunet.gns.messages.ClientLookupMessage;
+import org.gnunet.gns.messages.ClientLookupResultMessage;
+import org.gnunet.requests.MatchingRequestContainer;
+import org.gnunet.requests.SimpleRequest;
+import org.gnunet.util.*;
+import org.gnunet.util.crypto.EcdsaPrivateKey;
+import org.gnunet.util.crypto.EcdsaPublicKey;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * API to the GNUnet name system.
+ */
+public class Gns {
+    private static final Logger logger = LoggerFactory
+            .getLogger(Gns.class);
+    /**
+     * All pending and active lookup requests.
+     */
+    private MatchingRequestContainer<Long, 
SimpleRequest<LookupResultProcessor>> lookupRequests;
+
+    /**
+     * Request ID for lookup requests.
+     */
+    private long nextUID = 1;
+
+    /**
+     * Client connected to the GNS service.
+     */
+    private Client client;
+
+    private RelativeTime reconnectBackoff = RelativeTime.STD_BACKOFF;
+
+
+    public class GNSMessageReceiver extends RunaboutMessageReceiver {
+        public void visit(ClientLookupResultMessage m) {
+            SimpleRequest<LookupResultProcessor> r = 
lookupRequests.pollRequest(m.id);
+            if (null == r) {
+                logger.warn("no matching request for lookup result");
+                return;
+            }
+            r.getContext().process(m.records);
+        }
+
+        @Override
+        public void handleError() {
+            logger.warn("Error receiving from GNS service, reconnecting.");
+            Scheduler.addDelayed(reconnectBackoff, new Scheduler.Task() {
+                @Override
+                public void run(Scheduler.RunContext ctx) {
+                    client.reconnect();
+                    // re-send active requests
+                    lookupRequests.restart();
+                }
+            });
+            reconnectBackoff = reconnectBackoff.backoff();
+        }
+    }
+
+    /**
+     * Connect to the GNS service
+     *
+     * @param cfg configuration to use
+     */
+    public Gns(Configuration cfg) {
+        client = new Client("gns", cfg);
+        lookupRequests = new MatchingRequestContainer<Long, 
SimpleRequest<LookupResultProcessor>>(client);
+        client.installReceiver(new GNSMessageReceiver());
+    }
+
+    /**
+     * Perform an asynchronous lookup operation on the GNS.
+     *
+     * @param name the name to look up
+     * @param zone zone to look in
+     * @param type the GNS record type to look for
+     * @param onlyCached true to only check locally (not in the DHT)
+     * @param shortenZoneKey the private key of the shorten zone (can be NULL);
+     *                    specify to enable automatic shortening (given a PSEU
+     *                    record, if a given pseudonym is not yet used in the
+     *                    shorten zone, we automatically add the respective 
zone
+     *                    under that name)
+     * @param proc function to call on result
+     * @return handle to the queued request
+     */
+    public Cancelable lookup(String name,
+                             EcdsaPublicKey zone,
+                             long type, boolean onlyCached,
+                             EcdsaPrivateKey shortenZoneKey,
+                             LookupResultProcessor proc) {
+        ClientLookupMessage m = new ClientLookupMessage();
+
+        if (null != shortenZoneKey) {
+            m.haveKey = 1;
+            m.shortenKey = shortenZoneKey;
+        } else {
+            m.haveKey = 0;
+            m.shortenKey = EcdsaPrivateKey.zeroKey();
+        }
+        m.id = nextUID++;
+        m.name = name;
+        m.onlyCached = onlyCached ? 1 : 0;
+        m.type = type;
+        m.zone = zone;
+
+        return lookupRequests.addRequest(m.id, new 
SimpleRequest<LookupResultProcessor>(m, proc));
+    }
+
+
+    public void disconnect() {
+        client.disconnect();
+        client = null;
+    }
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/GnsTool.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/GnsTool.java                       
        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/GnsTool.java       2014-02-11 
11:16:15 UTC (rev 32297)
@@ -0,0 +1,81 @@
+package org.gnunet.gns;
+
+
+import org.gnunet.gns.callbacks.LookupResultProcessor;
+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;
+
+public class GnsTool {
+    public static void main(String[] args) {
+        int ret = new Program(args) {
+            @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();
+        System.exit(ret);
+    }
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/RecordFlags.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/RecordFlags.java                   
        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/RecordFlags.java   2014-02-11 
11:16:15 UTC (rev 32297)
@@ -0,0 +1,48 @@
+/*
+ This file is part of GNUnet.
+  (C) 2014 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns;
+
+
+public interface RecordFlags {
+    /**
+     * No special options.
+     */
+    public static final int NONE = 0;
+    /**
+     * No special options.
+     */
+    public static final int PRIVATE = 2;
+    /**
+     * This record was added automatically by the system
+     * and is pending user confimation.
+     */
+    public static final int PENDING = 4;
+    /**
+     * This expiration time of the record is a relative
+     * time (not an absolute time).
+     */
+    public static final int EXPIRATION = 8;
+    /**
+     * This record should not be used unless all (other) records with an 
absolute
+     * expiration time have expired.
+     */
+    public static final int SHADOW_RECORD = 16;
+}

Added: 
gnunet-java/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java
===================================================================
--- 
gnunet-java/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java   
                            (rev 0)
+++ 
gnunet-java/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java   
    2014-02-11 11:16:15 UTC (rev 32297)
@@ -0,0 +1,35 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns.callbacks;
+
+import org.gnunet.gns.GnsRecord;
+
+/**
+ * Processor for GNS request results.
+ */
+public interface LookupResultProcessor {
+    /**
+     * Process an array of records.
+     *
+     * @param records Records to process.
+     */
+    void process(GnsRecord[] records);
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/records/ARecordData.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/records/ARecordData.java           
                (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/records/ARecordData.java   
2014-02-11 11:16:15 UTC (rev 32297)
@@ -0,0 +1,68 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns.records;
+
+
+import com.google.common.net.InetAddresses;
+import org.gnunet.construct.FixedSizeIntegerArray;
+import org.gnunet.construct.UnionCase;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * A DNS A Record.
+ */
address@hidden(1)
+public class ARecordData implements RecordData {
+
+    @FixedSizeIntegerArray(length = 4, bitSize = 8, signed = false)
+    public byte[] addr;
+
+    @SuppressWarnings("UnusedDeclaration")
+    public static String recordTypeString = "A";
+
+    public static ARecordData fromString(String s) {
+        InetAddress addr = InetAddresses.forString(s);
+        byte[] addrBytes = addr.getAddress();
+        if (addrBytes.length != 4) {
+            return null;
+        }
+        ARecordData recordData = new ARecordData();
+        recordData.addr = addrBytes;
+        return recordData;
+    }
+
+    @Override
+    public String asRecordString() {
+        if (addr.length != 4) {
+            return null;
+        }
+        InetAddress inetAddress;
+        try {
+            inetAddress = Inet4Address.getByAddress(addr);
+        } catch (UnknownHostException e) {
+            return null;
+        }
+        return inetAddress.getHostAddress();
+    }
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/records/PkeyRecordData.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/records/PkeyRecordData.java        
                        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/records/PkeyRecordData.java        
2014-02-11 11:16:15 UTC (rev 32297)
@@ -0,0 +1,33 @@
+package org.gnunet.gns.records;
+
+import org.gnunet.construct.NestedMessage;
+import org.gnunet.construct.UnionCase;
+import org.gnunet.util.crypto.EcdsaPublicKey;
+
+/**
+ * A GNS PKEY record.
+ */
address@hidden(65536)
+public class PkeyRecordData implements RecordData {
+
+    @NestedMessage
+    public EcdsaPublicKey publicKey;
+
+    @SuppressWarnings("UnusedDeclaration")
+    public static String recordTypeString = "PKEY";
+
+    public static PkeyRecordData fromString(String s) {
+        EcdsaPublicKey publicKey = EcdsaPublicKey.fromString(s);
+        if (null == publicKey) {
+            return null;
+        }
+        PkeyRecordData recordData = new PkeyRecordData();
+        recordData.publicKey = publicKey;
+        return recordData;
+    }
+
+    @Override
+    public String asRecordString() {
+        return publicKey.toString();
+    }
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/records/RecordData.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/records/RecordData.java            
                (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/records/RecordData.java    
2014-02-11 11:16:15 UTC (rev 32297)
@@ -0,0 +1,30 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns.records;
+
+import org.gnunet.construct.MessageUnion;
+
+/**
+ * Tag interface for message for GNS record data union members.
+ */
+public interface RecordData extends MessageUnion {
+    public String asRecordString();
+}

Added: gnunet-java/src/main/java/org/gnunet/gns/records/UnknownRecordData.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/gns/records/UnknownRecordData.java     
                        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/gns/records/UnknownRecordData.java     
2014-02-11 11:16:15 UTC (rev 32297)
@@ -0,0 +1,39 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.gns.records;
+
+
+import org.gnunet.construct.IntegerFill;
+
+public class UnknownRecordData implements RecordData {
+
+    @IntegerFill(signed = true, bitSize = 8)
+    public byte[] data;
+
+    public String toString() {
+        return "(unknown record)";
+    }
+
+    @Override
+    public String asRecordString() {
+        return "(unknown)";
+    }
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]