gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r18046 - in gnunet-java: . src/org/gnunet/service src/org/g


From: gnunet
Subject: [GNUnet-SVN] r18046 - in gnunet-java: . src/org/gnunet/service src/org/gnunet/util src/org/gnunet/util/network src/org/gnunet/util/scheduler test/org/gnunet/util/resources
Date: Mon, 7 Nov 2011 21:17:14 +0100

Author: dold
Date: 2011-11-07 21:17:14 +0100 (Mon, 07 Nov 2011)
New Revision: 18046

Added:
   gnunet-java/src/org/gnunet/util/Client.java
   gnunet-java/src/org/gnunet/util/Connection.java
   gnunet-java/src/org/gnunet/util/Message.java
   gnunet-java/src/org/gnunet/util/MessageHandler.java
   gnunet-java/src/org/gnunet/util/Receiver.java
   gnunet-java/src/org/gnunet/util/Scheduler.java
   gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java
Removed:
   gnunet-java/src/org/gnunet/util/network/Client.java
   gnunet-java/src/org/gnunet/util/network/Connection.java
   gnunet-java/src/org/gnunet/util/network/Message.java
   gnunet-java/src/org/gnunet/util/network/MessageHandler.java
   gnunet-java/src/org/gnunet/util/network/Receiver.java
   gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java
   gnunet-java/src/org/gnunet/util/scheduler/Scheduler.java
Modified:
   gnunet-java/ISSUES
   gnunet-java/src/org/gnunet/service/StatisticsService.java
   gnunet-java/src/org/gnunet/util/AbsoluteTime.java
   gnunet-java/src/org/gnunet/util/Configuration.java
   gnunet-java/src/org/gnunet/util/RelativeTime.java
   gnunet-java/test/org/gnunet/util/resources/gnunet.config-1
Log:
package structure changes, started implementing scheduler

Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES  2011-11-07 18:43:40 UTC (rev 18045)
+++ gnunet-java/ISSUES  2011-11-07 20:17:14 UTC (rev 18046)
@@ -15,10 +15,25 @@
 * wrt the scheduler: can you select on anything else than a Connection? pipes, 
sockets (tcp/udp), files, timeout
 
 
-* logging / assertions (use guava?)
-* unit testing (JUnit vs NGUnit)
+* logging / assertions (use guava?) => slf4j
+* unit testing (JUnit vs NGUnit) => using JUnit4
 
+* Configuration.java:
+  * what about spaces in filenames? quotes in config values?
+  * formal spec of syntax?
 
+* binary message parsing:
+ * use DSL (eg. Xtext)
+ * use hand-written parsers
+ * use dynamically construct parsers at runtume from composable matchers (cf. 
pythons construct library)
+  * flexible
+  * slow
+ * generate parsers from java files using annotations
+  * is java flexible enough for this?
+   * cannot generate interfaces / add new members
+
+
+
 Used Libraries:
  * https://github.com/magnuss/java-bloomfilter (LGPL) => freeway!
  * http://code.google.com/p/junixsocket/ (Apache License 2.0)

Modified: gnunet-java/src/org/gnunet/service/StatisticsService.java
===================================================================
--- gnunet-java/src/org/gnunet/service/StatisticsService.java   2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/service/StatisticsService.java   2011-11-07 
20:17:14 UTC (rev 18046)
@@ -10,9 +10,9 @@
 import java.util.LinkedList;
 
 import org.gnunet.util.AbsoluteTime;
+import org.gnunet.util.Client;
 import org.gnunet.util.Configuration;
 import org.gnunet.util.RelativeTime;
-import org.gnunet.util.network.Client;
 
 public class StatisticsService {
 

Modified: gnunet-java/src/org/gnunet/util/AbsoluteTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/AbsoluteTime.java   2011-11-07 18:43:40 UTC 
(rev 18045)
+++ gnunet-java/src/org/gnunet/util/AbsoluteTime.java   2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -37,7 +37,7 @@
     public AbsoluteTime(final long abs_value) {
         this.abs_value = abs_value;
     }
-
+    
     public long getMilliseconds() {
         return abs_value;
     }
@@ -52,11 +52,21 @@
         return (abs_value <= other.abs_value) ? this : other;
     }
 
+    /**
+     * Returns the maximum of two time values.
+     * 
+     * @param other
+     * @return max(this,other)
+     */
     public AbsoluteTime max(final AbsoluteTime other) {
         return (abs_value >= other.abs_value) ? this : other;
 
     }
 
+    /**
+     * Calculate the remaining time 
+     * @return
+     */
     public RelativeTime getRemaining() {
         if (abs_value == Long.MAX_VALUE) {
             return RelativeTime.FOREVER;
@@ -95,6 +105,11 @@
         return new RelativeTime((long) exp);
     }
 
+    /**
+     * Calculates the difference between two absolute times.
+     * @param other
+     * @return
+     */
     public RelativeTime getDifference(final AbsoluteTime other) {
         if (other.abs_value == Long.MAX_VALUE) {
             return RelativeTime.FOREVER;
@@ -131,13 +146,4 @@
         }
         return new AbsoluteTime(abs_value - duration.getMilliseconds());
     }
-
-    public byte[] toNetwork() {
-        throw new UnsupportedOperationException();
-    }
-
-    static AbsoluteTime fromNetwork(final byte[] b) {
-        throw new UnsupportedOperationException();
-    }
-
 }

