Skip to content

Commit 9e17aac

Browse files
committed
Refect Graph.zig
1 parent 13878b2 commit 9e17aac

File tree

6 files changed

+60
-354
lines changed

6 files changed

+60
-354
lines changed

examples/core.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn SendFile(Successed: type, Failed: type) type {
3737
);
3838
}
3939

40-
pub const EnterFsmState = PingPong(SendFile(PingPong(ps.Exit).Start, ps.Exit).Start).Start;
40+
pub const EnterFsmState = PingPong(SendFile(PingPong(ps.Exit).Ping, ps.Exit).Start).Ping;
4141

4242
pub const Runner = ps.Runner(EnterFsmState);
4343
pub const curr_id = Runner.idFromState(EnterFsmState);

examples/pingpong.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ pub fn MkPingPong(
2222
) type {
2323
return struct {
2424
fn pingpogn_info(
25+
StateName: []const u8,
2526
sender: Role,
2627
receiver: []const Role,
2728
) ps.ProtocolInfo("pingpong", Role, context, &.{ client, server }, &.{NextFsmState}) {
28-
return .{ .sender = sender, .receiver = receiver };
29+
return .{ .name = StateName, .sender = sender, .receiver = receiver };
2930
}
3031

31-
pub const Start = union(enum) {
32-
ping: Data(i32, info.Cast(server, client, i32, PongFn, @This())),
32+
pub const Ping = union(enum) {
33+
ping: Data(i32, info.Cast("Pong", server, client, i32, PongFn, @This())),
3334
next: Data(void, NextFsmState),
3435

35-
pub const info = pingpogn_info(client, &.{server});
36+
pub const info = pingpogn_info("Ping", client, &.{server});
3637

3738
pub fn process(parent_ctx: *info.Ctx(client)) !@This() {
3839
const ctx = client_ctxFromParent(parent_ctx);

examples/sendfile.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn MkSendFile(
3333
) type {
3434
return struct {
3535
fn sendfile_info(
36+
StateName: []const u8,
3637
sender_: Role,
3738
receiver_: []const Role,
3839
) ps.ProtocolInfo(
@@ -42,7 +43,7 @@ pub fn MkSendFile(
4243
&.{ sender, receiver },
4344
&.{ Successed, Failed },
4445
) {
45-
return .{ .sender = sender_, .receiver = receiver_ };
46+
return .{ .name = StateName, .sender = sender_, .receiver = receiver_ };
4647
}
4748

4849
const SendFileSize = struct {
@@ -58,17 +59,17 @@ pub fn MkSendFile(
5859
};
5960

6061
//Here, a temporary `info` is built to use `Cast`
61-
pub const Start = sendfile_info(sender, &.{}).Cast(sender, receiver, u64, SendFileSize, Send);
62+
pub const Start = sendfile_info("", sender, &.{}).Cast("SendFileSize", sender, receiver, u64, SendFileSize, Send);
6263

6364
pub const Send = union(enum) {
6465
// zig fmt: off
6566
check : Data(u64 , CheckHash(@This(), Failed)),
6667
send : Data([]const u8, @This()),
67-
final : Data([]const u8, info.Cast(sender, receiver, u64, SendFinalHash, CheckHash(Successed, Failed))),
68+
final : Data([]const u8, info.Cast("SendFinalHash", sender, receiver, u64, SendFinalHash, CheckHash(Successed, Failed))),
6869
final_zero: Data(void , Successed),
6970
// zig fmt: on
7071

71-
pub const info = sendfile_info(sender, &.{receiver});
72+
pub const info = sendfile_info("Send", sender, &.{receiver});
7273

7374
const SendFinalHash = struct {
7475
pub fn process(parent_ctx: *@field(context, @tagName(sender))) !u64 {
@@ -149,7 +150,7 @@ pub fn MkSendFile(
149150
Successed: Data(void, A),
150151
Failed: Data(void, B),
151152

152-
pub const info = sendfile_info(receiver, &.{sender});
153+
pub const info = sendfile_info("CheckHash", receiver, &.{sender});
153154

154155
pub fn process(parent_ctx: *@field(context, @tagName(receiver))) !@This() {
155156
const ctx = recver_ctxFromParent(parent_ctx);

examples/two_phase_commit_generic.zig

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn main() !void {
1717
if (arg_iter.next()) |arg_str| {
1818
std.debug.print("arg_str {s}\n", .{arg_str});
1919
if (std.mem.eql(u8, "dot", arg_str)) {
20-
const graph: ps.Graph = try ps.Graph.initWithFsm(gpa, Random2pc);
20+
const graph: ps.Graph = try ps.Graph.initWithFsm(gpa, Start);
2121
const graph_fs = try std.fs.cwd().createFile("t.dot", .{});
2222
var graph_fs_writer = graph_fs.writer(try gpa.alloc(u8, 1 << 20));
2323
try graph.generateDot(&graph_fs_writer.interface);
@@ -108,7 +108,7 @@ const CharlieContext = struct {
108108
};
109109

110110
const SelectorContext = struct {
111-
times_2pc: u32 = 0,
111+
times: u32 = 0,
112112
xoshiro256: std.Random.Xoshiro256 = undefined,
113113
};
114114

@@ -119,7 +119,7 @@ const Context = struct {
119119
selector: type = SelectorContext,
120120
};
121121

122-
pub const EnterFsmState = Random2pc;
122+
pub const EnterFsmState = Start;
123123

124124
pub const Runner = ps.Runner(EnterFsmState);
125125
pub const curr_id = Runner.idFromState(EnterFsmState);
@@ -147,31 +147,32 @@ fn BAC(Next: type) type {
147147
}
148148

149149
//Randomly select a 2pc protocol
150-
pub const Random2pc = union(enum) {
150+
pub const Start = union(enum) {
151151
charlie_as_coordinator: Data(void, PingPong(.alice, .bob, PingPong(.bob, .charlie, PingPong(
152152
.charlie,
153153
.alice,
154-
CAB(@This()).Start,
155-
).Start).Start).Start),
156-
alice_as_coordinator: Data(void, PingPong(.charlie, .bob, ABC(@This()).Start).Start),
157-
bob_as_coordinator: Data(void, PingPong(.alice, .charlie, BAC(@This()).Start).Start),
154+
CAB(@This()).Begin,
155+
).Ping).Ping).Ping),
156+
alice_as_coordinator: Data(void, PingPong(.charlie, .bob, ABC(@This()).Begin).Ping),
157+
bob_as_coordinator: Data(void, PingPong(.alice, .charlie, BAC(@This()).Begin).Ping),
158158
exit: Data(void, ps.Exit),
159159

160160
pub const info: ps.ProtocolInfo(
161-
"random_2pc",
161+
"random_pingpong_and_2pc",
162162
AllRole,
163163
Context{},
164164
&.{ .selector, .charlie, .alice, .bob },
165165
&.{},
166166
) = .{
167+
.name = "Start",
167168
.sender = .selector,
168169
.receiver = &.{ .charlie, .alice, .bob },
169170
};
170171

171172
pub fn process(ctx: *SelectorContext) !@This() {
172-
ctx.times_2pc += 1;
173-
std.debug.print("times_2pc: {d}\n", .{ctx.times_2pc});
174-
if (ctx.times_2pc > 300) {
173+
ctx.times += 1;
174+
std.debug.print("times: {d}\n", .{ctx.times});
175+
if (ctx.times > 300) {
175176
return .{ .exit = .{ .data = {} } };
176177
}
177178

@@ -196,20 +197,24 @@ pub fn mk2pc(
196197
Failed: type,
197198
) type {
198199
return struct {
199-
fn two_pc(sender: Role, receiver: []const Role) ps.ProtocolInfo(
200+
fn two_pc(
201+
StateName: []const u8,
202+
sender: Role,
203+
receiver: []const Role,
204+
) ps.ProtocolInfo(
200205
"2pc_generic",
201206
Role,
202207
context,
203208
&.{ coordinator, alice, bob },
204209
&.{ Successed, Failed },
205210
) {
206-
return .{ .sender = sender, .receiver = receiver };
211+
return .{ .name = StateName, .sender = sender, .receiver = receiver };
207212
}
208213

209-
pub const Start = union(enum) {
214+
pub const Begin = union(enum) {
210215
begin: Data(void, AliceResp),
211216

212-
pub const info = two_pc(coordinator, &.{ alice, bob });
217+
pub const info = two_pc("Begin", coordinator, &.{ alice, bob });
213218

214219
pub fn process(ctx: *info.Ctx(coordinator)) !@This() {
215220
ctx.counter = 0;
@@ -220,7 +225,7 @@ pub fn mk2pc(
220225
pub const AliceResp = union(enum) {
221226
resp: Data(bool, BobResp),
222227

223-
pub const info = two_pc(alice, &.{coordinator});
228+
pub const info = two_pc("AliceResp", alice, &.{coordinator});
224229

225230
pub fn process(ctx: *info.Ctx(alice)) !@This() {
226231
const random: std.Random = ctx.xoshiro256.random();
@@ -241,7 +246,7 @@ pub fn mk2pc(
241246
union(enum) {
242247
resp: Data(bool, Check),
243248

244-
pub const info = two_pc(bob, &.{coordinator});
249+
pub const info = two_pc("BobResp", bob, &.{coordinator});
245250

246251
pub fn process(ctx: *info.Ctx(bob)) !@This() {
247252
const random: std.Random = ctx.xoshiro256.random();
@@ -261,9 +266,9 @@ pub fn mk2pc(
261266
pub const Check = union(enum) {
262267
succcessed: Data(void, Successed),
263268
failed: Data(void, Failed),
264-
failed_retry: Data(void, Start),
269+
failed_retry: Data(void, Begin),
265270

266-
pub const info = two_pc(coordinator, &.{ alice, bob });
271+
pub const info = two_pc("Check", coordinator, &.{ alice, bob });
267272

268273
pub fn process(ctx: *info.Ctx(coordinator)) !@This() {
269274
if (ctx.counter == 2) {

0 commit comments

Comments
 (0)