read_any() public method

public read_any ( ) : OtpErlangObject
return OtpErlangObject
Example #1
0
        /**
         * Create a list from a stream containing an list encoded in Erlang external
         * format.
         *
         * @param buf
         *            the stream containing the encoded list.
         *
         * @exception OtpErlangDecodeException
         *                if the buffer does not contain a valid external
         *                representation of an Erlang list.
         */
        public OtpErlangList(OtpInputStream buf)
        {
            int arity = buf.read_list_head();

            if (arity > 0)
            {
                elems = new OtpErlangObject[arity];
                for (int i = 0; i < arity; i++)
                {
                    elems[i] = buf.read_any();
                }
                /* discard the terminating nil (empty list) or read tail */
                if (buf.peek1() == OtpExternal.nilTag)
                {
                    buf.read_nil();
                }
                else
                {
                    lastTail = buf.read_any();
                }
            }
            else
            {
                elems = NO_ELEMENTS;
            }
        }
Example #2
0
 private void ListenActivity()
 {
     EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
     for (;;)
     {
         byte[] message = new byte[BUF];
         while (this.socket.Available == 0) { }
         int size = this.socket.ReceiveFrom(message, ref remote);
         if (size == 0) continue;
         Debug.Print("Received " + size + " bytes from " + ((IPEndPoint)remote).Address + " (" + ((IPEndPoint)remote).Port + ")");
         string messageString = "";
         for (int i = 0; i < size; ++i)
         {
             messageString += (int)message[i] + " ";
         }
         Debug.Print(messageString);
         OtpInputStream inStream = new OtpInputStream(message);
         OtpErlangObject msg = inStream.read_any();
         Debug.Print(msg.ToString());
         OtpErlangTuple t = new OtpErlangTuple(new OtpErlangObject[] { new OtpErlangAtom("ok"), msg });
         OtpOutputStream outStream = new OtpOutputStream(t, false);
         byte[] answer = outStream.GetBuffer();
         this.socket.SendTo(answer, remote);
         Debug.Print(t.ToString());
     }
 }
Example #3
0
 /**
  * <p>
  * Deserialize and return a new copy of the message contained in this
  * OtpMsg.
  * </p>
  *
  * <p>
  * The first time this method is called the actual payload is deserialized
  * and the Erlang term is created. Calling this method subsequent times will
  * not cuase the message to be deserialized additional times, instead the
  * same Erlang term object will be returned.
  * </p>
  *
  * @return an Erlang term.
  *
  * @exception OtpErlangDecodeException
  *                    if the byte stream could not be deserialized.
  *
  */
 public OtpErlangObject getMsg()
 {
     if (payload == null)
     {
         payload = paybuf.read_any();
     }
     return(payload);
 }
Example #4
0
        /**
         * Create a tuple from a stream containing an tuple encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded tuple.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang tuple.
         */
        public OtpErlangTuple(OtpInputStream buf)
        {
            int arity = buf.read_tuple_head();

            if (arity > 0)
            {
                elems = new OtpErlangObject[arity];

                for (int i = 0; i < arity; i++)
                {
                    elems[i] = buf.read_any();
                }
            }
            else
            {
                elems = NO_ELEMENTS;
            }
        }