Copied: gnunet-java/src/org/gnunet/util/Client.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/network/Client.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/Client.java                         (rev 0)
+++ gnunet-java/src/org/gnunet/util/Client.java 2011-11-07 20:17:14 UTC (rev 
18046)
@@ -0,0 +1,121 @@
+package org.gnunet.util;
+
+
+/**
+ * Represents a connection to a service.
+ */
+public class Client {
+       interface MessageHandler {
+
+       }
+
+       public static class TransmitHandle {
+               /**
+                * Cancel a request for notification.
+                */
+               void cancel() {
+                       throw new UnsupportedOperationException();
+               }
+
+       }
+
+       /**
+        * Get a connection with a service.
+        * 
+        * @param service_name
+        *            name of the service
+        * @param cfg
+        *            configuration to use
+        */
+       public Client(String service_name, Configuration c) {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Read from the service.
+        * 
+        * @param handler
+        *            function to call with the message
+        * @param timeout
+        *            how long to wait until timing out
+        */
+
+       public void receive(MessageHandler handler, RelativeTime timeout) {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Ask the client to call us once the specified number of bytes are 
free in
+        * the transmission buffer. May call the notify method immediately if 
enough
+        * space is available.
+        * 
+        * @param sock
+        *            connection to the service
+        * @param size
+        *            number of bytes to send
+        * @param timeout
+        *            after how long should we give up (and call notify with buf
+        *            NULL and size 0)?
+        * @param auto_retry
+        *            if the connection to the service dies, should we 
automatically
+        *            re-connect and retry (within the timeout period) or 
should we
+        *            immediately fail in this case? Pass GNUNET_YES if the 
caller
+        *            does not care about temporary connection errors, for 
example
+        *            because the protocol is stateless
+        * @param notify
+        *            function to call
+        * @param notify_cls
+        *            closure for notify
+        * @return NULL if someone else is already waiting to be notified 
non-NULL
+        *         if the notify callback was queued (can be used to cancel 
using
+        *         GNUNET_CONNECTION_notify_transmit_ready_cancel)
+        */
+       public TransmitHandle notifyTransmitReady(int size, RelativeTime 
timeout,
+                       boolean auto_retry, TransmitReadyNotify cb) {
+               throw new UnsupportedOperationException();
+       }
+
+       
+       /**
+        * Convenience API that combines sending a request
+        * to the service and waiting for a response.
+        * If either operation times out, the callback
+        * will be called with a "NULL" response (in which
+        * case the connection should probably be destroyed).
+        *
+        * @param sock connection to use
+        * @param hdr message to transmit
+        * @param timeout when to give up (for both transmission
+        *         and for waiting for a response)
+        * @param auto_retry if the connection to the service dies, should we
+        *        automatically re-connect and retry (within the timeout period)
+        *        or should we immediately fail in this case?  Pass GNUNET_YES
+        *        if the caller does not care about temporary connection errors,
+        *        for example because the protocol is stateless
+        * @param rn function to call with the response
+        * @param rn_cls closure for rn
+        * @return GNUNET_OK on success, GNUNET_SYSERR if a request
+        *         is already pending
+        */
+       //XXX return type bool vs. exceptions
+       public boolean transmitAndGetResponse(Message m, RelativeTime timeout,
+                       boolean autoRetry, MessageHandler handler) {
+               throw new UnsupportedOperationException();
+       }
+       
+       
+       /**
+        * Wait until the service is running.
+        *
+        * @param service name of the service to wait for
+        * @param cfg configuration to use
+        * @param timeout how long to wait at most in ms
+        * @param task task to run if service is running
+        *        (reason will be "PREREQ_DONE" (service running)
+        *         or "TIMEOUT" (service not known to be running))
+        */
+
+       public static void serviceTest(String service_name, Configuration cfg, 
RelativeTime timeout, Task t) {
+               throw new UnsupportedOperationException();
+       }
+}

Modified: gnunet-java/src/org/gnunet/util/Configuration.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Configuration.java  2011-11-07 18:43:40 UTC 
(rev 18045)
+++ gnunet-java/src/org/gnunet/util/Configuration.java  2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -1,17 +1,14 @@
 package org.gnunet.util;
 
-import java.io.BufferedReader;
-import java.io.DataInputStream;
+import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Scanner;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.slf4j.Logger;
@@ -27,7 +24,7 @@
             .getLogger(Configuration.class);
 
     private static Pattern section = Pattern.compile("\\[(.*?)\\]");
-    private static Pattern tag = Pattern.compile("(.*?) =( ?.*?)");
+    private static Pattern tag = Pattern.compile("(\\S+?) =( ?.*?)");
     private static Pattern comment = Pattern.compile("\\s*[%#].*\n?");
     private static Pattern delim = Pattern.compile("\\s*\\n\\s*");
 
@@ -37,7 +34,7 @@
      * Start with an empty configuration.
      */
     public Configuration() {
-        sections = new HashMap<String, Map<String, String>>();
+        sections = new LinkedHashMap<String, Map<String, String>>();
     }
 
     /**
@@ -64,6 +61,16 @@
                 sc.next(tag);
                 String option = sc.match().group(1).trim();
                 String value = sc.match().group(2).trim();
+
+                if (value.length() != 0 && value.charAt(0) == '"') {
+                    int pos = value.indexOf('"', 1);
+                    if (pos == -1) {
+                        logger.warn("incorrecly quoted config value");
+                        continue;
+                    }
+                    value = value.substring(1, pos);
+                }
+
                 setValueString(current_section, option, value);
             } else if (!sc.hasNext()) {
                 break;
@@ -94,16 +101,27 @@
      * 
      * @param filename
      *            where to write the configuration
+     * @throws IOException
      */
-    public void write(String filename) {
-        throw new UnsupportedOperationException();
+    public void write(String filename) throws IOException {
+        BufferedWriter w = new BufferedWriter(
+                new FileWriter(new File(filename)));
+        for (Map.Entry<String, Map<String, String>> s : sections.entrySet()) {
+            w.write(s.getKey());
+            w.newLine();
+            for (Map.Entry<String, String> e : s.getValue().entrySet()) {
+                w.write(e.getKey() + " = " + e.getValue());
+                w.newLine();
+            }
+        }
+        w.close();
     }
 
     /**
      * Remove the given section and all options in it.
      */
     public void removeSection(String section) {
-        throw new UnsupportedOperationException();
+        sections.remove(section);
     }
 
     /**
@@ -123,14 +141,14 @@
     public void setValueString(String section, String option, String value) {
         Map<String, String> table = sections.get(section);
         if (table == null) {
-            table = new HashMap<String, String>();
+            table = new LinkedHashMap<String, String>();
             sections.put(section, table);
         }
         table.put(option, value);
     }
 
     public void setValueNumber(String section, String option, long value) {
-        throw new UnsupportedOperationException();
+        setValueString(section, option, "" + value);
     }
 
     public long getValueNumer(String section, String option) {
@@ -151,7 +169,8 @@
     }
 
     public boolean haveValue(String section, String option) {
-        throw new UnsupportedOperationException();
+        return sections.containsValue(section)
+                && sections.get(section).containsKey(option);
     }
 
     /**

Copied: gnunet-java/src/org/gnunet/util/Connection.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/network/Connection.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/Connection.java                             
(rev 0)
+++ gnunet-java/src/org/gnunet/util/Connection.java     2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -0,0 +1,142 @@
+package org.gnunet.util;
+
+import java.net.*;
+
+
+public class Connection {
+       public class TransmitHandle {
+               void cancel() {
+                       throw new UnsupportedOperationException();
+               }
+       }
+
+       private boolean persist;
+
+       private Connection(Socket s) {
+       }
+
+       public static Connection fromAccept(ServerSocket ss) {
+               throw new UnsupportedOperationException();
+       }
+
+       public static Connection fromExisting(Socket ss) {
+               return new Connection(ss);
+       }
+
+       public static Connection fromConnect(String hostname, int port) {
+               throw new UnsupportedOperationException();
+       }
+
+       public static Connection fromUnixPath(String path) {
+               // problematic as java has no nativ support for unix domain 
sockets
+               throw new UnsupportedOperationException();
+       }
+
+       public static Connection fromSocketAddress(SocketAddress addr) {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Receive data from the given socket. Note that this function will call
+        * "receiver" asynchronously using the scheduler. It will "immediately"
+        * return. Note that there MUST only be one active receive call per 
socket
+        * at any given point in time (so do not call receive again until the
+        * receiver callback has been invoked).
+        * 
+        * @param max
+        *            maximum number of bytes to read
+        * @param timeout
+        *            maximum amount of time to wait
+        * @param receiver
+        *            function to call with received data
+        */
+
+       public void receive(int max, RelativeTime timeout, Receiver r) {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Cancel receive job on the given socket. Note that the receiver 
callback
+        * must not have been called yet in order for the cancellation to be 
valid.
+        * 
+        * @return closure of the original receiver callback closure
+        */
+       public void cancelReceive() {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Ask the socket to call us once the specified number of bytes are 
free in
+        * the transmission buffer. May call the notify method immediately if 
enough
+        * space is available. Note that this function will abort if "size" is
+        * greater than GNUNET_SERVER_MAX_MESSAGE_SIZE.
+        * 
+        * Note that "notify" will be called either when enough buffer space is
+        * available OR when the socket is destroyed. The size parameter given 
to
+        * notify is guaranteed to be larger or equal to size if the buffer is
+        * ready, or zero if the socket was destroyed (or at least closed for
+        * writing). Finally, any time before 'notify' is called, a client may 
call
+        * "notify_transmit_ready_cancel" to cancel the transmission request.
+        * 
+        * Only one transmission request can be scheduled at the same time. 
Notify
+        * will be run with the same scheduler priority as that of the caller.
+        * 
+        * @param size
+        *            number of bytes to send
+        * @param timeout
+        *            after how long should we give up (and call notify with buf
+        *            NULL and size 0)?
+        * @param notify
+        *            function to call when buffer space is available
+        * @return non-NULL if the notify callback was queued, NULL if we are
+        *         already going to notify someone else (busy)
+        */
+       public TransmitHandle notifyTransmitReady(int size, RelativeTime 
timeout,
+                       boolean auto_retry, TransmitReadyNotify cb) {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Set the persist option on this connection handle. Indicates that the
+        * underlying socket or fd should never really be closed. Used for
+        * indicating process death.
+        */
+       public void persist() {
+               this.persist = true;
+       }
+
+       /**
+        * Disable the "CORK" feature for communication with the given socket,
+        * forcing the OS to immediately flush the buffer on transmission 
instead of
+        * potentially buffering multiple messages. Essentially reduces the OS 
send
+        * buffers to zero. Used to make sure that the last messages sent 
through
+        * the connection reach the other side before the process is terminated.
+        */
+       // XXX: error code / exception
+       public void disableCorking() {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Check if socket is valid (no fatal errors have happened so far). Note
+        * that a socket that is still trying to connect is considered valid.
+        */
+       public boolean check() {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Configure this connection to ignore shutdown signals.
+        * 
+        * @param do_ignore
+        *            GNUNET_YES to ignore, GNUNET_NO to restore default
+        */
+       public void ignoreShutdown(boolean doIgnore) {
+       }
+       
+       public Channel getChannel() {
+           throw new UnsupportedOperationException();
+       }
+
+
+}

Copied: gnunet-java/src/org/gnunet/util/Message.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/network/Message.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/Message.java                                
(rev 0)
+++ gnunet-java/src/org/gnunet/util/Message.java        2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -0,0 +1,30 @@
+package org.gnunet.util;
+
+
+
+
+public class Message { // FIXME: MessageHeader!
+       
+       // Message (byte[] data, int offset); 
+
+       // public abstract UInt16_t size();// nice for reading. How about 
writing?
+
+       // public abstract UInt16_t type();
+
+       // uint16_t addr_len;
+       
+       // @AddressLength(addr_len);
+       // byte[] address;
+
+       // @ZeroTerminatedString
+       // byte[] zero_terminated_string; // where does complex parsing logic 
live?
+       
+       // int32 foo; // read+write
+       
+       public int type;
+       
+       // size is implicit in contents. does this cover all use cases?
+       public byte[] contents;
+       
+       // msg = MessageFactory.parse (data, off, MessageHeader.class);
+}

Copied: gnunet-java/src/org/gnunet/util/MessageHandler.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/network/MessageHandler.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/MessageHandler.java                         
(rev 0)
+++ gnunet-java/src/org/gnunet/util/MessageHandler.java 2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -0,0 +1,5 @@
+package org.gnunet.util;
+
+public class MessageHandler {
+
+}

Copied: gnunet-java/src/org/gnunet/util/Receiver.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/network/Receiver.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/Receiver.java                               
(rev 0)
+++ gnunet-java/src/org/gnunet/util/Receiver.java       2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -0,0 +1,6 @@
+package org.gnunet.util;
+
+public interface Receiver {
+       public void receive(byte[] buf, long available, Object sockaddr, int 
addrlen, int errCode);
+       
+}
\ No newline at end of file

Modified: gnunet-java/src/org/gnunet/util/RelativeTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/RelativeTime.java   2011-11-07 18:43:40 UTC 
(rev 18045)
+++ gnunet-java/src/org/gnunet/util/RelativeTime.java   2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -127,23 +127,4 @@
             return new RelativeTime(this.rel_value - other.rel_value);
         }
     }
-
-    /**
-     * Convert a relative time to a string.
-     * 
-     * @return string form of the time (as milliseconds)
-     */
-    @Override
-    public String toString() {
-        return "" + this.rel_value;
-    }
-
-    public byte[] toNetwork() {
-        throw new UnsupportedOperationException();
-    }
-
-    static RelativeTime fromNetwork(final byte[] b) {
-        throw new UnsupportedOperationException();
-    }
-
 }

Copied: gnunet-java/src/org/gnunet/util/Scheduler.java (from rev 18032, 
gnunet-java/src/org/gnunet/util/scheduler/Scheduler.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/Scheduler.java                              
(rev 0)
+++ gnunet-java/src/org/gnunet/util/Scheduler.java      2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -0,0 +1,241 @@
+// FIXME: license
+
+package org.gnunet.util;
+
+import java.io.IOException;
+import java.nio.channels.Channel;
+import java.nio.channels.Selector;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+
+
+/**
+ * Schedule computations using CPS
+ * 
+ * @author Florian Dold
+ */
+public class Scheduler {
+    public enum Priority {
+        KEEP, IDLE, BACKGROUND, DEFAULT, HIGH, UI, URGENT, SHUTDOWN
+    }
+
+    public enum Reason {
+        STARTUP, SHUTDOWN, TIMEOUT, READ_READY, WRITE_READY, PREREQ_DONE
+    }
+
+    static List<TaskIdentifier> pending = new LinkedList<TaskIdentifier>();
+
+    static List<TimeoutTask> pending_timeout = new LinkedList<TimeoutTask>();
+
+    static TaskIdentifier active_task;
+
+    static Priority current_priority;
+    static boolean current_liveness;
+
+    static int ready_count;
+    
+    static TaskIdentifier[] ready = new 
TaskIdentifier[Priority.values().length];
+    
+    static Set<Channel> rs = new TreeSet<Channel>();
+    static Set<Channel> ws = new TreeSet<Channel>();
+    
+    static RelativeTime timeout;
+
+    public static interface Task {
+
+        public void run(Context ctx);
+
+        public static class Context {
+            EnumSet<Reason> reasons;
+            Set<Channel> readableSet;
+            Set<Channel> writeableSet;
+        }
+    }
+
+    public static abstract class TaskIdentifier {
+        private final Task task;
+        private Task.Context ctx = new Task.Context();
+        boolean liveness;
+
+        TaskIdentifier(Task t) {
+            this.task = t;
+        }
+
+        /*
+         * The actual code a task is supposed to execute
+         */
+        void run(Task.Context ctx) {
+            task.run(ctx);
+        }
+
+        public abstract void cancel();
+    }
+
+    public static class Continuation extends TaskIdentifier {
+        Continuation(Task t) {
+            super(t);
+        }
+
+        @Override
+        public void cancel() {
+
+        }
+    }
+
+    static class TimeoutTask extends TaskIdentifier {
+        final AbsoluteTime timeout;
+
+        TimeoutTask(RelativeTime delay, Task t) {
+            super(t);
+            timeout = delay.toAbsolute();
+            // timeout_heap.add (dti.timeout, this);
+        }
+
+        // private final AbsoluteTime timeout;
+
+        public void cancel() {
+            // timeout_heap.remove (this);
+        }
+
+    }
+
+    public static void add(Task task) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Run the task as soon as its prerequisites are satisfied.
+     */
+    public static TaskIdentifier addDelayed(RelativeTime delay, Task task) {
+        return new TimeoutTask(delay, task);
+    }
+
+    /**
+     * Run the task regardless of any prerequisites.
+     */
+    public static void addContinuation(Task task, EnumSet<Reason> reason) {
+
+    }
+
+    /**
+     * Request the shutdown of a scheduler. Marks all currently pending tasks 
as
+     * ready because of shutdown. This will cause all tasks to run (as soon as
+     * possible, respecting priorities and prerequisite tasks). Note that tasks
+     * scheduled AFTER this call may still be delayed arbitrarily.
+     */
+    public static void shutdown() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Get information about the current load of this scheduler. Use this
+     * function to determine if an elective task should be added or simply
+     * dropped (if the decision should be made based on the number of tasks
+     * ready to run).
+     * 
+     * * @param p priority-level to query, use KEEP to query the level of the
+     * current task, use COUNT to get the sum over all priority levels
+     * 
+     * @return number of tasks pending right now
+     */
+    public int getLoad(Priority p) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Obtain the reason code for why the current task was started. Will return
+     * the same value as the GNUNET_SCHEDULER_TaskContext's reason field.
+     * 
+     * * @return reason(s) why the current task is run
+     */
+    public EnumSet<Reason> getReason() {
+        return active_task.ctx.reasons;
+    }
+
+    /**
+     * Cancel execution of the specified task. The task must not yet have run.
+     */
+    public void cancel(TaskIdentifier task) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Initialize and run scheduler. This function will return when all tasks
+     * have completed. On systems with signals, receiving a SIGTERM (and other
+     * similar signals) will cause "GNUNET_SCHEDULER_shutdown" to be run after
+     * the active task is complete. As a result, SIGTERM causes all active 
tasks
+     * to be scheduled with reason "GNUNET_SCHEDULER_REASON_SHUTDOWN". 
(However,
+     * tasks added afterwards will execute normally!). Note that any particular
+     * signal will only shut down one scheduler; applications should always 
only
+     * create a single scheduler.
+     * 
+     * @param task
+     *            task to run immediately
+     */
+    public static void run(Task task) {
+        // XXX: which kind of set to use?
+        // XXX: what about the shutdown pipe / signal handlers?
+        
+        try {
+            Selector s = SelectorProvider.provider().openSelector();
+        } catch (IOException e) {
+            // XXX: what to do?
+            e.printStackTrace();
+        }
+        
+        current_priority = Priority.DEFAULT;
+        current_liveness = true;
+        addContinuation(task, EnumSet.of(Reason.STARTUP));
+        while (checkLiveness()) {
+            rs.clear();
+            ws.clear();
+            timeout = RelativeTime.FOREVER;
+            updateSets();
+            if (ready_count > 0) {
+                timeout = RelativeTime.ZERO;
+            }
+        }
+    }
+
+    private static void updateSets() {
+        
+    }
+
+    /**
+     * Check if the system is still life. Trigger shutdown if we have tasks, 
but
+     * none of them give us lifeness.
+     * 
+     * @return true to continue the main loop, false to exit
+     */
+    private static boolean checkLiveness() {
+        if (ready_count > 0) {
+            return true;
+        }
+
+        for (TaskIdentifier t : pending) {
+            if (t.liveness) {
+                return true;
+            }
+        }
+
+        for (TaskIdentifier t : pending_timeout) {
+            if (t.liveness) {
+                return true;
+            }
+        }
+
+        if (!pending.isEmpty() || pending_timeout.isEmpty()) {
+            shutdown();
+            return true;
+        }
+
+        return false;
+    }
+
+}

Copied: gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java (from rev 
18032, gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java                    
        (rev 0)
+++ gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java    2011-11-07 
20:17:14 UTC (rev 18046)
@@ -0,0 +1,5 @@
+package org.gnunet.util;
+
+public interface TransmitReadyNotify {
+
+}

Deleted: gnunet-java/src/org/gnunet/util/network/Client.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Client.java 2011-11-07 18:43:40 UTC 
(rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/Client.java 2011-11-07 20:17:14 UTC 
(rev 18046)
@@ -1,125 +0,0 @@
-package org.gnunet.util.network;
-
-import org.gnunet.util.Configuration;
-import org.gnunet.util.RelativeTime;
-
-
-
-/**
- * Represents a connection to a service.
- */
-public class Client {
-       interface MessageHandler {
-
-       }
-
-       public static class TransmitHandle {
-               /**
-                * Cancel a request for notification.
-                */
-               void cancel() {
-                       throw new UnsupportedOperationException();
-               }
-
-       }
-
-       /**
-        * Get a connection with a service.
-        * 
-        * @param service_name
-        *            name of the service
-        * @param cfg
-        *            configuration to use
-        */
-       public Client(String service_name, Configuration c) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Read from the service.
-        * 
-        * @param handler
-        *            function to call with the message
-        * @param timeout
-        *            how long to wait until timing out
-        */
-
-       public void receive(MessageHandler handler, RelativeTime timeout) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Ask the client to call us once the specified number of bytes are 
free in
-        * the transmission buffer. May call the notify method immediately if 
enough
-        * space is available.
-        * 
-        * @param sock
-        *            connection to the service
-        * @param size
-        *            number of bytes to send
-        * @param timeout
-        *            after how long should we give up (and call notify with buf
-        *            NULL and size 0)?
-        * @param auto_retry
-        *            if the connection to the service dies, should we 
automatically
-        *            re-connect and retry (within the timeout period) or 
should we
-        *            immediately fail in this case? Pass GNUNET_YES if the 
caller
-        *            does not care about temporary connection errors, for 
example
-        *            because the protocol is stateless
-        * @param notify
-        *            function to call
-        * @param notify_cls
-        *            closure for notify
-        * @return NULL if someone else is already waiting to be notified 
non-NULL
-        *         if the notify callback was queued (can be used to cancel 
using
-        *         GNUNET_CONNECTION_notify_transmit_ready_cancel)
-        */
-       public TransmitHandle notifyTransmitReady(int size, RelativeTime 
timeout,
-                       boolean auto_retry, TransmitReadyNotify cb) {
-               throw new UnsupportedOperationException();
-       }
-
-       
-       /**
-        * Convenience API that combines sending a request
-        * to the service and waiting for a response.
-        * If either operation times out, the callback
-        * will be called with a "NULL" response (in which
-        * case the connection should probably be destroyed).
-        *
-        * @param sock connection to use
-        * @param hdr message to transmit
-        * @param timeout when to give up (for both transmission
-        *         and for waiting for a response)
-        * @param auto_retry if the connection to the service dies, should we
-        *        automatically re-connect and retry (within the timeout period)
-        *        or should we immediately fail in this case?  Pass GNUNET_YES
-        *        if the caller does not care about temporary connection errors,
-        *        for example because the protocol is stateless
-        * @param rn function to call with the response
-        * @param rn_cls closure for rn
-        * @return GNUNET_OK on success, GNUNET_SYSERR if a request
-        *         is already pending
-        */
-       //XXX return type bool vs. exceptions
-       public boolean transmitAndGetResponse(Message m, RelativeTime timeout,
-                       boolean autoRetry, MessageHandler handler) {
-               throw new UnsupportedOperationException();
-       }
-       
-       
-       /**
-        * Wait until the service is running.
-        *
-        * @param service name of the service to wait for
-        * @param cfg configuration to use
-        * @param timeout how long to wait at most in ms
-        * @param task task to run if service is running
-        *        (reason will be "PREREQ_DONE" (service running)
-        *         or "TIMEOUT" (service not known to be running))
-        */
-
-       public static void serviceTest(String service_name, Configuration cfg, 
RelativeTime timeout, Task t) {
-               throw new UnsupportedOperationException();
-       }
-}

Deleted: gnunet-java/src/org/gnunet/util/network/Connection.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Connection.java     2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/Connection.java     2011-11-07 
20:17:14 UTC (rev 18046)
@@ -1,144 +0,0 @@
-package org.gnunet.util.network;
-
-import java.net.*;
-
-import org.gnunet.util.RelativeTime;
-
-public class Connection {
-       public class TransmitHandle {
-               void cancel() {
-                       throw new UnsupportedOperationException();
-               }
-       }
-
-       private boolean persist;
-
-       private Connection(Socket s) {
-       }
-
-       public static Connection fromAccept(ServerSocket ss) {
-               throw new UnsupportedOperationException();
-       }
-
-       public static Connection fromExisting(Socket ss) {
-               return new Connection(ss);
-       }
-
-       public static Connection fromConnect(String hostname, int port) {
-               throw new UnsupportedOperationException();
-       }
-
-       public static Connection fromUnixPath(String path) {
-               // problematic as java has no nativ support for unix domain 
sockets
-               throw new UnsupportedOperationException();
-       }
-
-       public static Connection fromSocketAddress(SocketAddress addr) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Receive data from the given socket. Note that this function will call
-        * "receiver" asynchronously using the scheduler. It will "immediately"
-        * return. Note that there MUST only be one active receive call per 
socket
-        * at any given point in time (so do not call receive again until the
-        * receiver callback has been invoked).
-        * 
-        * @param max
-        *            maximum number of bytes to read
-        * @param timeout
-        *            maximum amount of time to wait
-        * @param receiver
-        *            function to call with received data
-        */
-
-       public void receive(int max, RelativeTime timeout, Receiver r) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Cancel receive job on the given socket. Note that the receiver 
callback
-        * must not have been called yet in order for the cancellation to be 
valid.
-        * 
-        * @return closure of the original receiver callback closure
-        */
-       public void receive_cancel() {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Ask the socket to call us once the specified number of bytes are 
free in
-        * the transmission buffer. May call the notify method immediately if 
enough
-        * space is available. Note that this function will abort if "size" is
-        * greater than GNUNET_SERVER_MAX_MESSAGE_SIZE.
-        * 
-        * Note that "notify" will be called either when enough buffer space is
-        * available OR when the socket is destroyed. The size parameter given 
to
-        * notify is guaranteed to be larger or equal to size if the buffer is
-        * ready, or zero if the socket was destroyed (or at least closed for
-        * writing). Finally, any time before 'notify' is called, a client may 
call
-        * "notify_transmit_ready_cancel" to cancel the transmission request.
-        * 
-        * Only one transmission request can be scheduled at the same time. 
Notify
-        * will be run with the same scheduler priority as that of the caller.
-        * 
-        * @param size
-        *            number of bytes to send
-        * @param timeout
-        *            after how long should we give up (and call notify with buf
-        *            NULL and size 0)?
-        * @param notify
-        *            function to call when buffer space is available
-        * @return non-NULL if the notify callback was queued, NULL if we are
-        *         already going to notify someone else (busy)
-        */
-       public TransmitHandle notifyTransmitReady(int size, RelativeTime 
timeout,
-                       boolean auto_retry, TransmitReadyNotify cb) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Set the persist option on this connection handle. Indicates that the
-        * underlying socket or fd should never really be closed. Used for
-        * indicating process death.
-        */
-       public void persist() {
-               this.persist = true;
-       }
-
-       /**
-        * Disable the "CORK" feature for communication with the given socket,
-        * forcing the OS to immediately flush the buffer on transmission 
instead of
-        * potentially buffering multiple messages. Essentially reduces the OS 
send
-        * buffers to zero. Used to make sure that the last messages sent 
through
-        * the connection reach the other side before the process is terminated.
-        */
-       // XXX: error code / exception
-       public void disableCorking() {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Check if socket is valid (no fatal errors have happened so far). Note
-        * that a socket that is still trying to connect is considered valid.
-        */
-       public boolean check() {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Configure this connection to ignore shutdown signals.
-        * 
-        * @param do_ignore
-        *            GNUNET_YES to ignore, GNUNET_NO to restore default
-        */
-       public void ignoreShutdown(boolean doIgnore) {
-
-       }
-
-       /*
-        * not implemented: GNUNET_CONNECTION_get_address (never used outside
-        * testing)
-        */
-
-}

Deleted: gnunet-java/src/org/gnunet/util/network/Message.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Message.java        2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/Message.java        2011-11-07 
20:17:14 UTC (rev 18046)
@@ -1,29 +0,0 @@
-package org.gnunet.util.network;
-
-public class Message { // FIXME: MessageHeader!
-       
-       // Message (byte[] data, int offset); 
-
-       // public abstract UInt16_t size();// nice for reading. How about 
writing?
-
-       // public abstract UInt16_t type();
-
-       // uint16_t addr_len;
-       
-       // @AddressLength(addr_len);
-       // byte[] address;
-
-       // @ZeroTerminatedString
-       // byte[] zero_terminated_string; // where does complex parsing logic 
live?
-       
-       // int32 foo; // read+write
-       
-       public int type;
-       
-       // size is implicit in contents. does this cover all use cases?
-       public byte[] contents;
-       
-
-       
-       // msg = MessageFactory.parse (data, off, MessageHeader.class);
-}

Deleted: gnunet-java/src/org/gnunet/util/network/MessageHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/MessageHandler.java 2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/MessageHandler.java 2011-11-07 
20:17:14 UTC (rev 18046)
@@ -1,5 +0,0 @@
-package org.gnunet.util.network;
-
-public class MessageHandler {
-
-}

Deleted: gnunet-java/src/org/gnunet/util/network/Receiver.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Receiver.java       2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/Receiver.java       2011-11-07 
20:17:14 UTC (rev 18046)
@@ -1,6 +0,0 @@
-package org.gnunet.util.network;
-
-public interface Receiver {
-       public void receive(byte[] buf, long available, Object sockaddr, int 
addrlen, int errCode);
-       
-}

Deleted: gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java    
2011-11-07 18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java    
2011-11-07 20:17:14 UTC (rev 18046)
@@ -1,5 +0,0 @@
-package org.gnunet.util.network;
-
-public interface TransmitReadyNotify {
-
-}

Deleted: gnunet-java/src/org/gnunet/util/scheduler/Scheduler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/scheduler/Scheduler.java    2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/src/org/gnunet/util/scheduler/Scheduler.java    2011-11-07 
20:17:14 UTC (rev 18046)
@@ -1,186 +0,0 @@
-// FIXME: license
-
-package org.gnunet.util.scheduler;
-
-import org.gnunet.util.AbsoluteTime;
-import org.gnunet.util.RelativeTime;
-
-/**
- * 
- * @author 
- */
-public class Scheduler {
-
-       public enum Priority {
-               KEEP, IDLE, BACKGROUND, DEFAULT, HIGH, UI, URGENT, SHUTDOWN
-       };
-
-       public enum Reason {
-               // XXX: do these values need to be 2^n? are they used in a bit 
mask? YES => what about EnumSet?
-               STARTUP, SHUTDOWN, TIMEOUT, READ_READY, WRITE_READY, PREREQ_DONE
-       };
-
-       
-       
-       public interface Task {
-               
-               public void run (Context ctx);
-               
-               public static class Context
-               {
-                       Reason reason;
-                       
-                       // read-ready set
-                       // write-ready set
-               };
-               
-       }
-
-       public static abstract class TaskIdentifier {
-
-               private final Task task;
-               
-               TaskIdentifier (Task t)
-               {
-                       this.task = t;
-               }
-               
-               /*
-                * // Unique Task Identifier, XXX do we/ need this? int id;
-                * 
-                * int prereq_id;
-                * 
-                * AbsoluteTime timeout;
-                * 
-                * AbsoluteTime start_time;
-                * 
-                * int read_fd, write_fd;
-                * 
-                * int lifeness;
-                */
-
-               /*
-                * The actual code a task is supposed to execute
-                */
-               void run(Task.Context ctx)
-               {
-                       task.run(ctx);
-               }
-               
-               public abstract void cancel ();
-       }
-       
-       static class TimeoutTask extends TaskIdentifier {
-
-               final AbsoluteTime timeout;
-               
-               TimeoutTask (RelativeTime delay, Task t)
-               {                       
-                       super (t);
-                       timeout = delay.toAbsolute();
-                       // timeout_heap.add (dti.timeout, this);
-               }
-               // private final AbsoluteTime timeout;
-
-               public void cancel () { 
-                       // timeout_heap.remove (this);
-               }
-                               
-       }
-       
-       /**
-        * Run the task as soon as its prerequisites are satisfied.
-        */
-       public void add(Task task) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Run the task as soon as its prerequisites are satisfied.
-        */
-       public TaskIdentifier add_delayed(RelativeTime delay, Task task) {
-               return new TimeoutTask (delay, task); 
-       }
-
-       
-
-       /**
-        * Initialize and run scheduler. This function will return when all 
tasks
-        * have completed. On systems with signals, receiving a SIGTERM (and 
other
-        * similar signals) will cause "GNUNET_SCHEDULER_shutdown" to be run 
after
-        * the active task is complete. As a result, SIGTERM causes all active 
tasks
-        * to be scheduled with reason "GNUNET_SCHEDULER_REASON_SHUTDOWN". 
(However,
-        * tasks added afterwards will execute normally!). Note that any 
particular
-        * signal will only shut down one scheduler; applications should always 
only
-        * create a single scheduler.
-        * 
-        * @param task
-        *            task to run first (and immediately)
-        */
-       public void run(Task task, ScheduleSpec spec) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * run with default spec (=> as soon as possible)
-        */
-       public void run(Task task) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Run the task as soon as its prerequisites are satisfied.
-        */
-       public void schedule(Task task) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Run the task regardless of any prerequisites.
-        */
-       public void setNext(Task task, Reason reason) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Request the shutdown of a scheduler. Marks all currently pending 
tasks as
-        * ready because of shutdown. This will cause all tasks to run (as soon 
as
-        * possible, respecting priorities and prerequisite tasks). Note that 
tasks
-        * scheduled AFTER this call may still be delayed arbitrarily.
-        */
-       public void shutdown() {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Get information about the current load of this scheduler. Use this
-        * function to determine if an elective task should be added or simply
-        * dropped (if the decision should be made based on the number of tasks
-        * ready to run).
-        * 
-        * * @param p priority-level to query, use KEEP to query the level of 
the
-        * current task, use COUNT to get the sum over all priority levels
-        * 
-        * @return number of tasks pending right now
-        */
-       public int getLoad(Priority p) {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Obtain the reason code for why the current task was started. Will 
return
-        * the same value as the GNUNET_SCHEDULER_TaskContext's reason field.
-        * 
-        * * @return reason(s) why the current task is run
-        */
-       public Reason getReason() {
-               throw new UnsupportedOperationException();
-       }
-
-       /**
-        * Cancel execution of the specified task. The task must not yet have 
run.
-        */
-       public void cancel(Task task) {
-               throw new UnsupportedOperationException();
-       }
-}

Modified: gnunet-java/test/org/gnunet/util/resources/gnunet.config-1
===================================================================
--- gnunet-java/test/org/gnunet/util/resources/gnunet.config-1  2011-11-07 
18:43:40 UTC (rev 18045)
+++ gnunet-java/test/org/gnunet/util/resources/gnunet.config-1  2011-11-07 
20:17:14 UTC (rev 18046)
@@ -4,6 +4,6 @@
 # foo
    # foo
 bar = baz
-bla = xd
+bla = "x  d" 
 [bla]
 spam =
\ No newline at end of file




reply via email to

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