forked from microsoft/GraphEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (97 loc) · 4.07 KB
/
Program.cs
File metadata and controls
104 lines (97 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Graph Engine
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trinity;
using Trinity.Core.Lib;
using Trinity.Extension.DistributedHashtable;
namespace DistributedHashtable
{
class Program
{
static void Main(string[] args)
{
TrinityConfig.AddServer(new Trinity.Network.ServerInfo("127.0.0.1", 5304, Global.MyAssemblyPath, Trinity.Diagnostics.LogLevel.Error));
if (args.Length < 1)
{
Console.WriteLine("Please provide a command line parameter (-s|-c).");
Console.WriteLine(" -s Start as a distributed hashtable server.");
Console.WriteLine(" -c Start as a distributed hashtable client.");
return;
}
if (args[0].Trim().ToLower().StartsWith("-s"))
{
DistributedHashtableServer server = new DistributedHashtableServer();
server.Start();
while(true) {
Thread.Sleep(1000);
}
}
if (args[0].Trim().ToLower().StartsWith("-c"))
{
HandlingUserInput();
}
}
static void HandlingUserInput()
{
TrinityConfig.CurrentRunningMode = RunningMode.Client;
while (true)
{
Console.WriteLine("Please input a command (set|get):");
string input = Console.ReadLine().Trim();
string[] fields = input.Split(new char[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries);
if (fields.Length > 0)
{
string command = fields[0].Trim().ToLower();
#region get
if (command.Equals("get"))
{
if (fields.Length < 2)
{
Console.WriteLine("example: get(key1) or get(\"key 2\")");
continue;
}
string key = fields[1].Trim().Trim(new char[] { '\"' });
using (var request = new GetMessageWriter(key))
{
using (var response = Global.CloudStorage.GetToDistributedHashtableServer(DistributedHashtableServer.GetServerIdByKey(key), request))
{
if (response.IsFound)
Console.WriteLine("The value of \"{0}\" is \"{1}\"", key, response.Value);
else
Console.WriteLine("The key is not found.");
}
}
continue;
}
#endregion
#region set
if (command.Equals("set"))
{
if (fields.Length < 3)
{
Console.WriteLine("example: set(key1, value1) or set(\"key 2\", \"value 2\")");
continue;
}
string key = fields[1].Trim().Trim(new char[] { '\"' });
string value = fields[2].Trim().Trim(new char[] { '\"' });
using (var request = new SetMessageWriter(key, value))
{
Console.WriteLine("Set the value of \"{0}\" is \"{1}\"", key, value);
Global.CloudStorage.SetToDistributedHashtableServer(DistributedHashtableServer.GetServerIdByKey(key), request);
}
continue;
}
#endregion
}
Console.WriteLine("Please input a valid command (set|get):");
}
}
}
}