[Webfunds-commits] java/webfunds/sox DepositReply.java

Ian Grigg iang@cypherpunks.ai
Fri, 6 Apr 2001 20:06:11 -0400 (AST)


iang        01/04/06 20:06:11

  Modified:    webfunds/sox DepositReply.java
  Log:
  rewrote Receipt code out and MailItem code in, so that when the
  Issuers are all upgraded with the new DepositRequest, we can
  bump up the version number and deprecate the Receipt-only Reply
  (to bring in line with MailItem-enabled MailReply!).

Revision  Changes    Path
1.23      +147 -46   java/webfunds/sox/DepositReply.java

Index: DepositReply.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/DepositReply.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- DepositReply.java	2000/09/24 23:32:04	1.22
+++ DepositReply.java	2001/04/07 00:06:11	1.23
@@ -1,13 +1,20 @@
 /*
- * $Id: DepositReply.java,v 1.22 2000/09/24 23:32:04 iang Exp $
+ * $Id: DepositReply.java,v 1.23 2001/04/07 00:06:11 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
 package webfunds.sox;
 
-import java.io.*;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 
+import webfunds.utils.Panic;
+
 /**
  *  A deposit reply.
  *  This might want to change to being an extension of MailReply
@@ -16,21 +23,28 @@
 public class DepositReply
     extends Reply
 {
-        
-    public static final int SUCCEEDED = 0;
+
+    // public static final int SUCCEEDED = 0;
     
     protected byte[] receipt_data;
+
+    protected MailItem[] mails;
 
-    public Receipt receipt()
+    private Receipt receipt()
     {
-        if (null == receipt_data || 0 == receipt_data.length)
+
+        MailItem[] mis = getMailItems();
+        if (null == mis || 0 == mis.length)
             return null;
+
+        byte[] data = mis[0].getMessage();
         Receipt r;
+
         try 
         {
-            r = new Receipt(receipt_data);
+            r = new Receipt(data);
         }
-        catch(Exception e) 
+        catch (Exception e) 
         {
             System.err.println("Ouch - dud receipt: " + e);
             r = null;
@@ -39,51 +53,77 @@
     }
     
     public Receipt getReceipt()      { return receipt(); }
-    public byte[]  getReceiptData()  { return receipt_data; }
+    // public byte[]  getReceiptData()  { return receipt_data; }
 
+    /**
+     *  This is the main interface.
+     *  Recover the mail yourself, please!
+     */
     public MailItem[] getMailItems()
     {
-        if (null == receipt_data || 0 == receipt_data.length)
-           return new MailItem[0];
-
-        MailItem mis[] = new MailItem[1];
-        try {
-            mis[0] = new MailItem(MailItem.RECEIPT, receipt_data);
-        } catch (SOXPacketException ex) {
-            throw new RuntimeException("cannot make MailItem: " + ex);
-        }
-        return mis ;
+        return mails;
+//        if (null == receipt_data || 0 == receipt_data.length)
+//           return new MailItem[0];
+//
+//        MailItem mis[] = new MailItem[1];
+//        if (sub_version < DepositRequest.DEPOSIT_WITHDRAW)
+//        {
+//            try {
+//                mis[0] = new MailItem(MailItem.RECEIPT, receipt_data);
+//            } catch (SOXPacketException ex) {
+//                throw new Panic("cannot make MailItem: " + ex);
+//            }
+//        }
+//        else          // delivered as a real MailItem
+//        {
+//            try {
+//                mis[0] = new MailItem(receipt_data);
+//            } catch (SOXPacketException ex) {
+//                throw new Panic("cannot make MailItem: " + ex);
+//            }
+//        }
+//        return mis ;
     }
 
-    /**
-     *  Create a Good DepositReply - Deprecated ???
-     *  Hence the odd calling sequence, it is a hack for old receipt code.
-     *  This isn't used AFAIK.
-    public DepositReply(byte[] receipt, DepositRequest request)
-    {
-        super(request);
-        this.receipt_data = receipt;
-    }
-     */
 
+
     /**
      * Create a Good DepositReply with a MailItem
      */
     public DepositReply(DepositRequest request, MailItem mail)
     {
         super(request);
+
         if (!mail.isReceipt())
-            throw new RuntimeException("only receipt is supported: " + mail);
-        this.receipt_data = mail.getMessage();
+            throw new Panic("only receipt is supported: " + mail);
+
+        mails = new MailItem[1];
+        mails[0] = mail;
     }
 
