package nanomsg;

import java.util.HashMap;
import java.util.Map;

import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import nanomsg.NativeLibrary;


public final class Nanomsg {
    public static final int NN_HAUSNUMERO = 156384712;
    public static final int ECONNREFUSED = NN_HAUSNUMERO + 7;
    public static final int EAFNOSUPPORT = NN_HAUSNUMERO + 10;
    public static final int ETERM = NN_HAUSNUMERO + 53;
    public static final int EFSM = NN_HAUSNUMERO + 54;
    public static final int EAGAIN = (NN_HAUSNUMERO + 12);
    public static final int NN_DONTWAIT = 1;
    public static final int NN_MSG = -1;
    public static final int AF_SP = 1;
    public static final int AF_SP_RAW = 2;

    /* PubSub */
    public static final int NN_PROTO_PUBSUB = 2;
    public static final int NN_PUB = NN_PROTO_PUBSUB * 16 + 0;
    public static final int NN_SUB = NN_PROTO_PUBSUB * 16 + 1;
    public static final int NN_SUB_SUBSCRIBE = 1;
    public static final int NN_SUB_UNSUBSCRIBE = 2;

    /* ReqRep */
    public static final int NN_PROTO_REQREP = 3;
    public static final int NN_REQ = NN_PROTO_REQREP * 16 + 0;
    public static final int NN_REP = NN_PROTO_REQREP * 16 + 1;
    public static final int NN_REQ_RESEND_IVL = 1;

    /* Pair */
    public static final int NN_PROTO_PAIR = 1;
    public static final int NN_PAIR = NN_PROTO_PAIR * 16 + 0;

    /* Pipeline */
    public static final int NN_PROTO_PIPELINE = 5;
    public static final int NN_PUSH = NN_PROTO_PIPELINE * 16 + 0;
    public static final int NN_PULL = NN_PROTO_PIPELINE * 16 + 1;

    /* Bus */
    public static final int NN_PROTO_BUS = 7;
    public static final int NN_BUS = NN_PROTO_BUS * 16 + 0;

    /* Low level native interface wrapper */

    public static final int getErrorNumber() {
        return NativeLibrary.nn_errno();
    }

    public static final String getError() {
        final int currentError = Nanomsg.getErrorNumber();
        return NativeLibrary.nn_strerror(currentError);
    }

    public static final Map<String, Integer> getSymbols() throws InterruptedException {
        HashMap<String, Integer> result = new HashMap<String, Integer>();

        try {
            int index = 0;
            while (true) {
                IntByReference valueRef = new IntByReference();
                Pointer ptr = NativeLibrary.nn_symbol(index, valueRef);

                if (ptr.equals(Pointer.NULL)) {
                    break;
                }

                result.put(ptr.getString(0), valueRef.getValue());
                index += 1;
            }

        } catch (NullPointerException e) {
            /* Do nothing because it raided when stops. */
        }

        return result;
    }
}

@@ -189,16 +189,49 @@
 nomsg %7B%0A
+    public class constants %7B%0A    
     publ
@@ -277,16 +277,20 @@
 384712;%0A
+    
     publ
@@ -339,32 +339,36 @@
 NUMERO + 7;%0A    
+   
+ 
 public static fi
@@ -410,16 +410,20 @@
 O + 10;%0A
+    
     publ
@@ -470,16 +470,20 @@
 O + 53;%0A
+    
     publ
@@ -529,24 +529,28 @@
 O + 54;%0A    
+ 
+   
 public stati
@@ -592,16 +592,20 @@
  + 12);%0A
+    
     publ
@@ -637,32 +637,36 @@
 NTWAIT = 1;%0A    
+   
+ 
 public static fi
@@ -686,16 +686,20 @@
 G = -1;%0A
+    
     publ
@@ -725,24 +725,28 @@
  AF_SP = 1;%0A
+    
     public s
@@ -777,24 +777,28 @@
 W = 2;%0A%0A    
+ 
+   
 /* PubSub */
@@ -794,24 +794,28 @@
 * PubSub */%0A
+    
     public s
@@ -851,24 +851,28 @@
 UB = 2;%0A    
+   
+ 
 public stati
@@ -910,32 +910,36 @@
 UBSUB * 16 + 0;%0A
+    
     public stati
@@ -977,32 +977,36 @@
 UBSUB * 16 + 1;%0A
+    
     public stati
@@ -1035,32 +1035,36 @@
 SCRIBE = 1;%0A    
+ 
+   
 public static fi
@@ -1092,24 +1092,28 @@
 CRIBE = 2;%0A%0A
+    
     /* ReqRe
@@ -1113,32 +1113,36 @@
 * ReqRep */%0A    
+   
+ 
 public static fi
@@ -1170,16 +1170,20 @@
 EP = 3;%0A
+    
     publ
@@ -1229,32 +1229,36 @@
 EQREP * 16 + 0;%0A
+    
     public stati
@@ -1300,32 +1300,36 @@
 P * 16 + 1;%0A    
+ 
+   
 public static fi
@@ -1356,24 +1356,28 @@
 D_IVL = 1;%0A%0A
+    
     /* Pair 
@@ -1375,32 +1375,36 @@
  /* Pair */%0A    
+   
+ 
 public static fi
@@ -1426,24 +1426,28 @@
 O_PAIR = 1;%0A
+    
     public s
@@ -1493,24 +1493,28 @@
  * 16 + 0;%0A%0A
+    
     /* Pipel
@@ -1516,32 +1516,36 @@
 Pipeline */%0A    
+ 
+   
 public static fi
@@ -1575,16 +1575,20 @@
 NE = 5;%0A
+    
     publ
@@ -1645,24 +1645,28 @@
 16 + 0;%0A    
+   
+ 
 public stati
@@ -1712,24 +1712,28 @@
  * 16 + 1;%0A%0A
+    
     /* Bus *
@@ -1730,24 +1730,28 @@
   /* Bus */%0A
+    
     public s
@@ -1784,24 +1784,28 @@
 US = 7;%0A    
+ 
+   
 public stati
@@ -1843,24 +1843,30 @@
 US * 16 + 0;
+%0A    %7D
 %0A%0A    /* Low

Move constants defined at Nanomsg class to inner constants class.