[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r17937 - in gnunet-java: . src/org/gnunet/service src/org/g
From: |
gnunet |
Subject: |
[GNUnet-SVN] r17937 - in gnunet-java: . src/org/gnunet/service src/org/gnunet/util src/org/gnunet/util/client src/org/gnunet/util/connection src/org/gnunet/util/datastructure src/org/gnunet/util/network |
Date: |
Wed, 2 Nov 2011 17:48:06 +0100 |
Author: dold
Date: 2011-11-02 17:48:06 +0100 (Wed, 02 Nov 2011)
New Revision: 17937
Added:
gnunet-java/src/org/gnunet/util/datastructure/
gnunet-java/src/org/gnunet/util/datastructure/BloomFilter.java
gnunet-java/src/org/gnunet/util/network/
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
Removed:
gnunet-java/src/org/gnunet/util/client/Client.java
gnunet-java/src/org/gnunet/util/client/MessageHandler.java
gnunet-java/src/org/gnunet/util/connection/Connection.java
gnunet-java/src/org/gnunet/util/connection/Receiver.java
gnunet-java/src/org/gnunet/util/connection/TransmitReadyNotify.java
gnunet-java/src/org/gnunet/util/network/Connection.java
Modified:
gnunet-java/ISSUES
gnunet-java/src/org/gnunet/service/StatisticsService.java
gnunet-java/src/org/gnunet/util/network/Receiver.java
gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java
Log:
structural changes
Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES 2011-11-02 16:08:48 UTC (rev 17936)
+++ gnunet-java/ISSUES 2011-11-02 16:48:06 UTC (rev 17937)
@@ -1,3 +1,4 @@
+* what about licensing of libraries in general?
* java has no native unix domain sockets (=>use JNI library, licensing issues?)
* package structure, inner classes vs. packages
* naming conventions: stick with java standards?
@@ -4,4 +5,10 @@
* ugly cross-reference to TransmitReadyNotify.java in
org.gnunet.util.client.Client
* Client/Connection class hierarchy
* how to handle disconnect/destroy (as java cannot directly dispose of objects)
-*
\ No newline at end of file
+* why is configuration passed around so often?
+
+
+
+Used Libraries:
+ * https://github.com/magnuss/java-bloomfilter (LGPL)
+ * http://code.google.com/p/junixsocket/ (Apache License 2.0)
\ No newline at end of file
Modified: gnunet-java/src/org/gnunet/service/StatisticsService.java
===================================================================
--- gnunet-java/src/org/gnunet/service/StatisticsService.java 2011-11-02
16:08:48 UTC (rev 17936)
+++ gnunet-java/src/org/gnunet/service/StatisticsService.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -5,8 +5,8 @@
import java.util.LinkedList;
-import org.gnunet.util.client.Client;
import org.gnunet.util.config.Configuration;
+import org.gnunet.util.network.Client;
import org.gnunet.util.time.AbsoluteTime;
import org.gnunet.util.time.RelativeTime;
Deleted: gnunet-java/src/org/gnunet/util/client/Client.java
===================================================================
--- gnunet-java/src/org/gnunet/util/client/Client.java 2011-11-02 16:08:48 UTC
(rev 17936)
+++ gnunet-java/src/org/gnunet/util/client/Client.java 2011-11-02 16:48:06 UTC
(rev 17937)
@@ -1,107 +0,0 @@
-package org.gnunet.util.client;
-
-import org.gnunet.util.config.Configuration;
-import org.gnunet.util.connection.TransmitReadyNotify;
-import org.gnunet.util.time.RelativeTime;
-
-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: should rettype really be bool?
- // XXX: how to represent the message? does the original function really
only
- // get the header?
- public boolean transmitAndGetResponse(Message m, RelativeTime timeout,
- boolean autoRetry, MessageHandler handler) {
- throw new UnsupportedOperationException();
- }
-}
Deleted: gnunet-java/src/org/gnunet/util/client/MessageHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/client/MessageHandler.java 2011-11-02
16:08:48 UTC (rev 17936)
+++ gnunet-java/src/org/gnunet/util/client/MessageHandler.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -1,5 +0,0 @@
-package org.gnunet.util.client;
-
-public class MessageHandler {
-
-}
Deleted: gnunet-java/src/org/gnunet/util/connection/Connection.java
===================================================================
--- gnunet-java/src/org/gnunet/util/connection/Connection.java 2011-11-02
16:08:48 UTC (rev 17936)
+++ gnunet-java/src/org/gnunet/util/connection/Connection.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -1,146 +0,0 @@
-package org.gnunet.util.connection;
-
-import java.net.*;
-
-import org.gnunet.util.time.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, long 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
- * @param notify_cls
- * closure for notify
- * @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/connection/Receiver.java
===================================================================
--- gnunet-java/src/org/gnunet/util/connection/Receiver.java 2011-11-02
16:08:48 UTC (rev 17936)
+++ gnunet-java/src/org/gnunet/util/connection/Receiver.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -1,6 +0,0 @@
-package org.gnunet.util.connection;
-
-public interface Receiver {
- public void receive(byte[] buf, long available, Object sockaddr, int
addrlen, int errCode);
-
-}
Deleted: gnunet-java/src/org/gnunet/util/connection/TransmitReadyNotify.java
===================================================================
--- gnunet-java/src/org/gnunet/util/connection/TransmitReadyNotify.java
2011-11-02 16:08:48 UTC (rev 17936)
+++ gnunet-java/src/org/gnunet/util/connection/TransmitReadyNotify.java
2011-11-02 16:48:06 UTC (rev 17937)
@@ -1,5 +0,0 @@
-package org.gnunet.util.connection;
-
-public interface TransmitReadyNotify {
-
-}
Added: gnunet-java/src/org/gnunet/util/datastructure/BloomFilter.java
===================================================================
--- gnunet-java/src/org/gnunet/util/datastructure/BloomFilter.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/datastructure/BloomFilter.java
2011-11-02 16:48:06 UTC (rev 17937)
@@ -0,0 +1,441 @@
+package org.gnunet.util.datastructure;
+
+/**
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import java.io.Serializable;
+import java.nio.charset.Charset;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.BitSet;
+import java.util.Collection;
+
+/**
+ * Implementation of a Bloom-filter, as described here:
+ * http://en.wikipedia.org/wiki/Bloom_filter
+ *
+ * For updates and bugfixes, see http://github.com/magnuss/java-bloomfilter
+ *
+ * Inspired by the SimpleBloomFilter-class written by Ian Clarke. This
+ * implementation provides a more evenly distributed Hash-function by
+ * using a proper digest instead of the Java RNG. Many of the changes
+ * were proposed in comments in his blog:
+ *
http://blog.locut.us/2008/01/12/a-decent-stand-alone-java-bloom-filter-implementation/
+ *
+ * @param <E> Object type that is to be inserted into the Bloom filter, e.g.
String or Integer.
+ * @author Magnus Skjegstad <address@hidden>
+ */
+public class BloomFilter<E> implements Serializable {
+ private BitSet bitset;
+ private int bitSetSize;
+ private double bitsPerElement;
+ private int expectedNumberOfFilterElements; // expected (maximum) number
of elements to be added
+ private int numberOfAddedElements; // number of elements actually added to
the Bloom filter
+ private int k; // number of hash functions
+
+ static final Charset charset = Charset.forName("UTF-8"); // encoding used
for storing hash values as strings
+
+ static final String hashName = "MD5"; // MD5 gives good enough accuracy in
most circumstances. Change to SHA1 if it's needed
+ static final MessageDigest digestFunction;
+ static { // The digest method is reused between instances
+ MessageDigest tmp;
+ try {
+ tmp = java.security.MessageDigest.getInstance(hashName);
+ } catch (NoSuchAlgorithmException e) {
+ tmp = null;
+ }
+ digestFunction = tmp;
+ }
+
+ /**
+ * Constructs an empty Bloom filter. The total length of the Bloom filter
will be
+ * c*n.
+ *
+ * @param c is the number of bits used per element.
+ * @param n is the expected number of elements the filter will contain.
+ * @param k is the number of hash functions used.
+ */
+ public BloomFilter(double c, int n, int k) {
+ this.expectedNumberOfFilterElements = n;
+ this.k = k;
+ this.bitsPerElement = c;
+ this.bitSetSize = (int)Math.ceil(c * n);
+ numberOfAddedElements = 0;
+ this.bitset = new BitSet(bitSetSize);
+ }
+
+ /**
+ * Constructs an empty Bloom filter. The optimal number of hash functions
(k) is estimated from the total size of the Bloom
+ * and the number of expected elements.
+ *
+ * @param bitSetSize defines how many bits should be used in total for the
filter.
+ * @param expectedNumberOElements defines the maximum number of elements
the filter is expected to contain.
+ */
+ public BloomFilter(int bitSetSize, int expectedNumberOElements) {
+ this(bitSetSize / (double)expectedNumberOElements,
+ expectedNumberOElements,
+ (int) Math.round((bitSetSize / (double)expectedNumberOElements) *
Math.log(2.0)));
+ }
+
+ /**
+ * Constructs an empty Bloom filter with a given false positive
probability. The number of bits per
+ * element and the number of hash functions is estimated
+ * to match the false positive probability.
+ *
+ * @param falsePositiveProbability is the desired false positive
probability.
+ * @param expectedNumberOfElements is the expected number of elements in
the Bloom filter.
+ */
+ public BloomFilter(double falsePositiveProbability, int
expectedNumberOfElements) {
+ this(Math.ceil(-(Math.log(falsePositiveProbability) / Math.log(2))) /
Math.log(2), // c = k / ln(2)
+ expectedNumberOfElements,
+ (int)Math.ceil(-(Math.log(falsePositiveProbability) /
Math.log(2)))); // k = ceil(-log_2(false prob.))
+ }
+
+ /**
+ * Construct a new Bloom filter based on existing Bloom filter data.
+ *
+ * @param bitSetSize defines how many bits should be used for the filter.
+ * @param expectedNumberOfFilterElements defines the maximum number of
elements the filter is expected to contain.
+ * @param actualNumberOfFilterElements specifies how many elements have
been inserted into the <code>filterData</code> BitSet.
+ * @param filterData a BitSet representing an existing Bloom filter.
+ */
+ public BloomFilter(int bitSetSize, int expectedNumberOfFilterElements, int
actualNumberOfFilterElements, BitSet filterData) {
+ this(bitSetSize, expectedNumberOfFilterElements);
+ this.bitset = filterData;
+ this.numberOfAddedElements = actualNumberOfFilterElements;
+ }
+
+ /**
+ * Generates a digest based on the contents of a String.
+ *
+ * @param val specifies the input data.
+ * @param charset specifies the encoding of the input data.
+ * @return digest as long.
+ */
+ public static int createHash(String val, Charset charset) {
+ return createHash(val.getBytes(charset));
+ }
+
+ /**
+ * Generates a digest based on the contents of a String.
+ *
+ * @param val specifies the input data. The encoding is expected to be
UTF-8.
+ * @return digest as long.
+ */
+ public static int createHash(String val) {
+ return createHash(val, charset);
+ }
+
+ /**
+ * Generates a digest based on the contents of an array of bytes.
+ *
+ * @param data specifies input data.
+ * @return digest as long.
+ */
+ public static int createHash(byte[] data) {
+ return createHashes(data, 1)[0];
+ }
+
+ /**
+ * Generates digests based on the contents of an array of bytes and splits
the result into 4-byte int's and store them in an array. The
+ * digest function is called until the required number of int's are
produced. For each call to digest a salt
+ * is prepended to the data. The salt is increased by 1 for each call.
+ *
+ * @param data specifies input data.
+ * @param hashes number of hashes/int's to produce.
+ * @return array of int-sized hashes
+ */
+ public static int[] createHashes(byte[] data, int hashes) {
+ int[] result = new int[hashes];
+
+ int k = 0;
+ byte salt = 0;
+ while (k < hashes) {
+ byte[] digest;
+ synchronized (digestFunction) {
+ digestFunction.update(salt);
+ salt++;
+ digest = digestFunction.digest(data);
+ }
+
+ for (int i = 0; i < digest.length/4 && k < hashes; i++) {
+ int h = 0;
+ for (int j = (i*4); j < (i*4)+4; j++) {
+ h <<= 8;
+ h |= ((int) digest[j]) & 0xFF;
+ }
+ result[k] = h;
+ k++;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Compares the contents of two instances to see if they are equal.
+ *
+ * @param obj is the object to compare to.
+ * @return True if the contents of the objects are equal.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final BloomFilter<E> other = (BloomFilter<E>) obj;
+ if (this.expectedNumberOfFilterElements !=
other.expectedNumberOfFilterElements) {
+ return false;
+ }
+ if (this.k != other.k) {
+ return false;
+ }
+ if (this.bitSetSize != other.bitSetSize) {
+ return false;
+ }
+ if (this.bitset != other.bitset && (this.bitset == null ||
!this.bitset.equals(other.bitset))) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Calculates a hash code for this class.
+ * @return hash code representing the contents of an instance of this
class.
+ */
+ @Override
+ public int hashCode() {
+ int hash = 7;
+ hash = 61 * hash + (this.bitset != null ? this.bitset.hashCode() : 0);
+ hash = 61 * hash + this.expectedNumberOfFilterElements;
+ hash = 61 * hash + this.bitSetSize;
+ hash = 61 * hash + this.k;
+ return hash;
+ }
+
+
+ /**
+ * Calculates the expected probability of false positives based on
+ * the number of expected filter elements and the size of the Bloom filter.
+ * <br /><br />
+ * The value returned by this method is the <i>expected</i> rate of false
+ * positives, assuming the number of inserted elements equals the number of
+ * expected elements. If the number of elements in the Bloom filter is less
+ * than the expected value, the true probability of false positives will
be lower.
+ *
+ * @return expected probability of false positives.
+ */
+ public double expectedFalsePositiveProbability() {
+ return getFalsePositiveProbability(expectedNumberOfFilterElements);
+ }
+
+ /**
+ * Calculate the probability of a false positive given the specified
+ * number of inserted elements.
+ *
+ * @param numberOfElements number of inserted elements.
+ * @return probability of a false positive.
+ */
+ public double getFalsePositiveProbability(double numberOfElements) {
+ // (1 - e^(-k * n / m)) ^ k
+ return Math.pow((1 - Math.exp(-k * (double) numberOfElements
+ / (double) bitSetSize)), k);
+
+ }
+
+ /**
+ * Get the current probability of a false positive. The probability is
calculated from
+ * the size of the Bloom filter and the current number of elements added
to it.
+ *
+ * @return probability of false positives.
+ */
+ public double getFalsePositiveProbability() {
+ return getFalsePositiveProbability(numberOfAddedElements);
+ }
+
+
+ /**
+ * Returns the value chosen for K.<br />
+ * <br />
+ * K is the optimal number of hash functions based on the size
+ * of the Bloom filter and the expected number of inserted elements.
+ *
+ * @return optimal k.
+ */
+ public int getK() {
+ return k;
+ }
+
+ /**
+ * Sets all bits to false in the Bloom filter.
+ */
+ public void clear() {
+ bitset.clear();
+ numberOfAddedElements = 0;
+ }
+
+ /**
+ * Adds an object to the Bloom filter. The output from the object's
+ * toString() method is used as input to the hash functions.
+ *
+ * @param element is an element to register in the Bloom filter.
+ */
+ public void add(E element) {
+ add(element.toString().getBytes(charset));
+ }
+
+ /**
+ * Adds an array of bytes to the Bloom filter.
+ *
+ * @param bytes array of bytes to add to the Bloom filter.
+ */
+ public void add(byte[] bytes) {
+ int[] hashes = createHashes(bytes, k);
+ for (int hash : hashes)
+ bitset.set(Math.abs(hash % bitSetSize), true);
+ numberOfAddedElements ++;
+ }
+
+ /**
+ * Adds all elements from a Collection to the Bloom filter.
+ * @param c Collection of elements.
+ */
+ public void addAll(Collection<? extends E> c) {
+ for (E element : c)
+ add(element);
+ }
+
+ /**
+ * Returns true if the element could have been inserted into the Bloom
filter.
+ * Use getFalsePositiveProbability() to calculate the probability of this
+ * being correct.
+ *
+ * @param element element to check.
+ * @return true if the element could have been inserted into the Bloom
filter.
+ */
+ public boolean contains(E element) {
+ return contains(element.toString().getBytes(charset));
+ }
+
+ /**
+ * Returns true if the array of bytes could have been inserted into the
Bloom filter.
+ * Use getFalsePositiveProbability() to calculate the probability of this
+ * being correct.
+ *
+ * @param bytes array of bytes to check.
+ * @return true if the array could have been inserted into the Bloom
filter.
+ */
+ public boolean contains(byte[] bytes) {
+ int[] hashes = createHashes(bytes, k);
+ for (int hash : hashes) {
+ if (!bitset.get(Math.abs(hash % bitSetSize))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns true if all the elements of a Collection could have been
inserted
+ * into the Bloom filter. Use getFalsePositiveProbability() to calculate
the
+ * probability of this being correct.
+ * @param c elements to check.
+ * @return true if all the elements in c could have been inserted into the
Bloom filter.
+ */
+ public boolean containsAll(Collection<? extends E> c) {
+ for (E element : c)
+ if (!contains(element))
+ return false;
+ return true;
+ }
+
+ /**
+ * Read a single bit from the Bloom filter.
+ * @param bit the bit to read.
+ * @return true if the bit is set, false if it is not.
+ */
+ public boolean getBit(int bit) {
+ return bitset.get(bit);
+ }
+
+ /**
+ * Set a single bit in the Bloom filter.
+ * @param bit is the bit to set.
+ * @param value If true, the bit is set. If false, the bit is cleared.
+ */
+ public void setBit(int bit, boolean value) {
+ bitset.set(bit, value);
+ }
+
+ /**
+ * Return the bit set used to store the Bloom filter.
+ * @return bit set representing the Bloom filter.
+ */
+ public BitSet getBitSet() {
+ return bitset;
+ }
+
+ /**
+ * Returns the number of bits in the Bloom filter. Use count() to retrieve
+ * the number of inserted elements.
+ *
+ * @return the size of the bitset used by the Bloom filter.
+ */
+ public int size() {
+ return this.bitSetSize;
+ }
+
+ /**
+ * Returns the number of elements added to the Bloom filter after it
+ * was constructed or after clear() was called.
+ *
+ * @return number of elements added to the Bloom filter.
+ */
+ public int count() {
+ return this.numberOfAddedElements;
+ }
+
+ /**
+ * Returns the expected number of elements to be inserted into the filter.
+ * This value is the same value as the one passed to the constructor.
+ *
+ * @return expected number of elements.
+ */
+ public int getExpectedNumberOfElements() {
+ return expectedNumberOfFilterElements;
+ }
+
+ /**
+ * Get expected number of bits per element when the Bloom filter is full.
This value is set by the constructor
+ * when the Bloom filter is created. See also getBitsPerElement().
+ *
+ * @return expected number of bits per element.
+ */
+ public double getExpectedBitsPerElement() {
+ return this.bitsPerElement;
+ }
+
+ /**
+ * Get actual number of bits per element based on the number of elements
that have currently been inserted and the length
+ * of the Bloom filter. See also getExpectedBitsPerElement().
+ *
+ * @return number of bits per element.
+ */
+ public double getBitsPerElement() {
+ return this.bitSetSize / (double)numberOfAddedElements;
+ }
+}
\ No newline at end of file
Copied: gnunet-java/src/org/gnunet/util/network/Client.java (from rev 17933,
gnunet-java/src/org/gnunet/util/client/Client.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Client.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/network/Client.java 2011-11-02 16:48:06 UTC
(rev 17937)
@@ -0,0 +1,122 @@
+package org.gnunet.util.network;
+
+import org.gnunet.util.config.Configuration;
+import org.gnunet.util.time.RelativeTime;
+
+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: should rettype really be bool?
+ // XXX: how to represent the message? does the original function really
only
+ // get the header?
+ 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/connection/Connection.java 2011-11-01
21:25:18 UTC (rev 17896)
+++ gnunet-java/src/org/gnunet/util/network/Connection.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -1,75 +0,0 @@
-package org.gnunet.util.connection;
-
-import java.net.*;
-
-import org.gnunet.util.time.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, long 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();
- }
-
- public void receive(int max, RelativeTime timeout, Receiver r) {
- throw new UnsupportedOperationException();
- }
-
- public void receive_cancel() {
- throw new UnsupportedOperationException();
- }
-
-
- public TransmitHandle notifyTransmitReady(int size, RelativeTime
timeout, boolean auto_retry,
- TransmitReadyNotify cb) {
- throw new UnsupportedOperationException();
- }
-
- public void persist() {
- this.persist = true;
- }
-
- // XXX: error code / exception
- public void disableCorking() {
- throw new UnsupportedOperationException();
- }
-
- public boolean check() {
- throw new UnsupportedOperationException();
- }
-
- public void ignoreShutdown(boolean doIgnore) {
-
- }
-
- /*
- * not implemented: GNUNET_CONNECTION_get_address (never used outside
testing)
- */
-
-}
Copied: gnunet-java/src/org/gnunet/util/network/Connection.java (from rev
17933, gnunet-java/src/org/gnunet/util/connection/Connection.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Connection.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/network/Connection.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -0,0 +1,146 @@
+package org.gnunet.util.network;
+
+import java.net.*;
+
+import org.gnunet.util.time.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, long 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
+ * @param notify_cls
+ * closure for notify
+ * @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)
+ */
+
+}
Added: gnunet-java/src/org/gnunet/util/network/Message.java
===================================================================
--- gnunet-java/src/org/gnunet/util/network/Message.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/network/Message.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -0,0 +1,5 @@
+package org.gnunet.util.network;
+
+public class Message {
+
+}
Copied: gnunet-java/src/org/gnunet/util/network/MessageHandler.java (from rev
17896, gnunet-java/src/org/gnunet/util/client/MessageHandler.java)
===================================================================
--- gnunet-java/src/org/gnunet/util/network/MessageHandler.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/network/MessageHandler.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -0,0 +1,5 @@
+package org.gnunet.util.network;
+
+public class MessageHandler {
+
+}
Modified: gnunet-java/src/org/gnunet/util/network/Receiver.java
===================================================================
--- gnunet-java/src/org/gnunet/util/connection/Receiver.java 2011-11-01
21:25:18 UTC (rev 17896)
+++ gnunet-java/src/org/gnunet/util/network/Receiver.java 2011-11-02
16:48:06 UTC (rev 17937)
@@ -1,4 +1,4 @@
-package org.gnunet.util.connection;
+package org.gnunet.util.network;
public interface Receiver {
public void receive(byte[] buf, long available, Object sockaddr, int
addrlen, int errCode);
Modified: gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java
===================================================================
--- gnunet-java/src/org/gnunet/util/connection/TransmitReadyNotify.java
2011-11-01 21:25:18 UTC (rev 17896)
+++ gnunet-java/src/org/gnunet/util/network/TransmitReadyNotify.java
2011-11-02 16:48:06 UTC (rev 17937)
@@ -1,4 +1,4 @@
-package org.gnunet.util.connection;
+package org.gnunet.util.network;
public interface TransmitReadyNotify {
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r17937 - in gnunet-java: . src/org/gnunet/service src/org/gnunet/util src/org/gnunet/util/client src/org/gnunet/util/connection src/org/gnunet/util/datastructure src/org/gnunet/util/network,
gnunet <=