forked from microsoft/coyote-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureClusterManager.cs
103 lines (86 loc) · 3.23 KB
/
AzureClusterManager.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Newtonsoft.Json;
namespace Microsoft.Coyote.Samples.CloudMessaging
{
internal class AzureClusterManager : ClusterManager
{
[DataContract]
public class RegisterMessageBusEvent : Event
{
public ITopicClient TopicClient;
}
public ITopicClient TopicClient;
protected override Task OnInitializeAsync(Event initialEvent)
{
var reg = initialEvent as RegisterMessageBusEvent;
this.TopicClient = reg.TopicClient;
return base.OnInitializeAsync(initialEvent);
}
public override async Task BroadcastVoteRequestAsync(Event e)
{
var request = e as VoteRequestEvent;
Message message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)))
{
Label = "VoteRequest",
ReplyTo = request.CandidateId
};
await this.TopicClient.SendAsync(message);
}
public override async Task SendVoteResponseAsync(Event e)
{
var response = e as VoteResponseEvent;
Message message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)))
{
Label = "VoteResponse",
To = response.TargetId
};
await this.TopicClient.SendAsync(message);
}
public override async Task BroadcastClientRequestAsync(Event e)
{
var req = e as ClientRequestEvent;
Message message = new Message(Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(req)))
{
Label = "ClientRequest"
};
await this.TopicClient.SendAsync(message);
}
public override async Task SendClientResponseAsync(Event e)
{
var response = e as ClientResponseEvent;
Message message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)))
{
Label = "ClientResponse"
};
await this.TopicClient.SendAsync(message);
}
public override async Task SendAppendEntriesRequestAsync(Event e)
{
var request = e as AppendLogEntriesRequestEvent;
Message message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)))
{
Label = "AppendEntriesRequest",
To = request.To,
ReplyTo = request.LeaderId
};
await this.TopicClient.SendAsync(message);
}
public override async Task SendAppendEntriesResponseAsync(Event e)
{
var response = e as AppendLogEntriesResponseEvent;
Message message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)))
{
Label = "AppendEntriesResponse",
To = response.To,
ReplyTo = response.SenderId
};
await this.TopicClient.SendAsync(message);
}
}
}