11-module (example ).
22
3- -export ([
4- start_command_ship /0 ,
5- spawn_pirate_ship /2 ,
6- ship_exists /2 ,
7- create_mission_ref /0 ,
8- send_message /3 ,
9- message_received /2 ,
10- get_last_message /2 ,
11- enable_distress_beacon /1 ,
12- link_ships /2 ,
13- destroy_ship /2 ,
14- get_last_distress_signal /1 ,
15- enable_auto_replacement /2 ,
16- count_ships_by_name /2 ,
17- get_ship_by_name /2 ,
18- send_distress_signal /4 ,
19- get_distress_signal_by_ref /2 ,
20- create_auth_ref /2 ,
21- send_authenticated_message /4 ,
22- authenticate_message /2
23- ]).
3+ -export ([start_command_ship /0 , spawn_pirate_ship /2 , ship_exists /2 , create_mission_ref /0 ,
4+ send_message /3 , message_received /2 , get_last_message /2 , enable_distress_beacon /1 ,
5+ link_ships /2 , destroy_ship /2 , get_last_distress_signal /1 , enable_auto_replacement /2 ,
6+ count_ships_by_name /2 , get_ship_by_name /2 , send_distress_signal /4 ,
7+ get_distress_signal_by_ref /2 , create_auth_ref /2 , send_authenticated_message /4 ,
8+ authenticate_message /2 ]).
249
2510% % Internal ship state record
26- -record (ship_state , {
27- name ,
28- messages = [],
29- received_refs = [],
30- auth_refs = [],
31- trap_exit = false ,
32- distress_signals = [],
33- ships_registry = [],
34- auto_replace = false ,
35- last_distress = undefined
36- }).
11+ -record (ship_state ,
12+ {name ,
13+ messages = [],
14+ received_refs = [],
15+ auth_refs = [],
16+ trap_exit = false ,
17+ distress_signals = [],
18+ ships_registry = [],
19+ auto_replace = false ,
20+ last_distress = undefined }).
3721
3822% % Start a command ship process that can act as a supervisor
3923start_command_ship () ->
40- spawn_link (fun () -> command_ship_loop (# ship_state {name = " Command" }) end ).
24+ spawn_link (fun () -> command_ship_loop (# ship_state {name = " Command" }) end ).
4125
4226% % Spawn a new pirate ship process with the given name
4327spawn_pirate_ship (CommandShip , Name ) ->
44- PirateShip = spawn (fun () -> pirate_ship_loop (# ship_state {name = Name }) end ),
28+ PirateShip = spawn (fun () -> pirate_ship_loop (# ship_state {name = Name }) end ),
4529 CommandShip ! {register_ship , Name , PirateShip },
4630 PirateShip .
4731
4832% % Check if a ship with the given name exists in the command ship's registry
4933ship_exists (CommandShip , Name ) ->
5034 CommandShip ! {ship_exists , Name , self ()},
5135 receive
52- {ship_exists_response , Result } -> Result
36+ {ship_exists_response , Result } ->
37+ Result
5338 after 1000 ->
5439 false
5540 end .
@@ -67,7 +52,8 @@ send_message(FromShip, ToShip, Message) ->
6752message_received (Ship , Ref ) ->
6853 Ship ! {check_received , Ref , self ()},
6954 receive
70- {received_check , Result } -> Result
55+ {received_check , Result } ->
56+ Result
7157 after 1000 ->
7258 false
7359 end .
@@ -76,7 +62,8 @@ message_received(Ship, Ref) ->
7662get_last_message (Ship , Ref ) ->
7763 Ship ! {get_message , Ref , self ()},
7864 receive
79- {message_response , Message } -> Message
65+ {message_response , Message } ->
66+ Message
8067 after 1000 ->
8168 undefined
8269 end .
@@ -105,7 +92,8 @@ destroy_ship(Ship, Reason) ->
10592get_last_distress_signal (CommandShip ) ->
10693 CommandShip ! {get_last_distress , self ()},
10794 receive
108- {last_distress , Signal } -> Signal
95+ {last_distress , Signal } ->
96+ Signal
10997 after 1000 ->
11098 undefined
11199 end .
@@ -119,7 +107,8 @@ enable_auto_replacement(CommandShip, Enable) ->
119107count_ships_by_name (CommandShip , Name ) ->
120108 CommandShip ! {count_ships , Name , self ()},
121109 receive
122- {ship_count , Count } -> Count
110+ {ship_count , Count } ->
111+ Count
123112 after 1000 ->
124113 0
125114 end .
@@ -128,7 +117,8 @@ count_ships_by_name(CommandShip, Name) ->
128117get_ship_by_name (CommandShip , Name ) ->
129118 CommandShip ! {get_ship , Name , self ()},
130119 receive
131- {ship_pid , Pid } -> Pid
120+ {ship_pid , Pid } ->
121+ Pid
132122 after 1000 ->
133123 undefined
134124 end .
@@ -142,7 +132,8 @@ send_distress_signal(Ship, CommandShip, Ref, Message) ->
142132get_distress_signal_by_ref (CommandShip , Ref ) ->
143133 CommandShip ! {get_distress_ref , Ref , self ()},
144134 receive
145- {distress_ref_response , Signal } -> Signal
135+ {distress_ref_response , Signal } ->
136+ Signal
146137 after 1000 ->
147138 undefined
148139 end .
@@ -163,7 +154,8 @@ send_authenticated_message(FromShip, ToShip, AuthRef, Message) ->
163154authenticate_message (Ship , AuthRef ) ->
164155 Ship ! {auth_check , AuthRef , self ()},
165156 receive
166- {auth_result , Result } -> Result
157+ {auth_result , Result } ->
158+ Result
167159 after 1000 ->
168160 false
169161 end .
@@ -173,141 +165,122 @@ command_ship_loop(State) ->
173165 receive
174166 {enable_distress_beacon } ->
175167 process_flag (trap_exit , true ),
176- command_ship_loop (State # ship_state {trap_exit = true });
177-
168+ command_ship_loop (State # ship_state {trap_exit = true });
178169 {register_ship , Name , Pid } ->
179170 NewRegistry = [{Name , Pid } | State # ship_state .ships_registry ],
180- command_ship_loop (State # ship_state {ships_registry = NewRegistry });
181-
171+ command_ship_loop (State # ship_state {ships_registry = NewRegistry });
182172 {ship_exists , Name , From } ->
183173 Exists = lists :keymember (Name , 1 , State # ship_state .ships_registry ),
184174 From ! {ship_exists_response , Exists },
185175 command_ship_loop (State );
186-
187176 {link_to , Ship } ->
188177 link (Ship ),
189178 command_ship_loop (State );
190-
191179 {auto_replace , Enable } ->
192- command_ship_loop (State # ship_state {auto_replace = Enable });
193-
180+ command_ship_loop (State # ship_state {auto_replace = Enable });
194181 {get_last_distress , From } ->
195182 From ! {last_distress , State # ship_state .last_distress },
196183 command_ship_loop (State );
197-
198184 {count_ships , Name , From } ->
199185 Count = length ([X || {N , _ } = X <- State # ship_state .ships_registry , N =:= Name ]),
200186 From ! {ship_count , Count },
201187 command_ship_loop (State );
202-
203188 {get_ship , Name , From } ->
204189 case lists :keyfind (Name , 1 , State # ship_state .ships_registry ) of
205- {Name , Pid } -> From ! {ship_pid , Pid };
206- false -> From ! {ship_pid , undefined }
190+ {Name , Pid } ->
191+ From ! {ship_pid , Pid };
192+ false ->
193+ From ! {ship_pid , undefined }
207194 end ,
208195 command_ship_loop (State );
209-
210196 {distress_signal , Ship , Ref , Message } ->
211197 case lists :keyfind (Ship , 2 , State # ship_state .ships_registry ) of
212198 {Name , Ship } ->
213199 DistressSignals = [{distress , Name , Message , Ref } | State # ship_state .distress_signals ],
214- command_ship_loop (State # ship_state {
215- distress_signals = DistressSignals ,
216- last_distress = {distress , Name , Message , Ref }
217- });
200+ command_ship_loop (State # ship_state {distress_signals = DistressSignals ,
201+ last_distress = {distress , Name , Message , Ref }});
218202 false ->
219203 command_ship_loop (State )
220204 end ;
221-
222205 {get_distress_ref , Ref , From } ->
223206 Signal = lists :keyfind (Ref , 4 , State # ship_state .distress_signals ),
224207 From ! {distress_ref_response , Signal },
225208 command_ship_loop (State );
226-
227209 {'EXIT' , Pid , {ship_destroyed , Reason }} ->
228210 case lists :keyfind (Pid , 2 , State # ship_state .ships_registry ) of
229211 {Name , Pid } ->
230- NewState = State # ship_state {
231- last_distress = {ship_lost , Name , Reason },
232- ships_registry = lists : keydelete ( Pid , 2 , State # ship_state . ships_registry )
233- },
234-
212+ NewState =
213+ State # ship_state { last_distress = {ship_lost , Name , Reason },
214+ ships_registry =
215+ lists : keydelete ( Pid , 2 , State # ship_state . ships_registry ) },
216+
235217 % Auto-replace if enabled
236- FinalState = case NewState # ship_state .auto_replace of
237- true ->
238- NewShip = spawn (fun () -> pirate_ship_loop (# ship_state {name = Name }) end ),
239- NewRegistry = [{Name , NewShip } | NewState # ship_state .ships_registry ],
240- link (NewShip ),
241- NewState # ship_state {ships_registry = NewRegistry };
242- false ->
243- NewState
244- end ,
245-
218+ FinalState =
219+ case NewState # ship_state .auto_replace of
220+ true ->
221+ NewShip = spawn (fun () -> pirate_ship_loop (# ship_state {name = Name }) end ),
222+ NewRegistry = [{Name , NewShip } | NewState # ship_state .ships_registry ],
223+ link (NewShip ),
224+ NewState # ship_state {ships_registry = NewRegistry };
225+ false ->
226+ NewState
227+ end ,
228+
246229 command_ship_loop (FinalState );
247230 false ->
248231 command_ship_loop (State )
249232 end ;
250-
251233 {register_auth_ref , Ref } ->
252234 AuthRefs = [Ref | State # ship_state .auth_refs ],
253- command_ship_loop (State # ship_state {auth_refs = AuthRefs });
254-
235+ command_ship_loop (State # ship_state {auth_refs = AuthRefs });
255236 {auth_check , Ref , From } ->
256237 Result = lists :member (Ref , State # ship_state .auth_refs ),
257238 From ! {auth_result , Result },
258239 command_ship_loop (State );
259-
260240 _Other ->
261241 command_ship_loop (State )
262242 end .
263243
264244% % Main loop for pirate ship processes
265245pirate_ship_loop (State ) ->
266246 receive
267- {message , FromShip , {Ref , Content }} ->
247+ {message , _FromShip , {Ref , Content }} ->
268248 NewRefs = [Ref | State # ship_state .received_refs ],
269249 NewMessages = [{Ref , Content } | State # ship_state .messages ],
270- pirate_ship_loop (State # ship_state {
271- received_refs = NewRefs ,
272- messages = NewMessages
273- });
274-
250+ pirate_ship_loop (State # ship_state {received_refs = NewRefs , messages = NewMessages });
275251 {check_received , Ref , From } ->
276252 Result = lists :member (Ref , State # ship_state .received_refs ),
277253 From ! {received_check , Result },
278254 pirate_ship_loop (State );
279-
280255 {get_message , Ref , From } ->
281- Message = case lists :keyfind (Ref , 1 , State # ship_state .messages ) of
282- {Ref , Content } -> Content ;
283- false -> undefined
284- end ,
256+ Message =
257+ case lists :keyfind (Ref , 1 , State # ship_state .messages ) of
258+ {Ref , Content } ->
259+ Content ;
260+ false ->
261+ undefined
262+ end ,
285263 From ! {message_response , Message },
286264 pirate_ship_loop (State );
287-
288265 {link_to , Ship } ->
289266 link (Ship ),
290267 pirate_ship_loop (State );
291-
292268 {register_auth_ref , Ref } ->
293269 AuthRefs = [Ref | State # ship_state .auth_refs ],
294- pirate_ship_loop (State # ship_state {auth_refs = AuthRefs });
295-
296- {auth_message , FromShip , AuthRef , Message } ->
270+ pirate_ship_loop (State # ship_state {auth_refs = AuthRefs });
271+ {auth_message , _FromShip , AuthRef , Message } ->
297272 % Store the auth message if we have the ref registered
298273 case lists :member (AuthRef , State # ship_state .auth_refs ) of
299274 true ->
300275 NewMessages = [{AuthRef , Message } | State # ship_state .messages ],
301- pirate_ship_loop (State # ship_state {messages = NewMessages });
276+ pirate_ship_loop (State # ship_state {messages = NewMessages });
302277 false ->
303278 pirate_ship_loop (State )
304279 end ;
305-
306280 {auth_check , Ref , From } ->
307281 Result = lists :member (Ref , State # ship_state .auth_refs ),
308282 From ! {auth_result , Result },
309283 pirate_ship_loop (State );
310-
311284 _Other ->
312285 pirate_ship_loop (State )
313286 end .
0 commit comments