Multi-role finite state machine, used for multithreaded programs or distributed programs.
Some similar libraries:
Requires zig version greater than 0.15.0.
Download and add troupe as a dependency by running the following command in your project root:
zig fetch --save git+https://github.com/sdzx-1/troupe.gitThen, retrieve the dependency in your build.zig:
const troupe = b.dependency("troupe", .{
.target = target,
.optimize = optimize,
});Finally, add the dependency's module to your module's imports:
exe_mod.addImport("troupe", troupe.module("root"));You should now be able to import troupe in your module's code:
const troupe = @import("troupe");troupe ensures that the behavior of each role is completely determined by the state machine. If the communication itself can guarantee the order (such as TCP), then the protocol described by troupe is deterministic and the behavior of all roles is consistent.
Through polystate, we know that state can be used as a function and parameter, which we call high-order state.
Through the introduction here, we know that communication can be modeled using a state machine.
Multi-role communication differs from client-server communication in that troupe requires that messages generated during branching must be notified to all other parties. This ensures that all roles are synchronized.
If two protocol participants are exactly the same, then the states are directly combined. If the participants of the two protocols are different, then we need to notify all other roles except the roles of the previous protocol. This issue describes the situation.
zig build pingpongAlice and Bob have multiple ping-pong communications back and forth.
zig build sendfileAlice sends a file to Bob, and every time she sends a chunk of data, she checks whether the hash values of the sent and received data match.
zig build pingpong-sendfileCombining the pingpong protocol and the sendfile protocol
zig build 2pcA two-phase protocol demo with Charlie as the coordinator and Alice and Bob as participants. Alice and Bob have no actual transactions; they simply randomly return true or false.
zig build random-pingpong-2pcA complex protocol involving four actors has an additional selector to select the combined protocol to run. Here, we arbitrarily combine the pingpong protocol and the 2pc protocol. Note that the communication actors in pingpong and 2pc are different. troupe supports this combination of different protocols, even if the protocols have different numbers of participants.