public void Add(string content) { Console.WriteLine(content); _node.AddLogEntry(Encoding.UTF8.GetBytes(content)); }
static void Scenario1(string[] args) { Console.WriteLine("Scenario 1 is running"); if (args.Length < 1) { Console.WriteLine("RaftCluster TCP listening port is not specified"); return; } if (args.Length < 2 || !(new System.IO.FileInfo(args[2])).Exists) { Console.WriteLine("Path to configuration file is not supplied or file is not found"); return; } string dbreezePath = ""; if (args.Length >= 3) { dbreezePath = args[3]; } Console.WriteLine($"Listening port: {args[1]}; Path to config: {args[2]}; Path to DBreeze folder: {args[3]}"); var configLines = System.IO.File.ReadAllLines(args[2]); TcpRaftNode rn = null; //rn = TcpRaftNode.GetFromConfig(1, System.IO.File.ReadAllText(args[2]), // dbreezePath, Convert.ToInt32(args[1]), log, // (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return true; }); rn = TcpRaftNode.GetFromConfig(System.IO.File.ReadAllText(args[2]), dbreezePath, Convert.ToInt32(args[1]), log, (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return(true); }); rn.Start(); AddLogEntryResult addRes = null; while (true) { var cmd = Console.ReadLine(); switch (cmd) { case "set1": addRes = rn.AddLogEntry(new byte[] { 23 }); Console.WriteLine($"Adding: {addRes.AddResult.ToString()}"); break; case "set1a": addRes = rn.AddLogEntry(new byte[] { 27 }, entityName: "inMemory1"); Console.WriteLine($"Adding: {addRes.AddResult.ToString()}"); break; case "set10": for (int k = 0; k < 10; k++) { addRes = rn.AddLogEntry(new byte[] { 23 }); Console.WriteLine($"Adding: {addRes.AddResult.ToString()}"); } break; case "set10a": for (int k = 0; k < 10; k++) { addRes = rn.AddLogEntry(new byte[] { 23 }, entityName: "inMemory1"); Console.WriteLine($"Adding: {addRes.AddResult.ToString()}"); } break; } } }
static void Main(string[] args) { log = new Logger(); var config = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + @"\config.txt"); TcpRaftNode rn1 = TcpRaftNode.GetFromConfig(1, config, System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node1", 4250, log, (entityName, index, data) => { Console.WriteLine($"Committed {entityName}/{index}"); Console.WriteLine("0000000000000 4250数据:" + System.Text.Encoding.UTF8.GetString(data)); return(true); }); TcpRaftNode rn2 = TcpRaftNode.GetFromConfig(1, config, System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node2", 4251, log, (entityName, index, data) => { Console.WriteLine($"Committed {entityName}/{index}"); Console.WriteLine("111111111111 4251数据:" + System.Text.Encoding.UTF8.GetString(data)); var random = new Random(); var num = random.Next(1, 3) % 2; Console.WriteLine(num); if (num == 1) { return(true); } else { return(false); } }); TcpRaftNode rn3 = TcpRaftNode.GetFromConfig(1, config, System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node3", 4252, log, (entityName, index, data) => { Console.WriteLine($"Committed {entityName}/{index}"); Console.WriteLine("222222222222 4252数据:" + System.Text.Encoding.UTF8.GetString(data)); return(true); }); rn1.Start(); rn2.Start(); rn3.Start(); while (true) { Console.Clear(); Console.WriteLine("选择要发送的:1、4250 2、4251 3、4252"); switch (Console.ReadLine()) { case "1": rn1.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1"); break; case "2": rn2.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1"); break; case "3": rn3.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1"); break; } } }