[Webfunds-commits] java/webfunds/sox NymReply.java NymRequest.java SOXRequestException.java Errors.java

Ian Grigg iang@cypherpunks.ai
Fri, 13 Oct 2000 21:52:23 -0400 (AST)


iang        00/10/13 21:52:22

  Modified:    webfunds/sox Errors.java
  Added:       webfunds/sox NymReply.java NymRequest.java
                        SOXRequestException.java
  Log:
  Nym is new feature to allow setting names and other details with key pairs.

Revision  Changes    Path
1.16      +16 -2     java/webfunds/sox/Errors.java

Index: Errors.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/Errors.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- Errors.java	2000/07/18 22:21:04	1.15
+++ Errors.java	2000/10/14 01:52:21	1.16
@@ -1,5 +1,5 @@
 /*
- * $Id: Errors.java,v 1.15 2000/07/18 22:21:04 iang Exp $
+ * $Id: Errors.java,v 1.16 2000/10/14 01:52:21 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -78,8 +78,15 @@
     public static final int ERROR_LATER                = 63;
     public static final int ERROR_BACKEND              = 64;
 
-    public static final int HIGHEST_ERROR_IN_STRINGS   = ERROR_BACKEND;
+    /**
+     *  70-79 are Nym errors.
+     */
+    public static final int ERROR_ILLEGAL_CHARS        = 70;
+
+    public static final int HIGHEST_ERROR_IN_STRINGS   = ERROR_ILLEGAL_CHARS;
 
+
+
     private static final String errors[] =
     {
     /*  0 */    "good",
@@ -126,6 +133,13 @@
                 "not a KHID",
                 "try again later - backend is down",
                 "unknown backend error",
+                "",
+                "",
+                "",
+                "",
+                "",
+
+    /* 70 */    "Illegal Chars In Name",
     };
 
     static



1.1                  java/webfunds/sox/NymReply.java

Index: NymReply.java
===================================================================
/*
 * $Id: NymReply.java,v 1.1 2000/10/14 01:52:22 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.sox;

import java.io.*;

/**
 *  A deposit reply.
 *  This might want to change to being an extension of MailReply
 *  in the next change.
 */
