Exemple #1
0
        public void run()
        {
            Socket socket = this.socket;

            try
            {
                long offset = -1;
                if (bis == null)
                {
                    bis = new BufferedStream(new NetworkStream(socket));
                }
                ExtendedDataInput @in = null;

                while (true)
                {
                    if (@in == null)
                    {
                        bool isLittle = bis.ReadByte() != 0;
                        if (isLittle)
                        {
                            @in = new LittleEndianDataInputStream(bis);
                        }
                        else
                        {
                            @in = new BigEndianDataInputStream(bis);
                        }
                    }
                    else
                    {
                        @in.readBoolean();
                    }

                    @in.readLong();
                    long msgid = @in.readLong();

                    if (offset == -1)
                    {
                        offset = msgid;
                    }
                    topic = @in.readString();
                    short          flag    = @in.readShort();
                    IEntityFactory factory = new BasicEntityFactory();
                    int            form    = flag >> 8;
                    int            type    = flag & 0xff;

                    if (form < 0 || form > MAX_FORM_VALUE)
                    {
                        throw new IOException("Invalid form value: " + form);
                    }
                    if (type < 0 || type > MAX_TYPE_VALUE)
                    {
                        throw new IOException("Invalid type value: " + type);
                    }

                    DATA_FORM df = (DATA_FORM)form;
                    DATA_TYPE dt = (DATA_TYPE)type;

                    IEntity body;
                    try
                    {
                        body = factory.createEntity(df, dt, @in);
                    }
                    catch
                    {
                        throw;
                    }
                    if (body.isTable())
                    {
                        if (body.rows() != 0)
                        {
                            throw new Exception("When message is table, it should be empty.");
                        }
                    }
                    else if (body.isVector())
                    {
                        dispatcher.setMsgId(topic, msgid);
                        BasicAnyVector dTable = (BasicAnyVector)body;

                        int colSize = dTable.rows();
                        int rowSize = dTable.getEntity(0).rows();
                        if (rowSize == 1)
                        {
                            BasicMessage rec = new BasicMessage(msgid, topic, dTable);
                            dispatcher.dispatch(rec);
                        }
                        else if (rowSize > 1)
                        {
                            List <IMessage> messages = new List <IMessage>(rowSize);
                            for (int i = 0; i < rowSize; i++)
                            {
                                BasicAnyVector row = new BasicAnyVector(colSize);

                                for (int j = 0; j < colSize; j++)
                                {
                                    AbstractVector vector = (AbstractVector)dTable.getEntity(j);
                                    IEntity        entity = vector.get(i);
                                    row.setEntity(j, entity);
                                }
                                BasicMessage rec = new BasicMessage(msgid, topic, row);
                                messages.Add(rec);
                                msgid++;
                            }
                            dispatcher.batchDispatch(messages);
                        }
                        offset += rowSize;
                    }
                    else
                    {
                        throw new Exception("message body has an invalid format. Vector or table is expected");
                    }
                }
            }
            catch (Exception)
            {
                if (dispatcher.isClosed(topic))
                {
                    return;
                }
                else
                {
                    dispatcher.tryReconnect(topic);
                }
            }
            finally
            {
                try
                {
                    socket.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.Write(ex.StackTrace);
                }
            }
        }
