Example #1
0
        static void Main()
        {
            MessageIOGateway gw = new MessageIOGateway();
              Client.MessageDecoder d = new Client.MessageDecoder();
              Client.MessageEncoder e = new Client.MessageEncoder();

              Message m = new Message();

              m.what = PR_COMMAND_GETDATA;
              string text = "*";
              m.setString(PR_NAME_KEYS, text);

              e.Encode(m);

              byte [] buffer = e.GetAndResetBuffer();

              long start = DateTime.Now.Ticks;

              for (int i = 0; i < MESSAGE_COUNT; ++i) {
            d.Decode(buffer, buffer.Length);
              }

              long end = DateTime.Now.Ticks;

              long elapsed = end - start;

              DateTime t = new DateTime(elapsed);

              Console.WriteLine("Elapsed time: " + t.ToString());
              Console.WriteLine("Messages flattened and unflattened: " + MESSAGE_COUNT.ToString());
        }
Example #2
0
        public ExampleClient(string hostName, int port)
        {
            Client client = new Client(hostName, port);

              client.RegisterForMessages(new MessagesCallback(MessagesCallback), null);
              client.RegisterForDisconnect(new DisconnectCallback(DisconnectCallback),
                   null);

              client.BeginConnect(new AsyncCallback(this.ConnectCallback), client);

              Message m1 = new Message();

              m1.what = StorageReflectConstants.PR_COMMAND_SETPARAMETERS;
              m1.setBoolean("SUBSCRIBE:*", true);
              m1.setBoolean("SUBSCRIBE:*/*", true);
              m1.setBoolean("SUBSCRIBE:*/*/*", true);
              m1.setBoolean("SUBSCRIBE:*/*/*/*", true);
              m1.setBoolean("SUBSCRIBE:*/*/*/*/*", true);

              client.Send(m1);

              while (true) {
            Thread.Sleep(5000);
              }
        }
Example #3
0
        public void MessagesCallback(Message [] messages,
				 Client client, 
				 object state)
        {
            foreach (Message message in messages) {
            System.Console.WriteLine(message.ToString());
              }
        }
Example #4
0
        /// <summary>
        /// Converts the given Message into bytes and sends it out the stream.
        /// </summary>
        /// <param name="outputStream">the data stream to send the converted bytes
        /// </param>
        /// <param name="msg">the message to convert</param>
        /// <exception cref="IOException"/>
        ///
        public void flattenMessage(Stream outputStream, Message msg)
        {
            int flattenedSize = msg.flattenedSize();

              if (flattenedSize >= 32 &&
              _encoding >= MUSCLE_MESSAGE_ENCODING_ZLIB_1 &&
              _encoding <= MUSCLE_MESSAGE_ENCODING_ZLIB_9) {

            // currently do not compress outgoing messages
            // do later
            BinaryWriter writer = new BinaryWriter(outputStream);
            writer.Write((int) flattenedSize);
            writer.Write((int) MessageIOGateway.MUSCLE_MESSAGE_ENCODING_DEFAULT);
            msg.flatten(writer);
            writer.Flush();
              }
              else {
            BinaryWriter writer = new BinaryWriter(outputStream);
            writer.Write((int) flattenedSize);
            writer.Write((int) MessageIOGateway.MUSCLE_MESSAGE_ENCODING_DEFAULT);
            msg.flatten(writer);
            writer.Flush();
              }
        }
Example #5
0
 /// Sets the given field name to contain a single Message value.  
 /// Any previous field contents are replaced. 
 /// Note that the neither the array nor the Message objects are copied;
 /// rather both the array and the Messages become part of this Message.
 /// <param name="name"/>
 /// <param name="val/>
 public void setMessages(string name, Message [] vals)
 {
     setObjects(name, B_MESSAGE_TYPE, vals, vals.Length);
 }
Example #6
0
 /// Sets the given field name to contain a single Message value.  
 /// Any previous field contents are replaced. 
 /// Note that a copy of (val) is NOT made; the passed-in mesage 
 /// object becomes part of this Message.
 ///
 /// <param name="name"/>
 /// <param name="val/>
 public void setMessage(string name, Message val)
 {
     MessageField field = getCreateOrReplaceField(name, B_MESSAGE_TYPE);
     Message [] array = (Message[]) field.getData();
     if ((array == null)||(field.size() != 1))
       array = new Message[1];
     array[0] = val;
     field.setPayload(array, 1);
 }