Example #5
0
        /**
         * Read a compressed term from the stream
         *
         * @return the resulting uncompressed term.
         *
         * @exception OtpErlangDecodeException
         *                if the next term in the stream is not a compressed term.
         */
        public OtpErlangObject read_compressed()
        {
            int tag = read1skip_version();

            if (tag != OtpExternal.compressedTag)
            {
                throw new OtpErlangDecodeException("Wrong tag encountered, expected "
                                                   + OtpExternal.compressedTag + ", got " + tag);
            }

            int size = read4BE();

            byte[]        buf = new byte[size];
            DeflateStream dos = new DeflateStream(this, CompressionMode.Decompress, true);

            try
            {
                int dsize = dos.Read(buf, 0, size);
                if (dsize != size)
                {
                    throw new OtpErlangDecodeException("Decompression gave " + dsize + " bytes, not " + size);
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw;
            }
            catch (InvalidDataException e)
            {
                throw new OtpErlangDecodeException(e.Message);
            }

            OtpInputStream ois = new OtpInputStream(buf, flags);

            return(ois.read_any());
        }
Example #6
0
        /**
         * Create a tuple from a stream containing an tuple encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded tuple.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang tuple.
         */
        public OtpErlangTuple(OtpInputStream buf)
        {
            int arity = buf.read_tuple_head();

            if (arity > 0)
            {
                elems = new OtpErlangObject[arity];

                for (int i = 0; i < arity; i++)
                {
                    elems[i] = buf.read_any();
                }
            }
            else
            {
                elems = NO_ELEMENTS;
            }
        }
        /**
         * Read a compressed term from the stream
         *
         * @return the resulting uncompressed term.
         *
         * @exception OtpErlangDecodeException
         *                if the next term in the stream is not a compressed term.
         */
        public OtpErlangObject read_compressed()
        {
            int tag = read1skip_version();

            if (tag != OtpExternal.compressedTag)
            {
                throw new OtpErlangDecodeException("Wrong tag encountered, expected "
                                   + OtpExternal.compressedTag + ", got " + tag);
            }

            int size = read4BE();
            byte[] buf = new byte[size];
            DeflateStream dos = new DeflateStream(this, CompressionMode.Decompress, true);
            try
            {
                int dsize = dos.Read(buf, 0, size);
                if (dsize != size)
                {
                    throw new OtpErlangDecodeException("Decompression gave " + dsize + " bytes, not " + size);
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw;
            }
            catch (InvalidDataException e)
            {
                throw new OtpErlangDecodeException(e.Message);
            }

            OtpInputStream ois = new OtpInputStream(buf, flags);
            return ois.read_any();
        }
 /**
  * Read binary data in the Erlang external format, and produce a
  * corresponding Erlang data type object. This method is normally used when
  * Erlang terms are received in messages, however it can also be used for
  * reading terms from disk.
  *
  * @param buf
  *                an input stream containing one or more encoded Erlang
  *                terms.
  *
  * @return an object representing one of the Erlang data types.
  *
  * @exception OtpErlangDecodeException
  *                    if the stream does not contain a valid representation
  *                    of an Erlang term.
  */
 public static OtpErlangObject decode(OtpInputStream buf)
 {
     return buf.read_any();
 }
Example #9
0
 /**
  * Create a list from a stream containing an list encoded in Erlang external
  * format.
  *
  * @param buf
  *            the stream containing the encoded list.
  *
  * @exception OtpErlangDecodeException
  *                if the buffer does not contain a valid external
  *                representation of an Erlang list.
  */
 public OtpErlangList(OtpInputStream buf)
 {
     int arity = buf.read_list_head();
     if (arity > 0)
     {
         elems = new OtpErlangObject[arity];
         for (int i = 0; i < arity; i++)
         {
             elems[i] = buf.read_any();
         }
         /* discard the terminating nil (empty list) or read tail */
         if (buf.peek1() == OtpExternal.nilTag)
         {
             buf.read_nil();
         }
         else
         {
             lastTail = buf.read_any();
         }
     }
     else
     {
         elems = NO_ELEMENTS;
     }
 }
        public override void run()
        {
            if (!connected)
            {
                deliver(new IOException("Not connected"));
                return;
            }

            byte[] lbuf = new byte[4];
            OtpInputStream ibuf;
            OtpErlangObject traceobj;
            int len;
            byte[] tock = { 0, 0, 0, 0 };

            try
            {
            receive_loop:
                while (!done)
                {
                    // don't return until we get a real message
                    // or a failure of some kind (e.g. EXIT)
                    // read length and read buffer must be atomic!
                    do
                    {
                        // read 4 bytes - get length of incoming packet
                        // socket.getInputStream().read(lbuf);
                        readSock(socket, lbuf);
                        ibuf = new OtpInputStream(lbuf, flags);
                        len = ibuf.read4BE();

                        // received tick? send tock!
                        if (len == 0)
                        {
                            lock (this)
                            {
                                socket.GetOutputStream().Write(tock, 0, tock.Length);
                                socket.GetOutputStream().Flush();
                            }
                        }

                    } while (len == 0); // tick_loop

                    // got a real message (maybe) - read len bytes
                    byte[] tmpbuf = new byte[len];
                    // i = socket.getInputStream().read(tmpbuf);
                    readSock(socket, tmpbuf);
                    ibuf = new OtpInputStream(tmpbuf, flags);

                    if (ibuf.read1() != passThrough)
                    {
                        goto receive_loop;
                    }

                    // got a real message (really)
                    OtpErlangObject reason = null;
                    OtpErlangAtom cookie = null;
                    OtpErlangObject tmp = null;
                    OtpErlangTuple head = null;
                    OtpErlangAtom toName;
                    OtpErlangPid to;
                    OtpErlangPid from;
                    int tag;

                    // decode the header
                    tmp = ibuf.read_any();
                    if (!(tmp is OtpErlangTuple))
                    {
                        goto receive_loop;
                    }

                    head = (OtpErlangTuple)tmp;
                    if (!(head.elementAt(0) is OtpErlangLong))
                    {
                        goto receive_loop;
                    }

                    // lets see what kind of message this is
                    tag = (int)((OtpErlangLong)head.elementAt(0)).longValue();

                    switch (tag)
                    {
                        case sendTag: // { SEND, Cookie, ToPid }
                        case sendTTTag: // { SEND, Cookie, ToPid, TraceToken }
                            if (!cookieOk)
                            {
                                // we only check this once, he can send us bad cookies
                                // later if he likes
                                if (!(head.elementAt(1) is OtpErlangAtom))
                                {
                                    goto receive_loop;
                                }
                                cookie = (OtpErlangAtom)head.elementAt(1);
                                if (sendCookie)
                                {
                                    if (!cookie.atomValue().Equals(self.Cookie))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                else
                                {
                                    if (!cookie.atomValue().Equals(""))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                cookieOk = true;
                            }

                            if (traceLevel >= sendThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);

                                /* show received payload too */
                                ibuf.Mark(0);
                                traceobj = ibuf.read_any();

                                if (traceobj != null)
                                {
                                    log.Debug("   " + traceobj);
                                }
                                else
                                {
                                    log.Debug("   (null)");
                                }
                                ibuf.Reset();
                            }

                            to = (OtpErlangPid)head.elementAt(2);

                            deliver(new OtpMsg(to, ibuf));
                            break;

                        case regSendTag: // { REG_SEND, FromPid, Cookie, ToName }
                        case regSendTTTag: // { REG_SEND, FromPid, Cookie, ToName,
                            // TraceToken }
                            if (!cookieOk)
                            {
                                // we only check this once, he can send us bad cookies
                                // later if he likes
                                if (!(head.elementAt(2) is OtpErlangAtom))
                                {
                                    goto receive_loop;
                                }
                                cookie = (OtpErlangAtom)head.elementAt(2);
                                if (sendCookie)
                                {
                                    if (!cookie.atomValue().Equals(self.Cookie))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                else
                                {
                                    if (!cookie.atomValue().Equals(""))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                cookieOk = true;
                            }

                            if (traceLevel >= sendThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);

                                /* show received payload too */
                                ibuf.Mark(0);
                                traceobj = ibuf.read_any();

                                if (traceobj != null)
                                {
                                    log.Debug("   " + traceobj);
                                }
                                else
                                {
                                    log.Debug("   (null)");
                                }
                                ibuf.Reset();
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            toName = (OtpErlangAtom)head.elementAt(3);

                            deliver(new OtpMsg(from, toName.atomValue(), ibuf));
                            break;

                        case exitTag: // { EXIT, FromPid, ToPid, Reason }
                        case exit2Tag: // { EXIT2, FromPid, ToPid, Reason }
                            if (head.elementAt(3) == null)
                            {
                                goto receive_loop;
                            }
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);
                            reason = head.elementAt(3);

                            deliver(new OtpMsg(tag, from, to, reason));
                            break;

                        case exitTTTag: // { EXIT, FromPid, ToPid, TraceToken, Reason }
                        case exit2TTTag: // { EXIT2, FromPid, ToPid, TraceToken,
                            // Reason
                            // }
                            // as above, but bifferent element number
                            if (head.elementAt(4) == null)
                            {
                                goto receive_loop;
                            }
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);
                            reason = head.elementAt(4);

                            deliver(new OtpMsg(tag, from, to, reason));
                            break;

                        case linkTag: // { LINK, FromPid, ToPid}
                        case unlinkTag: // { UNLINK, FromPid, ToPid}
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);

                            deliver(new OtpMsg(tag, from, to));
                            break;

                        // absolutely no idea what to do with these, so we ignore
                        // them...
                        case groupLeaderTag: // { GROUPLEADER, FromPid, ToPid}
                        case nodeLinkTag: // { NODELINK }
                            // (just show trace)
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }
                            break;

                        default:
                            // garbage?
                            goto receive_loop;
                    }
                } // end receive_loop

                // this section reachable only with break
                // we have received garbage from peer
                deliver(new OtpErlangExit("Remote is sending garbage"));

            } // try

            catch (OtpAuthException e)
            {
                deliver(e);
            }
            catch (OtpErlangDecodeException)
            {
                deliver(new OtpErlangExit("Remote is sending garbage"));
            }
            catch (IOException)
            {
                deliver(new OtpErlangExit("Remote has closed connection"));
            }
            finally
            {
                close();
            }
        }
Example #11
0
 /**
  * Read binary data in the Erlang external format, and produce a
  * corresponding Erlang data type object. This method is normally used when
  * Erlang terms are received in messages, however it can also be used for
  * reading terms from disk.
  *
  * @param buf
  *                an input stream containing one or more encoded Erlang
  *                terms.
  *
  * @return an object representing one of the Erlang data types.
  *
  * @exception OtpErlangDecodeException
  *                    if the stream does not contain a valid representation
  *                    of an Erlang term.
  */
 public static OtpErlangObject decode(OtpInputStream buf)
 {
     return(buf.read_any());
 }