Author: toad
Date: 2007-05-31 22:40:13 +0000 (Thu, 31 May 2007)
New Revision: 13425

Added:
   trunk/freenet/src/freenet/io/comm/AsyncMessageFilterCallback.java
Modified:
   trunk/freenet/src/freenet/io/comm/MessageFilter.java
Log:
Async MessageFilter API. Sometimes an asynchronous match really is easier, as 
well as being cheaper.

Added: trunk/freenet/src/freenet/io/comm/AsyncMessageFilterCallback.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/AsyncMessageFilterCallback.java           
                (rev 0)
+++ trunk/freenet/src/freenet/io/comm/AsyncMessageFilterCallback.java   
2007-05-31 22:40:13 UTC (rev 13425)
@@ -0,0 +1,27 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.io.comm;
+
+/**
+ * Sometimes it really is simpler to do things asynchronously. And sometimes 
it's essential for performance.
+ * @author toad
+ */
+public interface AsyncMessageFilterCallback {
+
+       /**
+        * Called when the filter is matched. It will have been removed before 
the callback is called,
+        * and no locks should be held.
+        * @param m The message which matched the filter.
+        */
+       void onMatched(Message m);
+       
+       /**
+        * Check whether the filter should be removed. Note that USM locks may 
be held by the caller
+        * when this is called: the implementation should not do anything that 
might cause USM-related 
+        * locks to be taken or messages to be sent.
+        * @return True if the filter should be immediately timed out.
+        */
+       boolean shouldTimeout();
+       
+}

Modified: trunk/freenet/src/freenet/io/comm/MessageFilter.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/MessageFilter.java        2007-05-31 
22:29:38 UTC (rev 13424)
+++ trunk/freenet/src/freenet/io/comm/MessageFilter.java        2007-05-31 
22:40:13 UTC (rev 13425)
@@ -45,6 +45,7 @@
     private MessageFilter _or;
     private Message _message;
     private boolean _matchesDroppedConnections;
+    private AsyncMessageFilterCallback _callback;

     private MessageFilter() {
         setTimeout(DEFAULT_TIMEOUT);
@@ -68,6 +69,11 @@
                return this;
        }

+       public MessageFilter setNoTimeout() {
+               _timeout = Long.MAX_VALUE;
+               return this;
+       }
+       
        public MessageFilter setType(MessageType type) {
                _type = type;
                return this;
@@ -123,6 +129,11 @@
            return this;
        }

+       public MessageFilter setAsyncCallback(AsyncMessageFilterCallback cb) {
+               _callback = cb;
+               return this;
+       }
+       
        public boolean match(Message m) {
                if ((_or != null) && (_or.match(m))) {
                        _matched = true;
@@ -158,6 +169,8 @@
        }

        public boolean timedOut() {
+               if(_callback != null && _callback.shouldTimeout())
+                       _timeout = -1; // timeout immediately
                return _timeout < System.currentTimeMillis();
        }

@@ -205,8 +218,13 @@
      * Notify waiters that we have been matched.
      * Hopefully no locks will be held at this point by the caller.
      */
-       public synchronized void onMatched() {
-               notifyAll();
+       public void onMatched() {
+               if(_callback != null) {
+                       _callback.onMatched(_message);
+               }
+               synchronized(this) {
+                       notifyAll();
+               }
        }

        /**


Reply via email to