Example #7
0
        /// Sets the given field name to contain the given Flattenable values.
        /// Any previous field contents are replaced.
        /// Note that if the objects are Messages, Points, or Rects, 
        /// they will be cloned rather than flattened.
        /// <param name="name"/>
        /// <param name="val">
        /// Array of Flattenable objects to assign to the field.  
        /// The objects are all flattened and
        /// the flattened data is put into the Message; 
        /// the objects themselves do not become part of the message.
        /// </param>
        public void setFlats(string name, Flattenable [] vals)
        {
            int type = vals[0].typeCode();
            int len = vals.Length;
            MessageField field = getCreateOrReplaceField(name, type);

            // For these types, we have explicit support for holding
            // the objects in memory, so we'll just clone them
            switch(type) {
            case B_MESSAGE_TYPE:
              {
            Message [] array = new Message[len];
            for (int i=0; i<len; i++)
              array[i] = (Message) vals[i].cloneFlat();
            field.setPayload(array, len);
              }
              break;

            case B_POINT_TYPE:
              {
            Point [] array = new Point[len];
            for (int i=0; i<len; i++)
              array[i] = (Point) vals[i].cloneFlat();
            field.setPayload(array, len);

              }
              break;

            case B_RECT_TYPE:
              {
            Rect [] array = new Rect[len];
            for (int i=0; i<len; i++)
              array[i] = (Rect) vals[i].cloneFlat();
            field.setPayload(array, len);
              }
              break;

            default:
              {
            byte [][] array = (byte[][]) field.getData();
            if ((array == null)||(field.size() != len))
              array = new byte[len][];

            for (int i=0; i<len; i++) array[i] =
              flattenToArray(vals[i], array[i]);
            field.setPayload(array, len);
              }
              break;

            }
        }
Example #8
0
 /// Copy Constructor.
 /// <param name="copyMe">
 /// the Message to make us a (wholly independant) copy.
 /// </param>
 public Message(Message copyMe)
 {
     setEqualTo(copyMe);
 }
Example #9
0
 /// Take the data under (name) in this message, and moves it 
 /// into (moveTo).
 /// <exception cref="FieldNotFound"/>
 public void moveField(string name, Message moveTo)
 {
     MessageField field = getField(name);
     _fieldTable.Remove(name);
     moveTo.ensureFieldTableAllocated();
     moveTo._fieldTable.Add(name, field);
 }
