示例#1
0
        public static TcpClusterEndPoint BiserDecode(byte[] enc = null, Biser.Decoder extDecoder = null) //!!!!!!!!!!!!!! change return type
        {
            Biser.Decoder decoder = null;
            if (extDecoder == null)
            {
                if (enc == null || enc.Length == 0)
                {
                    return(null);
                }
                decoder = new Biser.Decoder(enc);
                if (decoder.CheckNull())
                {
                    return(null);
                }
            }
            else
            {
                if (extDecoder.CheckNull())
                {
                    return(null);
                }
                else
                {
                    decoder = extDecoder;
                }
            }

            TcpClusterEndPoint m = new TcpClusterEndPoint();  //!!!!!!!!!!!!!! change return type

            m.Host = decoder.GetString();
            m.Port = decoder.GetInt();

            return(m);
        }
示例#2
0
        internal List <TcpClusterEndPoint> clusterEndPoints = new List <TcpClusterEndPoint>();  //init clusterEndPoints creating 1-N connection


        public TcpRaftNode(List <TcpClusterEndPoint> clusterEndPoints, List <RaftNodeSettings> raftNodes, string dbreezePath, Func <string, ulong, byte[], bool> OnCommit, int port = 4250, IWarningLog log = null)
        {
            //this.rn_settings = rn_settings ?? new RaftNodeSettings();

            this.log  = log;
            this.port = port;
            if (clusterEndPoints != null)
            {
                var bt      = clusterEndPoints.SerializeBiser();
                var decoder = new Biser.Decoder(bt);
                this.clusterEndPoints = new List <TcpClusterEndPoint>();
                decoder.GetCollection(() => { return(TcpClusterEndPoint.BiserDecode(extDecoder: decoder)); }, this.clusterEndPoints, false);

                //this.clusterEndPoints.AddRange(clusterEndPoints.SerializeProtobuf().DeserializeProtobuf<List<TcpClusterEndPoint>>());
            }
            spider = new TcpSpider(this);

            bool firstNode = true;

            foreach (var rn_settings in raftNodes)
            {
                if (firstNode)
                {
                    rn_settings.EntityName = "default";
                    firstNode = false;
                }

                if (String.IsNullOrEmpty(rn_settings.EntityName))
                {
                    throw new Exception("Raft.Net: entities must have unique names. Change RaftNodeSettings.EntityName.");
                }

                if (this.raftNodes.ContainsKey(rn_settings.EntityName))
                {
                    throw new Exception("Raft.Net: entities must have unique names. Change RaftNodeSettings.EntityName.");
                }

                var rn = new RaftNode(rn_settings ?? new RaftNodeSettings(), dbreezePath, this.spider, this.log, OnCommit);

#if DEBUG
                rn.Verbose = rn_settings.VerboseRaft;                               //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   DEBUG PURPOSES
#endif
                rn.SetNodesQuantityInTheCluster((uint)this.clusterEndPoints.Count); //!!!!!!!!!!!!  ENABLE 1 for debug, make it dynamic (but not less then 3 if not DEBUG)
                rn.NodeAddress.NodeAddressId = port;                                //for debug/emulation purposes

                rn.NodeAddress.NodeUId = Guid.NewGuid().ToByteArray().Substring(8, 8).To_Int64_BigEndian();

                this.raftNodes[rn_settings.EntityName] = rn;

                rn.NodeStart();
            }
        }
示例#3
0
        public static TcpClusterEndPoint BiserJsonDecode(string enc = null, Biser.JsonDecoder extDecoder = null, Biser.JsonSettings settings = null) //!!!!!!!!!!!!!! change return type
        {
            Biser.JsonDecoder decoder = null;

            if (extDecoder == null)
            {
                if (enc == null || String.IsNullOrEmpty(enc))
                {
                    return(null);
                }
                decoder = new Biser.JsonDecoder(enc, settings);
                if (decoder.CheckNull())
                {
                    return(null);
                }
            }
            else
            {
                //JSONSettings of the existing decoder will be used
                decoder = extDecoder;
            }

            TcpClusterEndPoint m = new TcpClusterEndPoint();  //!!!!!!!!!!!!!! change return type

            foreach (var props in decoder.GetDictionary <string>())
            {
                switch (props)
                {
                case "Host":
                    m.Host = decoder.GetString();
                    break;

                case "Port":
                    m.Port = decoder.GetInt();
                    break;

                default:
                    decoder.SkipValue();    //MUST BE HERE
                    break;
                }
            }
            return(m);
        }//eof