protected virtual void WriteTransactionInfo(TransactionInfo command, StompFrameStream ss)
        {
            TransactionId id = command.TransactionId;

            if (id is LocalTransactionId)
            {
                string          type            = "BEGIN";
                TransactionType transactionType = (TransactionType)command.Type;
                switch (transactionType)
                {
                case TransactionType.CommitOnePhase:
                    command.ResponseRequired = true;
                    type = "COMMIT";
                    break;

                case TransactionType.Rollback:
                    command.ResponseRequired = true;
                    type = "ABORT";
                    break;
                }
                Console.WriteLine(">>> For transaction type: " + transactionType + " we are using command type: " + type);

                ss.WriteCommand(command, type);

                ss.WriteHeader("transaction", StompHelper.ToStomp(id));

                ss.Flush();
            }
        }
        protected virtual void WriteConsumerInfo(ConsumerInfo command, StompFrameStream ss)
        {
            ss.WriteCommand(command, "SUBSCRIBE");
            ss.WriteHeader("destination", StompHelper.ToStomp(command.Destination));
            ss.WriteHeader("id", StompHelper.ToStomp(command.ConsumerId));
            ss.WriteHeader("durable-subscriber-name", command.SubscriptionName);
            ss.WriteHeader("selector", command.Selector);
            if (command.NoLocal)
            {
                ss.WriteHeader("no-local", command.NoLocal);
            }
            ss.WriteHeader("ack", "client");

            // ActiveMQ extensions to STOMP
            ss.WriteHeader("activemq.dispatchAsync", command.DispatchAsync);
            if (command.Exclusive)
            {
                ss.WriteHeader("activemq.exclusive", command.Exclusive);
            }

            ss.WriteHeader("activemq.maximumPendingMessageLimit", command.MaximumPendingMessageLimit);
            ss.WriteHeader("activemq.prefetchSize", command.PrefetchSize);
            ss.WriteHeader("activemq.priority ", command.Priority);
            if (command.Retroactive)
            {
                ss.WriteHeader("activemq.retroactive", command.Retroactive);
            }

            consumers[command.ConsumerId] = command.ConsumerId;
            ss.Flush();
        }
        protected virtual void WriteMessage(ActiveMQMessage command, StompFrameStream ss)
        {
            ss.WriteCommand(command, "SEND");
            ss.WriteHeader("destination", StompHelper.ToStomp(command.Destination));
            if (command.ReplyTo != null)
            {
                ss.WriteHeader("reply-to", StompHelper.ToStomp(command.ReplyTo));
            }
            if (command.CorrelationId != null)
            {
                ss.WriteHeader("correlation-id", command.CorrelationId);
            }
            if (command.Expiration != 0)
            {
                ss.WriteHeader("expires", command.Expiration);
            }
            if (command.Priority != 4)
            {
                ss.WriteHeader("priority", command.Priority);
            }
            if (command.Type != null)
            {
                ss.WriteHeader("type", command.Type);
            }
            if (command.TransactionId != null)
            {
                ss.WriteHeader("transaction", StompHelper.ToStomp(command.TransactionId));
            }

            ss.WriteHeader("persistent", command.Persistent);

            // lets force the content to be marshalled

            command.BeforeMarshall(null);
            if (command is ActiveMQTextMessage)
            {
                ActiveMQTextMessage textMessage = command as ActiveMQTextMessage;
                ss.Content = encoding.GetBytes(textMessage.Text);
            }
            else
            {
                ss.Content       = command.Content;
                ss.ContentLength = command.Content.Length;
            }

            IPrimitiveMap map = command.Properties;

            foreach (string key in map.Keys)
            {
                ss.WriteHeader(key, map[key]);
            }
            ss.Flush();
        }
        public void Marshal(Object o, BinaryWriter binaryWriter)
        {
            Console.WriteLine(">>>> " + o);
            //Console.Out.Flush();
            StompFrameStream ds = new StompFrameStream(binaryWriter, encoding);

            if (o is ConnectionInfo)
            {
                WriteConnectionInfo((ConnectionInfo)o, ds);
            }
            else if (o is ActiveMQMessage)
            {
                WriteMessage((ActiveMQMessage)o, ds);
            }
            else if (o is ConsumerInfo)
            {
                WriteConsumerInfo((ConsumerInfo)o, ds);
            }
            else if (o is MessageAck)
            {
                WriteMessageAck((MessageAck)o, ds);
            }
            else if (o is TransactionInfo)
            {
                WriteTransactionInfo((TransactionInfo)o, ds);
            }
            else if (o is ShutdownInfo)
            {
                WriteShutdownInfo((ShutdownInfo)o, ds);
            }
            else if (o is RemoveInfo)
            {
                WriteRemoveInfo((RemoveInfo)o, ds);
            }
            else if (o is Command)
            {
                Command command = o as Command;
                if (command.ResponseRequired)
                {
                    Response response = new Response();
                    response.CorrelationId = command.CommandId;
                    SendCommand(response);
                }
                Console.WriteLine("#### Ignored command: " + o.GetType());
                Console.Out.Flush();
            }
            else
            {
                Console.WriteLine("#### Ignored command: " + o.GetType());
                Console.Out.Flush();
            }
        }
        protected virtual void WriteMessageAck(MessageAck command, StompFrameStream ss)
        {
            ss.WriteCommand(command, "ACK");

            // TODO handle bulk ACKs?
            ss.WriteHeader("message-id", StompHelper.ToStomp(command.FirstMessageId));
            if (command.TransactionId != null)
            {
                ss.WriteHeader("transaction", StompHelper.ToStomp(command.TransactionId));
            }

            ss.Flush();
        }
        protected virtual void WriteConnectionInfo(ConnectionInfo command, StompFrameStream ss)
        {
            // lets force a receipt
            command.ResponseRequired = true;

            ss.WriteCommand(command, "CONNECT");
            ss.WriteHeader("client-id", command.ClientId);
            ss.WriteHeader("login", command.UserName);
            ss.WriteHeader("passcode", command.Password);

            if (command.ResponseRequired)
            {
                ss.WriteHeader("request-id", command.CommandId);
            }

            ss.Flush();
        }
        protected virtual void WriteRemoveInfo(RemoveInfo command, StompFrameStream ss)
        {
            object id = command.ObjectId;

            if (id is ConsumerId)
            {
                ConsumerId consumerId = id as ConsumerId;
                ss.WriteCommand(command, "UNSUBSCRIBE");
                ss.WriteHeader("id", StompHelper.ToStomp(consumerId));
                ss.Flush();
                consumers.Remove(consumerId);
            }
            // When a session is removed, it needs to remove it's consumers too.
            if (id is SessionId)
            {
                // Find all the consumer that were part of the session.
                SessionId sessionId = (SessionId)id;
                ArrayList matches   = new ArrayList();
                foreach (DictionaryEntry entry in consumers)
                {
                    ConsumerId t = (ConsumerId)entry.Key;
                    if (sessionId.ConnectionId == t.ConnectionId && sessionId.Value == t.SessionId)
                    {
                        matches.Add(t);
                    }
                }

                // Un-subscribe them.
                foreach (ConsumerId consumerId in matches)
                {
                    ss.WriteCommand(command, "UNSUBSCRIBE");
                    ss.WriteHeader("id", StompHelper.ToStomp(consumerId));
                    ss.Flush();
                    consumers.Remove(consumerId);
                }
            }
        }
 protected virtual void WriteShutdownInfo(ShutdownInfo command, StompFrameStream ss)
 {
     ss.WriteCommand(command, "DISCONNECT");
     ss.Flush();
 }