Example #10
0
            /// Restores our state from the given stream.
            /// Assumes that our _type is already set correctly.
            /// <exception cref="IOException"/>
            ///
            public override void unflatten(BinaryReader reader, int numBytes)
            {
                // For fixed-size types, calculating the number of items
                // to read is easy...
                int flattenedCount = flattenedItemSize();

                if (flattenedCount > 0)
                  _numItems = numBytes / flattenedCount;

                switch(_type)
                  {
                  case B_BOOL_TYPE:
                {
                  bool [] array = new bool[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = (reader.ReadByte() > 0) ? true : false;
                  _payload = array;
                }
                break;

                  case B_INT8_TYPE:
                {
                  byte [] array = reader.ReadBytes(_numItems);
                  _payload = array;
                }
                break;

                  case B_INT16_TYPE:
                {
                  short [] array = new short[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = reader.ReadInt16();
                  _payload = array;
                }
                break;

                  case B_FLOAT_TYPE:
                {
                  float [] array = new float[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = reader.ReadSingle();
                  _payload = array;
                }
                break;

                  case B_INT32_TYPE:
                {
                  int [] array = new int[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = reader.ReadInt32();
                  _payload = array;
                }
                break;

                  case B_INT64_TYPE:
                {
                  long [] array = new long[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = reader.ReadInt64();
                  _payload = array;
                }
                break;

                  case B_DOUBLE_TYPE:
                {
                  double [] array = new double[_numItems];
                  for (int i=0; i<_numItems; i++)
                array[i] = reader.ReadDouble();
                  _payload = array;
                }
                break;

                  case B_POINT_TYPE:
                {
                  Point [] array = new Point[_numItems];
                  for (int i=0; i<_numItems; i++)  {
                Point p = array[i] = new Point();
                p.unflatten(reader, p.flattenedSize());
                  }
                  _payload = array;
                }
                break;

                  case B_RECT_TYPE:
                {
                  Rect [] array = new Rect[_numItems];
                  for (int i=0; i<_numItems; i++)  {
                Rect r = array[i] = new Rect();
                r.unflatten(reader, r.flattenedSize());
                  }
                  _payload = array;
                }
                break;

                  case B_MESSAGE_TYPE:
                {
                  ArrayList temp = new ArrayList();
                  while(numBytes > 0) {
                Message subMessage = new Message();
                int subMessageSize = reader.ReadInt32();
                subMessage.unflatten(reader, subMessageSize);
                temp.Add(subMessage);
                numBytes -= (subMessageSize + 4);  // 4 for the size int
                  }
                  _numItems = temp.Count;
                  Message [] array = new Message[_numItems];
                  for (int j=0; j<_numItems; j++)
                array[j] = (Message) temp[j];
                  _payload = array;
                }

                break;

                  case B_STRING_TYPE:
                {
                  Decoder d = Encoding.UTF8.GetDecoder();

                  _numItems = reader.ReadInt32();
                  string [] array = new string[_numItems];

                  byte [] byteArray = null;
                  char [] charArray = null;
                  for (int i=0; i<_numItems; i++) {
                int nextStringLen = reader.ReadInt32();
                byteArray = reader.ReadBytes(nextStringLen);

                int charsRequired = d.GetCharCount(byteArray,
                               0,
                               nextStringLen);

                if (charArray == null || charArray.Length < charsRequired)
                  charArray = new char[charsRequired];

                int charsDecoded = d.GetChars(byteArray,
                          0,
                          byteArray.Length,
                          charArray,
                          0);

                array[i] = new string(charArray, 0, charsDecoded - 1);
                  }
                  _payload = array;
                }
                break;

                  default:
                {
                  _numItems = reader.ReadInt32();
                  byte [][] array = new byte[_numItems][];
                  for (int i=0; i<_numItems; i++) {
                int length = reader.ReadInt32();
                array[i] = new byte[length];
                array[i] = reader.ReadBytes(length);
                  }
                  _payload = array;
                }
                break;
                  }
            }
Example #11
0
            /// Makes an independent clone of this field object 
            /// (a bit expensive)
            public override Flattenable cloneFlat()
            {
                MessageField clone = new MessageField(_type);
                System.Array newArray;  // this will be a copy of our data array
                switch(_type)
                  {
                  case B_BOOL_TYPE:    newArray = new bool[_numItems]; break;
                  case B_INT8_TYPE:    newArray = new byte[_numItems];    break;
                  case B_INT16_TYPE:   newArray = new short[_numItems];   break;
                  case B_FLOAT_TYPE:   newArray = new float[_numItems];   break;
                  case B_INT32_TYPE:   newArray = new int[_numItems];     break;
                  case B_INT64_TYPE:   newArray = new long[_numItems];    break;
                  case B_DOUBLE_TYPE:  newArray = new double[_numItems];  break;
                  case B_STRING_TYPE:  newArray = new string[_numItems];  break;
                  case B_POINT_TYPE:   newArray = new Point[_numItems];   break;
                  case B_RECT_TYPE:    newArray = new Rect[_numItems];    break;
                  case B_MESSAGE_TYPE: newArray = new Message[_numItems]; break;
                  default:             newArray = new byte[_numItems][];  break;
                  }

                newArray = (System.Array) _payload.Clone();

                // If the contents of newArray are modifiable, we need to
                // clone the contents also
                switch(_type)
                  {
                  case B_POINT_TYPE:
                {
                  Point [] points = (Point[]) newArray;
                  for (int i=0; i<_numItems; i++)
                points[i] = (Point) points[i].cloneFlat();
                }
                break;

                  case B_RECT_TYPE:
                {
                  Rect [] rects = (Rect[]) newArray;
                  for (int i=0; i<_numItems; i++)
                rects[i] = (Rect) rects[i].cloneFlat();
                }
                break;

                  case B_MESSAGE_TYPE:
                {
                  Message [] messages = (Message[]) newArray;
                  for (int i=0; i<_numItems; i++)
                messages[i] = (Message) messages[i].cloneFlat();
                }
                break;

                  default:
                {
                  if (newArray is byte[][])
                {
                  // Clone the byte arrays, since they are modifiable
                  byte [][] array = (byte[][]) newArray;
                  for (int i=0; i<_numItems; i++)
                {
                  byte [] newBuf = (byte []) array[i].Clone();
                  array[i] = newBuf;
                }
                }
                }
                break;
                  }

                clone.setPayload(newArray, _numItems);
                return clone;
            }
Example #12
0
 /// Take the data under (name) in this message, and copies it 
 /// into (moveTo). 
 /// <exception cref="FieldNotFound"/>
 public void copyField(string name, Message copyTo)
 {
     MessageField field = getField(name);
     copyTo.ensureFieldTableAllocated();
     copyTo._fieldTable.Add(name, field.cloneFlat());
 }
Example #13
0
 /// Returns an independent copy of this Message
 public override Flattenable cloneFlat()
 {
     Message clone = new Message();
     clone.setEqualTo(this);
     return clone;
 }
Example #14
0
        /// <summary>
        /// Reads from the input stream until a Message can be assembled 
        /// and returned.
        /// </summary>
        ///
        /// <param name="reader">the input stream from which to read</param>
        /// <exception cref="IOException"/>
        /// <exception cref="UnflattenFormatException"/>
        /// <returns>The next assembled Message</returns>
        ///
        public Message unflattenMessage(Stream inputStream)
        {
            BinaryReader reader = new BinaryReader(inputStream);
              int numBytes = reader.ReadInt32();
              int encoding = reader.ReadInt32();

              int independent = reader.ReadInt32();
              inputStream.Seek(-4, SeekOrigin.Current);

              Message pmsg = new Message();
              pmsg.unflatten(reader, numBytes);

              return pmsg;
        }