[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r18084 - in gnunet-java: . src/org/gnunet/construct src/org
From: |
gnunet |
Subject: |
[GNUnet-SVN] r18084 - in gnunet-java: . src/org/gnunet/construct src/org/gnunet/util |
Date: |
Wed, 9 Nov 2011 23:12:05 +0100 |
Author: dold
Date: 2011-11-09 23:12:05 +0100 (Wed, 09 Nov 2011)
New Revision: 18084
Removed:
gnunet-java/src/org/gnunet/util/Message.java
Modified:
gnunet-java/ISSUES
gnunet-java/src/org/gnunet/construct/Construct.java
gnunet-java/src/org/gnunet/util/AbsoluteTime.java
gnunet-java/src/org/gnunet/util/Client.java
gnunet-java/src/org/gnunet/util/Configuration.java
gnunet-java/src/org/gnunet/util/Connection.java
gnunet-java/src/org/gnunet/util/MessageHandler.java
gnunet-java/src/org/gnunet/util/Receiver.java
gnunet-java/src/org/gnunet/util/RelativeTime.java
gnunet-java/src/org/gnunet/util/Scheduler.java
gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java
Log:
a lot of changes and additions
Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES 2011-11-09 20:45:46 UTC (rev 18083)
+++ gnunet-java/ISSUES 2011-11-09 22:12:05 UTC (rev 18084)
@@ -31,9 +31,14 @@
* generate parsers from java files using annotations
* is java flexible enough for this?
* cannot generate interfaces / add new members
+
+* error handling: what to do with io errors in some cases?
+* process priority
+
+
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/construct/Construct.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -17,7 +17,6 @@
* @TODO performance evaluation
*/
public class Construct {
-
/**
* Given a byte array with a message, parse it into an object of type
c. The
* fields of the class are expected to be annotated with annotations
from
Modified: gnunet-java/src/org/gnunet/util/AbsoluteTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/AbsoluteTime.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/AbsoluteTime.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,3 +1,23 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
import org.slf4j.Logger;
@@ -3,4 +23,10 @@
import org.slf4j.LoggerFactory;
+
+/**
+ * A specific point in time.
+ *
+ * @author Florian Dold
+ */
public class AbsoluteTime {
private static final Logger logger = LoggerFactory
@@ -15,7 +41,7 @@
private final long abs_value;
/**
- * Set the timestamp offset for this instance.
+ * Sets the timestamp offset for this instance.
*
* @param offset
* the offset to skew the locale time by
@@ -25,8 +51,7 @@
}
/**
- * Get the current time (works just as "time", just that we use the unit of
- * time that the cron-jobs use (and is 64 bit)).
+ * Gets the current time.
*
* @return the current time
*/
@@ -37,7 +62,13 @@
public AbsoluteTime(final long abs_value) {
this.abs_value = abs_value;
}
+
+ /**
+ * Returns the milliseconds since some fixed point of reference.
+ *
+ * @return the absolute time in milliseconds
+ */
public long getMilliseconds() {
return abs_value;
}
@@ -64,23 +95,7 @@
}
/**
- * Calculate the remaining time
- * @return
- */
- public RelativeTime getRemaining() {
- if (abs_value == Long.MAX_VALUE) {
- return RelativeTime.FOREVER;
- }
- final AbsoluteTime now = AbsoluteTime.now();
- if (now.abs_value > abs_value) {
- return RelativeTime.ZERO;
- }
- return new RelativeTime(abs_value - now.abs_value);
-
- }
-
- /**
- * Calculate the estimate time of arrival/completion for an operation.
+ * Calculates the estimate time of arrival/completion for an operation.
*
* @param start
* when did the operation start?
@@ -91,8 +106,8 @@
* @return remaining duration for the operation, assuming it continues at
* the same speed
*/
- public RelativeTime calculateETA(final AbsoluteTime start, final long
finished,
- final long total) {
+ public RelativeTime calculateETA(final AbsoluteTime start,
+ final long finished, final long total) {
if (finished >= total) {
return RelativeTime.ZERO;
}
@@ -107,8 +122,9 @@
/**
* Calculates the difference between two absolute times.
+ *
* @param other
- * @return
+ * @return this - other
*/
public RelativeTime getDifference(final AbsoluteTime other) {
if (other.abs_value == Long.MAX_VALUE) {
@@ -120,11 +136,32 @@
return new RelativeTime(abs_value - other.abs_value);
}
+ /**
+ * Gets the duration of an operation as the difference of the current time
+ * and address@hidden this}.
+ *
+ * @return start_time(=this) - now
+ */
public RelativeTime getDuration() {
assert abs_value != Long.MAX_VALUE;
return getDifference(AbsoluteTime.now());
}
+ /**
+ * Calculates the remaining time relative to now.
+ *
+ * @return future - now
+ */
+ public RelativeTime getRemaining() {
+ return getDifference(AbsoluteTime.now());
+ }
+
+ /**
+ * Adds a relative time value to an absolute time.
+ *
+ * @param duration
+ * @return this + duration
+ */
public AbsoluteTime add(final RelativeTime duration) {
if (abs_value == Long.MAX_VALUE
|| duration.getMilliseconds() == Long.MAX_VALUE) {
@@ -137,6 +174,12 @@
return new AbsoluteTime(abs_value + duration.getMilliseconds());
}
+ /**
+ * Subtracts a relative time value to an absolute time
+ *
+ * @param duration
+ * @return this - duration
+ */
public AbsoluteTime subtract(final RelativeTime duration) {
if (abs_value <= duration.getMilliseconds()) {
return AbsoluteTime.ZERO;
Modified: gnunet-java/src/org/gnunet/util/Client.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Client.java 2011-11-09 20:45:46 UTC (rev
18083)
+++ gnunet-java/src/org/gnunet/util/Client.java 2011-11-09 22:12:05 UTC (rev
18084)
@@ -1,6 +1,30 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
+import org.gnunet.messages.MessageHeader;
+import org.gnunet.util.Scheduler.Task;
+
/**
* Represents a connection to a service.
*/
@@ -98,7 +122,7 @@
* is already pending
*/
//XXX return type bool vs. exceptions
- public boolean transmitAndGetResponse(Message m, RelativeTime timeout,
+ public boolean transmitAndGetResponse(MessageHeader m, RelativeTime
timeout,
boolean autoRetry, MessageHandler handler) {
throw new UnsupportedOperationException();
}
Modified: gnunet-java/src/org/gnunet/util/Configuration.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Configuration.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/Configuration.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,3 +1,24 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
import java.io.BufferedWriter;
@@ -5,17 +26,20 @@
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
+import java.util.Set;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Configuration management
+ * Configuration management.
*
* @author Florian Dold
*/
@@ -55,7 +79,6 @@
sc.next(comment);
} else if (sc.hasNext(section)) {
sc.next(section);
- System.out.println(sc.match().groupCount());
current_section = sc.match().group(1).trim();
} else if (sc.hasNext(tag)) {
sc.next(tag);
@@ -70,7 +93,6 @@
}
value = value.substring(1, pos);
}
-
setValueString(current_section, option, value);
} else if (!sc.hasNext()) {
break;
@@ -138,6 +160,13 @@
throw new UnsupportedOperationException();
}
+ /**
+ * Set an option to a string value in a section.
+ *
+ * @param section
+ * @param option
+ * @param value
+ */
public void setValueString(String section, String option, String value) {
Map<String, String> table = sections.get(section);
if (table == null) {
@@ -147,53 +176,178 @@
table.put(option, value);
}
+ /**
+ * Set an option to a string value in a section.
+ *
+ * @param section
+ * @param option
+ * @param value
+ */
public void setValueNumber(String section, String option, long value) {
setValueString(section, option, "" + value);
}
- public long getValueNumer(String section, String option) {
- throw new UnsupportedOperationException();
+ /**
+ * Get a configuration value that should be a number
+ *
+ * @param section
+ * @param option
+ * @return null if value not in configuration, the option's value otherwise
+ */
+ public Long getValueNumer(String section, String option) {
+ String num_str = getValueString(section, option);
+ if (num_str == null) {
+ return null;
+ }
+ try {
+ return Long.parseLong(num_str);
+ } catch (NumberFormatException e) {
+ return null;
+ }
}
+ /**
+ * Set an option to a string value in a section.
+ *
+ * @param section
+ * @param option
+ * @param value
+ * null if value not found
+ */
public String getValueString(String section, String option) {
- throw new UnsupportedOperationException();
+ if (!sections.containsKey(section)) {
+ return null;
+ }
+ return sections.get(section).get(option);
}
+ /**
+ * XXX: how is the time stored?
+ *
+ * @param section
+ * @param option
+ * @return null if option not found
+ */
public RelativeTime getValueTime(String section, String option) {
throw new UnsupportedOperationException();
}
+ /**
+ * Get a configuration value that should be in a set of predefined strings
+ *
+ * @param section
+ * section of interest
+ * @param option
+ * option of interest
+ * @param choices
+ * list of legal values
+ * @return matching value from choices, null if nothing matches
+ */
public String getValueChoice(String section, String option,
- List<String> choices) {
- throw new UnsupportedOperationException();
+ Iterable<String> choices) {
+ String value = getValueString(section, option);
+ if (value == null) {
+ return null;
+ }
+ for (String c : choices) {
+ if (c.equals(value)) {
+ return value;
+ }
+ }
+ return null;
}
+ /**
+ * Tests if we have a value for a particular option.
+ *
+ * @param section
+ * section of interest
+ * @param option
+ * option of interest
+ * @return true if so, false of not
+ *
+ */
+ // XXX: should this be named "hasValue"?
public boolean haveValue(String section, String option) {
return sections.containsValue(section)
&& sections.get(section).containsKey(option);
}
/**
+ * Gets a configuration value that should be in a set of {"YES","NO"}.
+ *
+ * @param section
+ * section of interest
+ * @param option
+ * option of interest
+ * @return true, false, null
+ */
+ public Boolean getValueYesNo(String section, String option) {
+ String v = getValueChoice(section, option, Arrays.asList("YES", "NO"));
+ if (v == null) {
+ return null;
+ }
+ if (v.equals("YES")) {
+ return true;
+ }
+ if (v.equals("NO")) {
+ return false;
+ }
+ return null;
+ }
+
+ /**
+ * Returns all configuration options in a section.
+ *
+ * @param s
+ * the section of interest
+ * @return an unmodifiable view of the section.
+ */
+ public Map<String, String> getSection(String s) {
+ Map<String, String> m = sections.get(s);
+ if (m == null) {
+ return null;
+ }
+ return Collections.unmodifiableMap(m);
+ }
+
+ /**
+ * Returns the names of all non-empty sections
+ *
+ * @return set of non-empty section names
+ */
+ public Set<String> getSections() {
+ return sections.keySet();
+ }
+
+ /**
* Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where
- * either in the "PATHS" section or the environtment "FOO" is set to
+ * either in the "PATHS" section or the environment "FOO" is set to
* "DIRECTORY".
*
* @param orig
* string to $-expand
* @return $-expanded string
*/
+ // XXX: this is not really platform independent?
public String expandDollar(String orig) {
- throw new UnsupportedOperationException();
+ Map<String, String> env = System.getenv();
+ for (Map.Entry<String, String> e : env.entrySet()) {
+ orig = orig.replace("$" + e.getKey(), e.getValue());
+ }
+
+ if (sections.containsKey("PATHS")) {
+ for (Map.Entry<String, String> e :
sections.get("PATHS").entrySet()) {
+ orig = orig.replace("$" + e.getKey(), e.getValue());
+ }
+ }
+ return orig;
}
public String getValueFileName(String section, String option) {
throw new UnsupportedOperationException();
}
- public boolean getValueYesNo(String section, String option) {
- throw new UnsupportedOperationException();
- }
-
public List<String> getValueFilenames(String section, String option) {
throw new UnsupportedOperationException();
}
Modified: gnunet-java/src/org/gnunet/util/Connection.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Connection.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/Connection.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,8 +1,43 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
-import java.net.*;
+import java.io.IOException;
+import java.net.Inet4Address;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.nio.channels.Channel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+/**
+ * A connection sends data over a channel asynchronously using the scheduler.
+ *
+ * @author Florian Dold
+ *
+ */
public class Connection {
public class TransmitHandle {
void cancel() {
@@ -11,20 +46,25 @@
}
private boolean persist;
+
+ private Channel chan;
- private Connection(Socket s) {
+ private Connection(Channel c) {
+ chan = c;
}
public static Connection fromAccept(ServerSocket ss) {
throw new UnsupportedOperationException();
}
- public static Connection fromExisting(Socket ss) {
- return new Connection(ss);
+ public static Connection fromExisting(Socket ss) throws IOException {
+ SocketChannel sc = ss.getChannel();
+ sc.configureBlocking(false);
+ return new Connection(sc);
}
- public static Connection fromConnect(String hostname, int port) {
- throw new UnsupportedOperationException();
+ public static Connection fromConnect(String hostname, int port) throws
IOException {
+ throw new UnsupportedOperationException();
}
public static Connection fromUnixPath(String path) {
Deleted: gnunet-java/src/org/gnunet/util/Message.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Message.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/Message.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,30 +0,0 @@
-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);
-}
Modified: gnunet-java/src/org/gnunet/util/MessageHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/MessageHandler.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/MessageHandler.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,5 +1,40 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
-public class MessageHandler {
+import org.gnunet.messages.MessageHeader;
+
+/**
+ *
+ * @author Florian Dold
+ *
+ */
+public interface MessageHandler {
+ /**
+ * Method to call when we receive a message
+ * from the service.
+ *
+ * @param msg message received, NULL on timeout or fatal error
+ */
+ public void handle(MessageHeader msg);
}
Modified: gnunet-java/src/org/gnunet/util/Receiver.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Receiver.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/Receiver.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,6 +1,38 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
+import java.net.SocketAddress;
+
+
public interface Receiver {
- public void receive(byte[] buf, long available, Object sockaddr, int
addrlen, int errCode);
+ /**
+ * Callback function for data received from the network.
+ *
+ * @param buf received data
+ * @param sockaddr address of the sender
+ * @param errCode XXX errno? really?! should this be an IO exception?
+ */
+ public void receive(byte[] buf, SocketAddress sockaddr, 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-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/RelativeTime.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,3 +1,24 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
import org.slf4j.Logger;
@@ -4,6 +25,8 @@
import org.slf4j.LoggerFactory;
/**
+ * Positive amount of time with no point of reference.
+ *
* @author Florian Dold
*/
public final class RelativeTime {
@@ -28,15 +51,20 @@
this.rel_value = abs_value;
}
+ /**
+ * Returns the amount of time in milliseconds.
+ *
+ * @return the amount of time in milliseconds
+ */
public long getMilliseconds() {
return rel_value;
}
/**
- * Convert relative time to an absolute time in the future.
+ * Converts relative time to an absolute time in the future.
*
- * @return timestamp that is "rel" in the future, or FOREVER if
rel==FOREVER
- * (or if we would overflow)
+ * @return timestamp that is in the future, or FOREVER if this=FOREVER (or
+ * if we would overflow)
*/
public AbsoluteTime toAbsolute() {
return AbsoluteTime.now().add(this);
@@ -45,7 +73,7 @@
/**
* Return the minimum of two relative time values.
*
- * @return the smaller time value
+ * @return min(this, other)
*/
public RelativeTime min(final RelativeTime other) {
return (rel_value <= other.rel_value) ? this : other;
@@ -54,7 +82,7 @@
/**
* Return the maximum of two relative time values.
*
- * @return the bigger time value
+ * @return max(this, other)
*/
public RelativeTime max(final RelativeTime other) {
return (rel_value >= other.rel_value) ? this : other;
@@ -63,13 +91,14 @@
/**
* Multiply relative time by a given factor.
*
- * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
+ * @return FOREVER if this=FOREVER or on overflow; otherwise this*factor
*/
public RelativeTime multiply(final int factor) {
if (factor == 0) {
return RelativeTime.ZERO;
}
final long ret = this.rel_value * factor;
+ // check for numeric overflow
if (ret / factor != rel_value) {
logger.warn("time overflow");
return RelativeTime.FOREVER;
@@ -84,7 +113,7 @@
* some duration
* @param factor
* integer to divide by
- * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
+ * @return FOREVER if this=FOREVER or factor=0; otherwise this/factor
*/
public RelativeTime divide(final int factor) {
if (factor == 0 || this.rel_value == Long.MAX_VALUE) {
@@ -95,6 +124,11 @@
/**
* Add relative times together.
+ *
+ * @param other
+ * the other timestamp
+ *
+ * @return this + other
*/
public RelativeTime add(final RelativeTime other) {
if (this.rel_value == Long.MAX_VALUE
@@ -115,8 +149,8 @@
*
* @param other
* second timestamp
- * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is
- * FOREVER, a1-a2 otherwise
+ * @return ZERO if other>=this (including both FOREVER), FOREVER if
+ * this=FOREVER, this-other otherwise
*/
public RelativeTime subtract(final RelativeTime other) {
if (this.rel_value >= other.rel_value) {
Modified: gnunet-java/src/org/gnunet/util/Scheduler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Scheduler.java 2011-11-09 20:45:46 UTC
(rev 18083)
+++ gnunet-java/src/org/gnunet/util/Scheduler.java 2011-11-09 22:12:05 UTC
(rev 18084)
@@ -1,11 +1,30 @@
-// FIXME: license
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
import java.io.IOException;
import java.nio.channels.Channel;
+import java.nio.channels.SelectableChannel;
import java.nio.channels.Selector;
-import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.EnumSet;
import java.util.LinkedList;
@@ -48,7 +67,6 @@
static RelativeTime timeout;
public static interface Task {
-
public void run(Context ctx);
public static class Context {
@@ -57,6 +75,12 @@
Set<Channel> writeableSet;
}
}
+
+ final public static Task NO_TASK = new Task() {
+ @Override
+ public void run(Context ctx) {
+ }
+ };
public static abstract class TaskIdentifier {
private final Task task;
@@ -102,10 +126,35 @@
public void cancel() {
// timeout_heap.remove (this);
}
-
}
-
- public static void add(Task task) {
+
+
+ /*
+ static class SelectTask extends TaskIdentifier {
+
+ }
+
+ static class PrereqTask extends TaskIdentifier {
+
+ }
+ */
+
+
+
+
+
+
+
+ /**
+ * Schedule a new task to be run as soon as possible. The task
+ * will be run with the priority of the calling task.
+ *
+ * @param task main function of the task
+ * @param task_cls closure of task
+ * @return unique task identifier for the job
+ * only valid until "task" is started!
+ */
+ public static void addNow(Task task) {
throw new UnsupportedOperationException();
}
@@ -122,6 +171,36 @@
public static void addContinuation(Task task, EnumSet<Reason> reason) {
}
+
+
+
+ public static TaskIdentifier addAfter(TaskIdentifier prereq, Task t) {
+ return addSelect(Priority.KEEP, prereq, RelativeTime.ZERO, null, null,
t);
+ }
+
+ public static TaskIdentifier addWithPriority(Priority prio, Task t) {
+ //return addSelect(prio, NO_TASK, RelativeTime.ZERO, null, null, t);
+ throw new UnsupportedOperationException();
+ }
+
+
+
+
+ // should register the channels with the selector
+ public static TaskIdentifier addSelect(Priority p, TaskIdentifier prereq,
+ RelativeTime delay, Set<SelectableChannel> rs,
Set<SelectableChannel> ws, Task t) {
+ return null;
+ }
+
+
+
+
+
+
+
+
+
+
/**
* Request the shutdown of a scheduler. Marks all currently pending tasks
as
@@ -177,33 +256,70 @@
*
* @param task
* task to run immediately
+ * @throws IOException
*/
- public static void run(Task task) {
+ public static void run(Task task) throws IOException {
// XXX: which kind of set to use?
// XXX: what about the shutdown pipe / signal handlers?
-
+ Selector sel;
try {
- Selector s = SelectorProvider.provider().openSelector();
+ sel = SelectorProvider.provider().openSelector();
} catch (IOException e) {
// XXX: what to do?
e.printStackTrace();
+ return;
}
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;
}
+
+ sel.select(timeout.getMilliseconds());
+
+ if (timeout.getMilliseconds() == 0) {
+ try {
+ Thread.sleep(1);
+ } catch (InterruptedException e) {
+ ;
+ }
+ }
+
+ checkReady();
+ runReady();
}
}
+ private static void runReady() {
+ do {
+ if (ready_count == 0) {
+ return;
+ }
+ } while (false /*....todo*/);
+
+
+ }
+
+ private static void checkReady() {
+ AbsoluteTime now = AbsoluteTime.now();
+ // ...
+
+ }
+
private static void updateSets() {
+ // update sets does not do the same thing as the C code.
}
Modified: gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java
===================================================================
--- gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java 2011-11-09
20:45:46 UTC (rev 18083)
+++ gnunet-java/src/org/gnunet/util/TransmitReadyNotify.java 2011-11-09
22:12:05 UTC (rev 18084)
@@ -1,5 +1,38 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009 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 2, 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.util;
+import java.nio.Buffer;
+
public interface TransmitReadyNotify {
+ /**
+ * Called when a socket is ready to send more date.
+ *
+ * @param buf
+ * @return
+ */
+ // (buffer is initially at mark, data up to limit can be send at once,
+ // capacity may not be exceeded
+ public void transmit(Buffer buf);
+
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r18084 - in gnunet-java: . src/org/gnunet/construct src/org/gnunet/util,
gnunet <=