Author: toad
Date: 2006-12-20 18:29:39 +0000 (Wed, 20 Dec 2006)
New Revision: 11499

Added:
   trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java
   trunk/freenet/src/freenet/io/comm/IncomingPacketFilterException.java
   trunk/freenet/src/freenet/node/OutgoingPacketMangler.java
Removed:
   trunk/freenet/src/freenet/io/comm/LowLevelFilter.java
   trunk/freenet/src/freenet/io/comm/LowLevelFilterException.java
Modified:
   trunk/freenet/src/freenet/io/comm/DummyPeerContext.java
   trunk/freenet/src/freenet/io/comm/UdpSocketManager.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/PacketSender.java
   trunk/freenet/src/freenet/node/PacketSequenceException.java
   trunk/freenet/src/freenet/support/WouldBlockException.java
Log:
Factor out a parent interface for FNPPacketMangler: OutgoingPacketMangler.
Rename LowLevelFilter to IncomingPacketFilter.
Delete some dead methods, and make some stuff private or default that shouldn't 
be default or public.

Modified: trunk/freenet/src/freenet/io/comm/DummyPeerContext.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/DummyPeerContext.java     2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/io/comm/DummyPeerContext.java     2006-12-20 
18:29:39 UTC (rev 11499)
@@ -6,7 +6,7 @@
 /**
  * @author amphibian
  * 
- * Default PeerContext if we don't have a LowLevelFilter installed.
+ * Default PeerContext if we don't have a IncomingPacketFilter installed.
  * Just carries the Peer.
  */
 public class DummyPeerContext implements PeerContext {

Copied: trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java (from rev 
11497, trunk/freenet/src/freenet/io/comm/LowLevelFilter.java)
===================================================================
--- trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java                 
        (rev 0)
+++ trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java 2006-12-20 
18:29:39 UTC (rev 11499)
@@ -0,0 +1,31 @@
+/* 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;
+
+/**
+ * Filter interface used by Freenet to decrypt incoming UDP packets.
+ * @see freenet.node.OutgoingPacketMangler
+ */
+public interface IncomingPacketFilter {
+
+    /**
+     * Process an incoming packet. This method should call
+     * USM.decodePacket() and USM.checkFilters() if necessary to 
+     * decode and dispatch messages.
+     * @param buf The buffer to read from.
+     * @param offset The offset to start reading from.
+     * @param length The length in bytes to read.
+     * @param peer The peer which sent us the packet. We only know
+     * the Peer because it's incoming; we are supposed to create
+     * or find PeerContext's for the Message's.
+     */
+    void process(byte[] buf, int offset, int length, Peer peer);
+
+    // Outgoing packets are handled elsewhere...
+    
+    /**
+     * Is the given connection closed?
+     */
+    boolean isDisconnected(PeerContext context);
+}

Copied: trunk/freenet/src/freenet/io/comm/IncomingPacketFilterException.java 
(from rev 11497, trunk/freenet/src/freenet/io/comm/LowLevelFilterException.java)
===================================================================
--- trunk/freenet/src/freenet/io/comm/IncomingPacketFilterException.java        
                        (rev 0)
+++ trunk/freenet/src/freenet/io/comm/IncomingPacketFilterException.java        
2006-12-20 18:29:39 UTC (rev 11499)
@@ -0,0 +1,17 @@
+/* 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;
+
+public class IncomingPacketFilterException extends Exception {
+       private static final long serialVersionUID = -1;
+
+       public IncomingPacketFilterException(String string) {
+               super(string);
+       }
+
+       public IncomingPacketFilterException() {
+               super();
+       }
+
+}

Deleted: trunk/freenet/src/freenet/io/comm/LowLevelFilter.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/LowLevelFilter.java       2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/io/comm/LowLevelFilter.java       2006-12-20 
18:29:39 UTC (rev 11499)
@@ -1,30 +0,0 @@
-/* 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;
-
-/**
- * Filter interface used by Freenet to decrypt incoming packets.
- */
-public interface LowLevelFilter {
-
-    /**
-     * Process an incoming packet. This method should call
-     * USM.decodePacket() and USM.checkFilters() if necessary to 
-     * decode and dispatch messages.
-     * @param buf The buffer to read from.
-     * @param offset The offset to start reading from.
-     * @param length The length in bytes to read.
-     * @param peer The peer which sent us the packet. We only know
-     * the Peer because it's incoming; we are supposed to create
-     * or find PeerContext's for the Message's.
-     */
-    void process(byte[] buf, int offset, int length, Peer peer);
-
-    // Outgoing packets are handled elsewhere...
-    
-    /**
-     * Is the given connection closed?
-     */
-    boolean isDisconnected(PeerContext context);
-}

Deleted: trunk/freenet/src/freenet/io/comm/LowLevelFilterException.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/LowLevelFilterException.java      
2006-12-20 18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/io/comm/LowLevelFilterException.java      
2006-12-20 18:29:39 UTC (rev 11499)
@@ -1,17 +0,0 @@
-/* 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;
-
-public class LowLevelFilterException extends Exception {
-       private static final long serialVersionUID = -1;
-
-       public LowLevelFilterException(String string) {
-               super(string);
-       }
-
-       public LowLevelFilterException() {
-               super();
-       }
-
-}

Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/UdpSocketManager.java     2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/io/comm/UdpSocketManager.java     2006-12-20 
18:29:39 UTC (rev 11499)
@@ -43,7 +43,7 @@
        private final LinkedList _filters = new LinkedList();
        private final LinkedList _unclaimed = new LinkedList();
        private int _dropProbability;
-       private LowLevelFilter lowLevelFilter;
+       private IncomingPacketFilter lowLevelFilter;
        /** RNG for debugging, used with _dropProbability.
         * NOT CRYPTO SAFE. DO NOT USE FOR THINGS THAT NEED CRYPTO SAFE RNG!
         */
@@ -257,7 +257,7 @@

        /**
         * Decode a packet from data and a peer.
-        * Can be called by LowLevelFilter's.
+        * Can be called by IncomingPacketFilter's.
      * @param data
      * @param offset
      * @param length
@@ -430,7 +430,7 @@
                }
        }

-       /** LowLevelFilter should call this when a node is disconnected. */
+       /** IncomingPacketFilter should call this when a node is disconnected. 
*/
        public void onDisconnect(PeerContext ctx) {
            synchronized(_filters) {
                        ListIterator i = _filters.listIterator();
@@ -569,7 +569,7 @@
 //                     try {
 //                             lowLevelFilter.processOutgoing(blockToSend, 0, 
blockToSend.length, destination);
 //                             return;
-//                     } catch (LowLevelFilterException t) {
+//                     } catch (IncomingPacketFilterException t) {
 //                             Logger.error(this, "Caught "+t+" sending "+m+" 
to "+destination, t);
 //                             destination.forceDisconnect();
 //                             throw new NotConnectedException("Error 
"+t.toString()+" forced disconnect");
@@ -582,7 +582,7 @@

        /**
         * Send a block of encoded bytes to a peer. This is called by
-        * send, and by LowLevelFilter.processOutgoing(..).
+        * send, and by IncomingPacketFilter.processOutgoing(..).
      * @param blockToSend The data block to send.
      * @param destination The peer to send it to.
      */
@@ -642,7 +642,7 @@
                _dispatcher = d;
        }

-       public void setLowLevelFilter(LowLevelFilter f) {
+       public void setLowLevelFilter(IncomingPacketFilter f) {
            lowLevelFilter = f;
        }


Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-12-20 
18:29:39 UTC (rev 11499)
@@ -30,21 +30,21 @@
  * 
  * This includes encryption, authentication, and may later
  * include queueing etc. (that may require some interface
- * changes in LowLevelFilter).
+ * changes in IncomingPacketFilter).
  */
-public class FNPPacketMangler implements LowLevelFilter {
+public class FNPPacketMangler implements OutgoingPacketMangler, 
IncomingPacketFilter {

        private static boolean logMINOR;
     final Node node;
     final PeerManager pm;
     final UdpSocketManager usm;
-    static final int MAX_PACKETS_IN_FLIGHT = 256; 
-    static final EntropySource fnpTimingSource = new EntropySource();
-    static final EntropySource myPacketDataSource = new EntropySource();
-    static final int RANDOM_BYTES_LENGTH = 12;
-    static final int HASH_LENGTH = 32;
+    final EntropySource fnpTimingSource;
+    final EntropySource myPacketDataSource;
+    private static final int MAX_PACKETS_IN_FLIGHT = 256; 
+    private static final int RANDOM_BYTES_LENGTH = 12;
+    private static final int HASH_LENGTH = 32;
     /** Minimum headers overhead */
-       public static final int HEADERS_LENGTH_MINIMUM =
+       private static final int HEADERS_LENGTH_MINIMUM =
                4 + // sequence number
        RANDOM_BYTES_LENGTH + // random junk
        1 + // version
@@ -60,13 +60,15 @@
        HASH_LENGTH + // hash
        1; // number of messages
        /** Headers overhead if there is one message and no acks. */
-       public static final int HEADERS_LENGTH_ONE_MESSAGE = 
+       static public final int HEADERS_LENGTH_ONE_MESSAGE = 
                HEADERS_LENGTH_MINIMUM + 2; // 2 bytes = length of message. 
rest is the same.

     public FNPPacketMangler(Node node) {
         this.node = node;
         this.pm = node.peers;
         this.usm = node.usm;
+        fnpTimingSource = new EntropySource();
+        myPacketDataSource = new EntropySource();
     }

     /**
@@ -84,12 +86,12 @@
      * 
      */

-    /**
-     * Decrypt and authenticate packet.
-     * Then feed it to USM.checkFilters.
-     * Packets generated should have a PeerNode on them.
-     * Note that the buffer can be modified by this method.
-     */
+       /**
+        * Decrypt and authenticate packet.
+        * Then feed it to USM.checkFilters.
+        * Packets generated should have a PeerNode on them.
+        * Note that the buffer can be modified by this method.
+        */
     public void process(byte[] buf, int offset, int length, Peer peer) {
         node.random.acceptTimerEntropy(fnpTimingSource, 0.25);
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
@@ -898,9 +900,9 @@
         if(logMINOR) Logger.minor(this, "Done");
     }

-    /**
-     * Build a packet and send it, from a whole bunch of messages.
-     */
+    /* (non-Javadoc)
+        * @see 
freenet.node.OutgoingPacketMangler#processOutgoingOrRequeue(freenet.node.MessageItem[],
 freenet.node.PeerNode, boolean, boolean)
+        */
     public void processOutgoingOrRequeue(MessageItem[] messages, PeerNode pn, 
boolean neverWaitForPacketNumber, boolean dontRequeue) {
        String requeueLogString = "";
        if(!dontRequeue) {
@@ -1126,12 +1128,9 @@
         processOutgoingPreformatted(buf, 0, loc, pn, neverWaitForPacketNumber, 
callbacks, alreadyReportedBytes);
     }

-    /**
-     * Build a packet and send it. From a Message recently converted into 
byte[],
-     * but with no outer formatting.
-     * @throws PacketSequenceException 
-     * @throws WouldBlockException 
-     */
+    /* (non-Javadoc)
+        * @see freenet.node.OutgoingPacketMangler#processOutgoing(byte[], int, 
int, freenet.node.KeyTracker, int)
+        */
     public void processOutgoing(byte[] buf, int offset, int length, KeyTracker 
tracker, int alreadyReportedBytes) throws KeyChangedException, 
NotConnectedException, PacketSequenceException, WouldBlockException {
         byte[] newBuf = preformat(buf, offset, length);
         processOutgoingPreformatted(newBuf, 0, newBuf.length, tracker, -1, 
null, alreadyReportedBytes);
@@ -1171,12 +1170,7 @@
         }
     }

-    public byte[] preformat(Message msg, PeerNode pn) {
-        byte[] buf = msg.encodeToPacket(pn);
-        return preformat(buf, 0, buf.length);
-    }
-    
-    public byte[] preformat(byte[] buf, int offset, int length) {
+    byte[] preformat(byte[] buf, int offset, int length) {
         byte[] newBuf;
         if(buf != null) {
             newBuf = new byte[length+3];
@@ -1191,22 +1185,9 @@
         return newBuf;
     }

-    /**
-     * Encrypt a packet, prepend packet acks and packet resend requests, and 
send it. 
-     * The provided data is ready-formatted, meaning that it already has the 
message 
-     * length's and message counts.
-     * @param buf Buffer to read data from.
-     * @param offset Point at which to start reading.
-     * @param length Number of bytes to read.
-     * @param tracker The KeyTracker to use to encrypt the packet and send it 
to the
-     * associated PeerNode.
-     * @param packetNumber If specified, force use of this particular packet 
number.
-     * Means this is a resend of a dropped packet.
-     * @throws NotConnectedException If the node is not connected.
-     * @throws KeyChangedException If the primary key changes while we are 
trying to send this packet.
-     * @throws PacketSequenceException 
-     * @throws WouldBlockException If we cannot allocate a packet number 
because it would block.
-     */
+    /* (non-Javadoc)
+        * @see 
freenet.node.OutgoingPacketMangler#processOutgoingPreformatted(byte[], int, 
int, freenet.node.KeyTracker, int, freenet.node.AsyncMessageCallback[], int)
+        */
     public void processOutgoingPreformatted(byte[] buf, int offset, int 
length, KeyTracker tracker, int packetNumber, AsyncMessageCallback[] callbacks, 
int alreadyReportedBytes) throws KeyChangedException, NotConnectedException, 
PacketSequenceException, WouldBlockException {
         if(logMINOR) {
             String log = 
"processOutgoingPreformatted("+Fields.hashCode(buf)+", "+offset+ ',' +length+ 
',' +tracker+ ',' +packetNumber+ ',';
@@ -1435,10 +1416,9 @@
         kt.pn.sentPacket();
     }

-    /**
-     * Send a handshake, if possible, to the node.
-     * @param pn
-     */
+    /* (non-Javadoc)
+        * @see 
freenet.node.OutgoingPacketMangler#sendHandshake(freenet.node.PeerNode)
+        */
     public void sendHandshake(PeerNode pn) {
        if(logMINOR) Logger.minor(this, "Possibly sending handshake to "+pn);
         DiffieHellmanContext ctx;
@@ -1502,6 +1482,9 @@
         }
     }

+    /* (non-Javadoc)
+        * @see 
freenet.node.OutgoingPacketMangler#isDisconnected(freenet.io.comm.PeerContext)
+        */
     public boolean isDisconnected(PeerContext context) {
         if(context == null) return false;
         return !((PeerNode)context).isConnected();
@@ -1526,4 +1509,8 @@

         sendAuthPacket(1, 1, phase, ctx.concatAndSignAndCrypt(), pn, replyTo);
     }
+
+       public void resend(ResendPacketItem item) throws 
PacketSequenceException, WouldBlockException, KeyChangedException, 
NotConnectedException {
+               processOutgoingPreformatted(item.buf, 0, item.buf.length, 
item.kt, item.packetNumber, item.callbacks, 0);
+       }
 }

Added: trunk/freenet/src/freenet/node/OutgoingPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/OutgoingPacketMangler.java                   
        (rev 0)
+++ trunk/freenet/src/freenet/node/OutgoingPacketMangler.java   2006-12-20 
18:29:39 UTC (rev 11499)
@@ -0,0 +1,76 @@
+/* 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.node;
+
+import freenet.io.comm.NotConnectedException;
+import freenet.io.comm.PeerContext;
+import freenet.support.WouldBlockException;
+
+/**
+ * Low-level interface for sending packets.
+ * A UDP-based transport will have to implement both this and 
IncomingPacketFilter, usually
+ * on the same class. 
+ * @see freenet.io.comm.IncomingPacketFilter
+ * @see freenet.node.FNPPacketMangler
+ */
+public interface OutgoingPacketMangler {
+
+       /**
+        * Build one or more packets and send them, from a whole bunch of 
messages.
+        * If any MessageItem's are formatted already, they will be sent as 
single packets.
+        * Any packets which cannot be sent will be requeued on the PeerNode.
+        */
+       public void processOutgoingOrRequeue(MessageItem[] messages, PeerNode 
pn,
+                       boolean neverWaitForPacketNumber, boolean dontRequeue);
+
+       /**
+        * Resend a single packet.
+        */
+       public void resend(ResendPacketItem item) throws 
PacketSequenceException, WouldBlockException, KeyChangedException, 
NotConnectedException;
+       
+       /**
+        * Build a packet and send it. From a Message recently converted into 
byte[],
+        * but with no outer formatting.
+        * @throws PacketSequenceException 
+        * @throws WouldBlockException 
+        */
+       public void processOutgoing(byte[] buf, int offset, int length,
+                       KeyTracker tracker, int alreadyReportedBytes)
+                       throws KeyChangedException, NotConnectedException,
+                       PacketSequenceException, WouldBlockException;
+
+       /**
+        * Encrypt a packet, prepend packet acks and packet resend requests, 
and send it. 
+        * The provided data is ready-formatted, meaning that it already has 
the message 
+        * length's and message counts.
+        * @param buf Buffer to read data from.
+        * @param offset Point at which to start reading.
+        * @param length Number of bytes to read.
+        * @param tracker The KeyTracker to use to encrypt the packet and send 
it to the
+        * associated PeerNode.
+        * @param packetNumber If specified, force use of this particular 
packet number.
+        * Means this is a resend of a dropped packet.
+        * @throws NotConnectedException If the node is not connected.
+        * @throws KeyChangedException If the primary key changes while we are 
trying to send this packet.
+        * @throws PacketSequenceException 
+        * @throws WouldBlockException If we cannot allocate a packet number 
because it would block.
+        */
+       public void processOutgoingPreformatted(byte[] buf, int offset, int 
length,
+                       KeyTracker tracker, int packetNumber,
+                       AsyncMessageCallback[] callbacks, int 
alreadyReportedBytes)
+                       throws KeyChangedException, NotConnectedException,
+                       PacketSequenceException, WouldBlockException;
+
+       /**
+        * Send a handshake, if possible, to the node.
+        * @param pn
+        */
+       public void sendHandshake(PeerNode pn);
+
+       /**
+        * Is a peer disconnected?
+        */
+       public boolean isDisconnected(PeerContext context);
+
+}
\ No newline at end of file

Modified: trunk/freenet/src/freenet/node/PacketSender.java
===================================================================
--- trunk/freenet/src/freenet/node/PacketSender.java    2006-12-20 18:16:45 UTC 
(rev 11498)
+++ trunk/freenet/src/freenet/node/PacketSender.java    2006-12-20 18:29:39 UTC 
(rev 11499)
@@ -229,7 +229,7 @@
                         if(item == null) continue;
                         try {
                             if(logMINOR) Logger.minor(this, "Resending 
"+item.packetNumber+" to "+item.kt);
-                            
node.packetMangler.processOutgoingPreformatted(item.buf, 0, item.buf.length, 
item.kt, item.packetNumber, item.callbacks, 0);
+                            node.packetMangler.resend(item);
                         } catch (KeyChangedException e) {
                             Logger.error(this, "Caught "+e+" resending packets 
to "+kt);
                             pn.requeueResendItems(resendItems);

Modified: trunk/freenet/src/freenet/node/PacketSequenceException.java
===================================================================
--- trunk/freenet/src/freenet/node/PacketSequenceException.java 2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/node/PacketSequenceException.java 2006-12-20 
18:29:39 UTC (rev 11499)
@@ -3,9 +3,9 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package freenet.node;

-import freenet.io.comm.LowLevelFilterException;
+import freenet.io.comm.IncomingPacketFilterException;

-public class PacketSequenceException extends LowLevelFilterException {
+public class PacketSequenceException extends IncomingPacketFilterException {
        private static final long serialVersionUID = -1;

        public PacketSequenceException(String string) {

Modified: trunk/freenet/src/freenet/support/WouldBlockException.java
===================================================================
--- trunk/freenet/src/freenet/support/WouldBlockException.java  2006-12-20 
18:16:45 UTC (rev 11498)
+++ trunk/freenet/src/freenet/support/WouldBlockException.java  2006-12-20 
18:29:39 UTC (rev 11499)
@@ -1,11 +1,11 @@
 package freenet.support;

-import freenet.io.comm.LowLevelFilterException;
+import freenet.io.comm.IncomingPacketFilterException;

 /**
  * Thrown when we would have to block but have been told not to.
  */
-public class WouldBlockException extends LowLevelFilterException {
+public class WouldBlockException extends IncomingPacketFilterException {
        private static final long serialVersionUID = -1;

        public WouldBlockException(String string) {


Reply via email to