+    public DepositReply(DepositRequest request, MailItem mails[])
+    {
+        super(request);
+
+        if (mails == null || (mails.length != 1))
+            throw new Panic("one MailItem only is supported");
+
+        MailItem m = mails[0];
+        if (!m.isReceipt())
+            throw new Panic("only receipt is supported: " + m);
+
+        this.mails = new MailItem[1];
+        this.mails[0] = m;
+    }
+
     /**
      * Create a Bad DepositReply - required by DepositRequest
      */
     public DepositReply(DepositRequest request, int errNum)
     {
         super(request, errNum);
-        this.receipt_data = new byte[0];
+        // this.receipt_data = new byte[0];
+        mails = new MailItem[0];
     }
 
 
@@ -99,7 +139,26 @@
     {
         DataOutputStream dos = new DataOutputStream(os);
         super.encode(dos);
-        writeByteArray(dos, receipt_data);
+
+        if (sub_version < DepositRequest.DEPOSIT_WITHDRAW)
+        {
+            byte[] receipt_data;
+            if (mails.length == 0)
+                receipt_data = new byte[0];
+            else
+                receipt_data = mails[0].getMessage();
+
+            writeByteArray(dos, receipt_data);
+            return ;
+        }
+
+        // this is basically the same code as MailReply
+
+        int len = (mails == null) ? 0 : mails.length;
+        dos.writeInt(len);
+        for(int i = 0; i < len; i++)
+            mails[i].encode(dos);
+
     }
 
     public void decode(InputStream is)
@@ -107,11 +166,53 @@
     {
         DataInputStream dis = new DataInputStream(is);
         super.decode(dis);
+
+        if (sub_version < DepositRequest.DEPOSIT_WITHDRAW)
+        {
+            byte[] receipt_data;
+            try {
+                receipt_data = readByteArray(dis);
+            } catch (IOException ex) {
+                 throw new SOXPacketException("IOEx: " + ex);
+            }
+            
+            if (errorNumber == 0)
+            {
+                mails = new MailItem[1];
+                MailItem mail = new MailItem(MailItem.RECEIPT, receipt_data);
+                mails[0] = mail;
+            }
+            else
+            {
+                mails = new MailItem[0];
+                if (receipt_data.length != 0)
+                    System.err.println("ignoring mail, error: " + errorNumber);
+            }
+            
+            return ;
+        }
+
+System.err.println("Decoding DepositReply " + sub_version);
+        int len;
         try {
-            receipt_data = readByteArray(dis);
+            len = dis.readInt();
         } catch (IOException ex) {
              throw new SOXPacketException("IOEx: " + ex);
         }
+        
+        mails = new MailItem[len];
+System.err.println("There are " + len + " mails in this reply");
+        for (int i = 0; i < len; i++)
+        {
+        
+            try {
+                mails[i] = new MailItem(dis);
+            } catch (IOException ex) {
+                throw new SOXPacketException("IOEx: " + ex);
+            }
+System.err.println("decode mail#" + i + ": " + mails[i].toString());
+        }
+
     }
 
 
@@ -124,7 +225,9 @@
         if (errorNumber != 0)
             return "DepositReply in error: " + super.toString();
 
-        String s = "SOX DepositReply ";
+        String s = "SOX DepositReply V" + sub_version +
+                   " (" + req_version + ") ";
+
         MailItem[] mails = getMailItems();
         int len = (mails == null) ? 0 : mails.length;
         s += "num = " + len + " mails";
@@ -177,20 +280,18 @@
         }
 
         Receipt r = Receipt.example();
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try {
-            r.encode(baos);
-        } catch (IOException ex) {
-            throw new RuntimeException("IOEx: " + ex);
-        }
-        byte[] buf = baos.toByteArray();
+        MailItem[] mis = new MailItem[1];
 
-        DepositReply obj;
+//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         try {
-            obj = new DepositReply(req, buf);
+            mis[0] = new MailItem(MailItem.RECEIPT, r);
+//            r.encode(baos);
         } catch (SOXPacketException ex) {
-            throw new RuntimeException("SOXPEx: " + ex);
+            throw new Panic("IOEx: " + ex);
         }
+//        byte[] buf = baos.toByteArray();
+
+        DepositReply obj = new DepositReply(req, mis);
         return obj;
     }
 
@@ -268,7 +369,7 @@
         DepositReply q = new DepositReply(r, buf);
         if (!p.equals(q))
         {
-            throw new RuntimeException("FAILED:\n\n"+q+"\n\n"+p+"\nEND\n");
+            throw new Panic("FAILED:\n\n"+q+"\n\n"+p+"\nEND\n");
         }
     }