Exemple #2
0
        public void run()
        {
            ConcurrentDictionary <string, SubscribeInfo> subscribeInfos = dispatcher_.getSubscribeInfos();
            Socket socket = this.socket_;

            try
            {
                if (bis_ == null)
                {
                    bis_ = new BufferedStream(new NetworkStream(socket));
                }
                ExtendedDataInput @in = null;

                while (!dispatcher_.isClose())
                {
                    if (@in == null)
                    {
                        bool isLittle = bis_.ReadByte() != 0;
                        if (isLittle)
                        {
                            @in = new LittleEndianDataInputStream(bis_);
                        }
                        else
                        {
                            @in = new BigEndianDataInputStream(bis_);
                        }
                    }
                    else
                    {
                        @in.readBoolean();
                    }

                    @in.readLong();
                    long msgid = @in.readLong();

                    topics = @in.readString();
                    short          flag     = @in.readShort();
                    IEntityFactory factory  = new BasicEntityFactory();
                    int            form     = flag >> 8;
                    int            type     = flag & 0xff;
                    bool           extended = type >= 128;
                    if (type >= 128)
                    {
                        type -= 128;
                    }

                    if (form < 0 || form > MAX_FORM_VALUE)
                    {
                        throw new IOException("Invalid form value: " + form);
                    }
                    if (type < 0 || type > MAX_TYPE_VALUE)
                    {
                        throw new IOException("Invalid type value: " + type);
                    }

                    DATA_FORM df = (DATA_FORM)form;
                    DATA_TYPE dt = (DATA_TYPE)type;

                    IEntity body;
                    try
                    {
                        body = factory.createEntity(df, dt, @in, extended);
                    }
                    catch
                    {
                        throw;
                    }
                    if (body.isTable())
                    {
                        foreach (string HATopic in topics.Split(','))
                        {
                            string topic = dispatcher_.getTopicForHATopic(HATopic);
                            if (topic == null)
                            {
                                throw new Exception("Subscription with topic " + HATopic + " does not exist. ");
                            }
                            if (!successTopics.Contains(topic))
                            {
                                SubscribeInfo subscribeInfo = null;
                                //Prevents a situation where streaming data arrives earlier than the subscription succeeds.
                                lock (subscribeInfos)
                                {
                                    if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                                    {
                                        throw new Exception("Subscription with topic " + topic + " does not exist. ");
                                    }
                                }
                                lock (subscribeInfo)
                                {
                                    if (subscribeInfo.getConnectState() != ConnectState.RECEIVED_SCHEMA)
                                    {
                                        subscribeInfo.setConnectState(ConnectState.RECEIVED_SCHEMA);
                                    }
                                    else
                                    {
                                        throw new Exception("Subscription with topic " + topic + " already has a thread parsing the stream data. ");
                                    }
                                }
                                successTopics.Add(topic);
                            }
                        }
                    }
                    else if (body.isVector())
                    {
                        foreach (string HATopic in topics.Split(','))
                        {
                            string topic = dispatcher_.getTopicForHATopic(HATopic);
                            if (topic == null)
                            {
                                throw new Exception("Subscription with topic " + HATopic + " does not exist. ");
                            }
                            SubscribeInfo subscribeInfo = null;
                            if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                            {
                                throw new Exception("Subscription with topic " + topic + " does not exist. ");
                            }
                            BasicAnyVector dTable = (BasicAnyVector)body;

                            int colSize = dTable.rows();
                            int rowSize = dTable.getEntity(0).rows();
                            if (rowSize == 1)
                            {
                                BasicMessage rec = new BasicMessage(msgid, topic, dTable);
                                dispatcher_.dispatch(rec);
                            }
                            else if (rowSize > 1)
                            {
                                List <IMessage> messages = new List <IMessage>(rowSize);
                                for (int i = 0; i < rowSize; i++)
                                {
                                    BasicAnyVector row = new BasicAnyVector(colSize);

                                    for (int j = 0; j < colSize; j++)
                                    {
                                        AbstractVector vector = (AbstractVector)dTable.getEntity(j);
                                        IEntity        entity = vector.get(i);
                                        row.setEntity(j, entity);
                                    }
                                    BasicMessage rec = new BasicMessage(msgid, topic, row);
                                    messages.Add(rec);
                                }
                                dispatcher_.batchDispatch(messages);
                            }
                            lock (subscribeInfo)
                            {
                                subscribeInfo.setMsgId(msgid);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("message body has an invalid format. Vector or table is expected");
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine(e.StackTrace);
                foreach (string topic in successTopics)
                {
                    SubscribeInfo subscribeInfo = null;
                    if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                    {
                        System.Console.Out.WriteLine("Subscription with topic " + topic + " doesn't exist. ");
                    }
                    else
                    {
                        lock (subscribeInfo)
                        {
                            subscribeInfo.setConnectState(ConnectState.NO_CONNECT);
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    socket.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.Write(ex.StackTrace);
                }
            }
            Console.WriteLine("MessageParser thread stopped.");
        }