This repository was archived by the owner on Oct 3, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (120 loc) · 4.97 KB
/
Copy pathProgram.cs
File metadata and controls
134 lines (120 loc) · 4.97 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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 Trinity;
using Trinity.Storage;
using Trinity.Network.Messaging;
namespace SSSP
{
class SSSPServerImpl : SSSPServerBase
{
public override void DistanceUpdatingHandler(DistanceUpdatingMessageReader request)
{
List<DistanceUpdatingMessage> DistanceUpdatingMessageList = new List<DistanceUpdatingMessage>();
request.recipients.ForEach((cellId) =>
{
using (var cell = Global.LocalStorage.UseSSSPCell(cellId))
{
if (cell.distance > request.distance + 1)
{
cell.distance = request.distance + 1;
cell.parent = request.senderId;
Console.Write(cell.distance + " ");
MessageSorter sorter = new MessageSorter(cell.neighbors);
for (int i = 0; i < Global.ServerCount; i++)
{
DistanceUpdatingMessageWriter msg = new DistanceUpdatingMessageWriter(cell.CellId,
cell.distance, sorter.GetCellRecipientList(i));
Global.CloudStorage.DistanceUpdatingToSSSPServer(i, msg);
}
}
}
});
}
public override void StartSSSPHandler(StartSSSPMessageReader request)
{
if (Global.CloudStorage.IsLocalCell(request.root))
{
using (var rootCell = Global.LocalStorage.UseSSSPCell(request.root))
{
rootCell.distance = 0;
rootCell.parent = -1;
MessageSorter sorter = new MessageSorter(rootCell.neighbors);
for (int i = 0; i < Global.ServerCount; i++)
{
DistanceUpdatingMessageWriter msg = new DistanceUpdatingMessageWriter(rootCell.CellId, 0, sorter.GetCellRecipientList(i));
Global.CloudStorage.DistanceUpdatingToSSSPServer(i, msg);
}
}
}
}
}
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 && args[0].StartsWith("-s"))
{
SSSPServerImpl server = new SSSPServerImpl();
server.Start();
while (true)
{
Thread.Sleep(10000);
}
}
//SSSP.exe -c startcell
if (args.Length >= 2 && args[0].StartsWith("-c"))
{
TrinityConfig.CurrentRunningMode = RunningMode.Client;
for (int i = 0; i < Global.ServerCount; i++)
{
using (var msg = new StartSSSPMessageWriter(long.Parse(args[1].Trim())))
{
Global.CloudStorage.StartSSSPToSSSPServer(i, msg);
}
}
}
//SSSP.exe -q cellID
if (args.Length >= 2 && args[0].StartsWith("-q"))
{
TrinityConfig.CurrentRunningMode = RunningMode.Client;
var cell = Global.CloudStorage.LoadSSSPCell(int.Parse(args[1]));
Console.WriteLine("Current vertex is {0}, the distance to the source vertex is {1}.",
cell.CellId, cell.distance);
while (cell.distance > 0)
{
cell = Global.CloudStorage.LoadSSSPCell(cell.parent);
Console.WriteLine("Current vertex is {0}, the distance to the source vertex is {1}.",
cell.CellId, cell.distance);
}
}
//SSSP.exe -g node count
if (args.Length >= 2 && args[0].StartsWith("-g"))
{
TrinityConfig.CurrentRunningMode = RunningMode.Client;
Random rand = new Random();
int nodeCount = int.Parse(args[1].Trim());
for (int i = 0; i < nodeCount; i++)
{
HashSet<long> neighbors = new HashSet<long>();
for (int j = 0; j < 10; j++)
{
long neighhor = rand.Next(0, nodeCount);
if (neighhor != i)
{
neighbors.Add(neighhor);
}
}
Global.CloudStorage.SaveSSSPCell(i, distance: int.MaxValue, parent: -1, neighbors: neighbors.ToList());
}
}
}
}
}