public class NymReply
    extends Reply
{
        
    public static final int  SUCCEEDED       = 0,
                             INTERNAL_ERROR  = 1,
                             NOT_PERMITTED   = 2,
                             FROZEN          = 3,
                             BAD_DATA        = 4;
    


    private String applicationErrors;
    private int    applicationStatus;

    private String[] names;

    /**
     *  Rather than predict what information is required, just use
     *  an array of strings.  Some conventions:
     *
     *       0.  short, printable PUBLISHABLE nickname
     *       1.  email address
     *
     *  Fields can be empty (null or empty, it is the same with SOX).
     *  Any other definitions can be handled at the application layer.
     */
    public String[]  getNames()                { return names; }

    /**
     *  Set the names.
     *  The array is copied, and all nulls converted to empties.
     *  Note that the strings are really treated as byte arrays.
     */
    public void      setNames(String[] names)
    {
        if (names == null)
            names = new String[0];

        /*
         *  copy our own array, and make the nulls into empty strings
         *  makes code and comparison easier later
         */
        int num = names.length;
        this.names = new String[num];
        for (int i = 0; i < num; i++)
            this.names[i] = (names[i] == null) ? "" : names[i];
    }

    private long flags;
    /**
     *  Rather than predict what information is required, just use
     *  a big long for flags.
     *  Any definitions can be handled at the application layer.
     */
    public long      getFlags()                { return flags; }
    public void      setFlags(long flags)      { this.flags = flags; }
            


    /**
     *  Create a Good NymReply.
     */
    public NymReply(NymRequest request, String[] names, long flags)
    {
        super(request);

        applicationStatus = SUCCEEDED;
        applicationErrors = "";

        this.names = names;
        this.flags = flags;
    }

    /**
     *  Create a Good NymReply but with a bad result.
     */
    public NymReply(NymRequest request, int errno, String errors)
    {
        super(request);

        applicationStatus = errno;
        applicationErrors = errors;

        names = new String[0];
        flags = 0;
    }

    /**
     * Create a Bad NymReply - required by NymRequest
     */
    public NymReply(NymRequest request, int errNum)
    {
        super(request, errNum);

        applicationStatus = -1;
        applicationErrors = "";

        names = new String[0];
        flags = 0;
    }


    public NymReply(NymRequest request, byte[] data)
        throws SOXPacketException                { super(request, data); }

    public NymReply(NymRequest request, InputStream is)
        throws SOXPacketException                { super(request, is); }


    public void encode(OutputStream os)
        throws IOException
    {
        DataOutputStream dos = new DataOutputStream(os);
        super.encode(dos);

        dos.writeInt(applicationStatus);
        writeString(dos, applicationErrors);

        dos.writeLong(flags);

        int num = names.length;
        dos.writeByte(num);
        for (int i = 0; i < num; i++)
            writeString(dos, names[i]);
    }

    public void decode(InputStream is)
        throws SOXPacketException
    {
        DataInputStream dis = new DataInputStream(is);
        super.decode(dis);

        try {
            applicationStatus = dis.readInt();
            applicationErrors = readString(dis);

            flags = dis.readLong();

            int num = dis.readUnsignedByte();
            if (num < 0 || num > 100)
                throw new SOXPacketException("number of names is odd: " + num);
            names = new String[num];
            for (int i = 0; i < num; i++)
                names[i] = readString(dis);
        } catch (IOException ex) {
             throw new SOXPacketException("IOEx: " + ex);
        }
    }




////// Self-Test //////////////////////////////////

    public String toString()
    {
        if (errorNumber != 0)
            return "NymReply in error: " + super.toString();

        String s = "SOX NymReply ";
        if (applicationStatus != 0)
            return s + " !" + applicationStatus + ": " + applicationErrors;

        s += "\tflags: " + flags + "\t\tnum: " + this.names.length;
        for (int i = 0; i < names.length; i++)
            s += "\n\t" + i + ": " + names[i];

        return s;
    }

    public boolean equals(java.lang.Object obj)
    {
        if (obj == null || !(obj instanceof NymReply))
            return false;

        NymReply other = (NymReply)obj;

        if (!super.equals(other))
            return false;

        if (applicationStatus != other.applicationStatus)
            return false;

        if (!applicationErrors.equals(other.applicationErrors))
            return false;

        if (flags != other.flags)
            return false;

//System.err.println("6");
        int me = names.length;
        int her = other.names.length;
        if (me != other.names.length)
            return false;
 
        String[] hers = other.names;
        for (int i = 0; i < me; i++)
             if (!names[i].equals(hers[i]))
                 return false;

        return true;
    }

    public static NymReply example(NymRequest req)
    {
        int b  = Utils.exampleByte() & 0x0FF;

        if ((b & 0x07) == 0x07)               // return error reply!
        {
            int errno = Utils.exampleByte() & 0x0FF;
            b >>= 3;
            NymReply obj = new NymReply(req, b);
            return obj;
        }

        if ((b & 0x07) == 0x06)               // return Appl error reply!
        {
            int errno = Utils.exampleByte() & 0x0FF;
            b >>= 3;
            String s = Utils.exampleString();
            NymReply obj = new NymReply(req, b, s);
            return obj;
        }

        long flags = Utils.exampleLong();
        int len = 0x07 & Utils.exampleByte();

        String[] names = new String[len];
        for (int i = 0; i < len; i++)
             names[i] = Utils.exampleString();

        NymReply example = new NymReply(req, names, flags);

        // example.setFlags(flags);
        // example.setNames(names);

        return example;
    }

    public static NymReply example()
    {
        return example(NymRequest.example());
    }

    public static void main(String[] args)
    {
        int num = 2000;
        String type = "-c";
        if (args.length > 0)
        {
            int a = 0;
            if (args[a].startsWith("-"))
            {
                type = args[a++];
            }

            if (a < args.length)
            {
                Integer i = null;
                try {
                    i = new Integer(args[a]);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
                num = i.intValue();
            }
        }

        try {
            if (type.equals("-t") || type.equals("-i"))
            {
                while (true)
                {
                    if (type.equals("-t"))
                        readWrite();
                    else if (type.equals("-i"))
                        input();
                }
            }
            else
            {
                for (int i = 0; i < num; i++)
                {
                    if (type.equals("-c"))
                        cycle();
                    else if (type.equals("-o"))
                        output();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.out.flush();          // last buffered lump still there
        System.out.close();
        System.exit(0);
    }

    protected static void cycle()
        throws Exception
    {
        NymRequest r = NymRequest.example();
        NymReply p = example(r);
        System.err.println("Writing: " + p);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        p.encode(baos);
        byte[] buf = baos.toByteArray();

        NymReply q = new NymReply(r, buf);
        if (!p.equals(q))
        {
            throw new RuntimeException("FAILED:\n\n"+q+"\n\n"+p+"\nEND\n");
        }
    }

    protected static void output()
        throws Exception
    {
        NymRequest r = NymRequest.example();
        r.encode(System.out);
        NymReply b = example(r);
        b.encode(System.out);
    }

    protected static void readWrite()
        throws Exception
    {
        NymRequest r = null;
        r = new NymRequest(System.in);
        r.encode(System.out);

        NymReply b = null;
        b = new NymReply(r, System.in);
        System.err.println("Write: " + b);
        b.encode(System.out);
    }

    protected static void input()
        throws Exception
    {
        NymRequest r = null;
        r = new NymRequest(System.in);

        NymReply b = null;
        b = new NymReply(r, System.in);
        System.err.println("Read: " + b);
    }
}



1.1                  java/webfunds/sox/NymRequest.java

Index: NymRequest.java
===================================================================
/* $Id: NymRequest.java,v 1.1 2000/10/14 01:52:22 iang Exp $
 *
 * Copyright (c) Systemics Inc. 1995-2000 on behalf of
 * The WebFunds Development Team. All Rights Reserved.
 */
package webfunds.sox;

import java.io.*;
import java.security.*;
import java.security.cert.Certificate;



/**
 *  A Request to set or notify the server of application level details
 *  pertaining to the registered key used.
 *  Could be used for identity management.
 */
public class NymRequest
    extends Request
{
    /**
     * The version of the encoded request.
     */
    public static final int VERSION = 2;
    protected int version = VERSION;         // change this if an old packet

    public static final String name      = "SOX.Nym";
    public              String getName() { return name ; }



    public String[] names;

    /**
     *  Rather than predict what information is required, just use
     *  an array of strings.  Some conventions:
     *
     *       0.  short, printable PUBLISHABLE nickname
     *       1.  email address
     *
     *  Fields can be empty (null or empty, it is the same with SOX).
     *  Any other definitions can be handled at the application layer.
     */
    public String[]  getNames()                { return names; }

    /**
     *  Set the names.
     *  The array is copied, and all nulls converted to empties.
     *  Note that the strings are really treated as byte arrays.
     */
    public void      setNames(String[] n)
    {
        if (n == null)
            n = new String[0];

        /*
         *  copy our own array, and make the nulls into empty strings
         *  makes code and comparison easier later
         */
        int num = n.length;
        this.names = new String[num];
        for (int i = 0; i < num; i++)
            this.names[i] = (n[i] == null) ? "" : n[i];
    }

    private long flags;
    /**
     *  Rather than predict what information is required, just use
     *  a big long for flags.
     *  Any definitions can be handled at the application layer.
     */
    public long      getFlags()                { return flags; }
    public void      setFlags(long flags)      { this.flags = flags; }



    /**
     * Get a Reply with Errors set.
     */
    public Reply errorReply(int e)
                           { return new NymReply(this, e); }
    public NymReply goodReply(String[] names, long flags)
                           { return new NymReply(this, names, flags); }

    /** Get the Reply returned for this request */
    public Reply reconstructReply(byte[] b)
        throws SOXPacketException
    {
        NymReply reply = new NymReply(this, b);
        return (Reply)reply;
    }




    public NymRequest(String requestId, AccountId acc)
        throws SOXKeyException
    {
        super(requestId, acc);

        this.names = new String[0];
        this.flags = 0;
    }

    public NymRequest(byte[] data)
        throws SOXPacketException                 { super(data); }

    public NymRequest(InputStream is)
        throws SOXPacketException                 { super(is); }

    public void encode(OutputStream os)
        throws IOException
    {
        DataOutputStream dos = new DataOutputStream(os);
        super.encode(dos);

        dos.writeByte(version);

        dos.writeLong(flags);

        int num = this.names.length;
        dos.writeByte(num);
        for (int i = 0; i < num; i++)
            writeString(dos, this.names[i]);
    }


    public void decode(InputStream is)
        throws IOException, SOXPacketException
    {
        DataInputStream dis = new DataInputStream(is);
        super.decode(dis);

        int v = dis.readUnsignedByte();

        if (v != VERSION)
            throw new SOXPacketException("Invalid version number in IdR:" +
                        v + " != " + VERSION);
        version = v;         // remember this for sig checks

        flags = dis.readLong();

        int num = dis.readUnsignedByte();
        if (num > 100)
            throw new SOXPacketException("number of strings is odd: " + num);
        this.names = new String[num];
        for (int i = 0; i < num; i++)
            this.names[i] = new String(readString(dis));
    }




////// Self-Test //////////////////////////////////

    public String toString()
    {
        String s = "SOX NymRequest V" + version + " ";
        s += super.toString();
        s += "\tflags: " + flags + "\t\tnum: " + this.names.length;
        for (int i = 0; i < this.names.length; i++)
            s += "\n\t" + i + ": " + this.names[i];
        return s;
    }

    public boolean equals(java.lang.Object obj)
    {
        if (!(obj instanceof NymRequest))
            return false;

//System.err.println("1");
        if (!super.equals((Request) obj))
            return false ;

//System.err.println("3");
        NymRequest other = (NymRequest)obj;

        if (flags != other.flags)
            return false;

//System.err.println("6");
        int me = this.names.length;
        if (me != other.names.length)
            return false;

        String[] hers = other.names;
        for (int i = 0; i < me; i++)
             if (!this.names[i].equals(hers[i]))
                 return false;

//System.err.println("9");
        return true;
    }

    public static NymRequest example()
    {

        long flags = Utils.exampleLong();
        int len = 0x07 & Utils.exampleByte();

        String[] s = new String[len];
        for (int i = 0; i < len; i++)
             s[i] = Utils.exampleString();

        String rid = webfunds.utils.Hex.data2hex(Utils.exampleData(len));
     
        AccountId id = AccountId.example();

        NymRequest example = null;
        try {
            example = new NymRequest(rid, id);
        } catch(SOXKeyException e) {
            e.printStackTrace();
            throw new RuntimeException("new IdReq: " + e);
        }

        example.setFlags(flags);
        example.setNames(s);

        return example;
    }

    public static void main(String[] args)
    {
        int num = 2000;
        String type = "-c";
        if (args.length > 0)
        {
            int a = 0;
            if (args[a].startsWith("-"))
            {
                type = args[a++];
            }

            if (a < args.length)
            {
                Integer i = null;
                try {
                    i = new Integer(args[a]);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
                num = i.intValue();
            }
        }

        try {
            if (type.equals("-t") || type.equals("-i"))
            {
                while (true)
                {
                    if (type.equals("-t"))
                        readWrite();
                    else if (type.equals("-i"))
                        input();
                }
            }
            else
            {
                for (int i = 0; i < num; i++)
                {
                    if (type.equals("-c"))
                        cycle();
                    else if (type.equals("-o"))
                        output();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.out.flush();          // last buffered lump still there
        System.out.close();
        System.exit(0);
    }

    protected static void cycle()
        throws Exception
    {
        NymRequest p = example();
        System.err.println("Writing: " + p);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        p.encode(baos);
        byte[] buf = baos.toByteArray();

        NymRequest q = new NymRequest(buf);
        if (!p.equals(q))
        {
            throw new RuntimeException("FAILED:\n\n"+q+"\n\n"+p+"\nEND\n");
        }
    }

    protected static void output()
        throws Exception
    {
        NymRequest b = example();
        b.encode(System.out);
    }

    protected static void readWrite()
        throws Exception
    {
        NymRequest b = null;
        b = new NymRequest(System.in);
        System.err.println("Read: " + b);
        b.encode(System.out);
    }

    protected static void input()
        throws Exception
    {
        NymRequest b = null;
        b = new NymRequest(System.in);
        System.err.println("Read: " + b);
    }
}



1.1                  java/webfunds/sox/SOXRequestException.java

Index: SOXRequestException.java
===================================================================
/*
 * $Id: SOXRequestException.java,v 1.1 2000/10/14 01:52:22 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.sox;


/**
 * This exception class is thrown when a request fails
 * and the reason is an application error - user to deal with it.
 * The number is set to the SOX error number,
 * and the message is the message returned in the reply.
 */
public class SOXRequestException
    extends SOXException
{

    public SOXRequestException(String msg)            { super(UNKNOWN, msg); }
    public SOXRequestException(int errno, String msg) { super(errno, msg); }
    public SOXRequestException(int errno)             { super(errno, ""); }
}