From 25ceb5362c137ac1fd051d9e8fb497ae2e3b7d12 Mon Sep 17 00:00:00 2001 From: Maartz Date: Sun, 30 Mar 2025 21:37:52 +0200 Subject: [PATCH 01/11] add space-pirates to the config --- config.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config.json b/config.json index 3175db29..6eae6a4a 100644 --- a/config.json +++ b/config.json @@ -1021,6 +1021,19 @@ "practices": [], "prerequisites": [], "difficulty": 5 + }, + { + "slug": "space-pirates", + "name": "space-pirates", + "uuid": "c9e78be6-8e53-4507-a143-ae927069d901", + "practices": [], + "prerequisites": [], + "difficulty": 5, + "topics": [ + "processes", + "references", + "concurrency" + ] } ], "foregone": [ From 2eb9d25e100ab11048f89351c6b50439c760a49f Mon Sep 17 00:00:00 2001 From: Maartz Date: Sun, 30 Mar 2025 21:38:22 +0200 Subject: [PATCH 02/11] add space pirates exercise --- .../space-pirates/.docs/instructions.md | 34 ++ .../space-pirates/.docs/introduction.md | 10 + .../practice/space-pirates/.meta/config.json | 17 + .../practice/space-pirates/.meta/example.erl | 313 ++++++++++++++++++ .../practice/space-pirates/hints/README.md | 4 + .../practice/space-pirates/hints/hint_1.md | 9 + .../practice/space-pirates/hints/hint_2.md | 15 + .../practice/space-pirates/hints/hint_3.md | 62 ++++ exercises/practice/space-pirates/rebar.config | 30 ++ .../space-pirates/src/space_pirates.app.src | 10 + .../space-pirates/src/space_pirates.erl | 0 .../test/space_pirates_tests.erl | 0 12 files changed, 504 insertions(+) create mode 100644 exercises/practice/space-pirates/.docs/instructions.md create mode 100644 exercises/practice/space-pirates/.docs/introduction.md create mode 100644 exercises/practice/space-pirates/.meta/config.json create mode 100644 exercises/practice/space-pirates/.meta/example.erl create mode 100644 exercises/practice/space-pirates/hints/README.md create mode 100644 exercises/practice/space-pirates/hints/hint_1.md create mode 100644 exercises/practice/space-pirates/hints/hint_2.md create mode 100644 exercises/practice/space-pirates/hints/hint_3.md create mode 100644 exercises/practice/space-pirates/rebar.config create mode 100644 exercises/practice/space-pirates/src/space_pirates.app.src create mode 100644 exercises/practice/space-pirates/src/space_pirates.erl create mode 100644 exercises/practice/space-pirates/test/space_pirates_tests.erl diff --git a/exercises/practice/space-pirates/.docs/instructions.md b/exercises/practice/space-pirates/.docs/instructions.md new file mode 100644 index 00000000..5c0d6f7c --- /dev/null +++ b/exercises/practice/space-pirates/.docs/instructions.md @@ -0,0 +1,34 @@ +# Instructions + +Your task is to implement a reliable communication system for Captain Nova's pirate fleet using Erlang's process model. The system needs to handle ship-to-ship communication, distress signals, authentication, and automatic ship replacement when one is destroyed. +Core Requirements + +Command Ship - Implement a central command ship process that acts as a supervisor for the fleet +Pirate Ships - Create ship processes that can send and receive authenticated messages +Distress Beacon - Enable command ships to trap exit signals when pirate ships are destroyed +Message Authentication - Use Erlang references to create secure communications between ships +Ship Registry - Maintain a registry of active ships in the fleet +Auto-Replacement - When a ship is destroyed, automatically spawn a replacement + +Concepts You'll Be Using + +Process Creation: Using spawn/1 and spawn_link/1 to create processes +Message Passing: Sending messages between processes using the ! operator +Process Linking: Using link/1 to connect processes +Exit Signal Handling: Using process_flag(trap_exit, true) to catch process termination +References: Using make_ref/0 to create unique identifiers for messages +Process Registration: Maintaining a registry of processes + +## Your Task + +Implement the functions in the space_pirates.erl module to create a robust, fault-tolerant messaging system that can: + +Start a command ship that can monitor other ships +Spawn pirate ships that can communicate with each other +Enable the distress beacon to trap exit signals +Create unique references for mission authentication +Send authenticated messages between ships +Handle ship destruction and automatic replacement + +Remember, in deep space, there's no room for failure. Ships get destroyed, communications get intercepted, and the Empire is always watching. Your system needs to be reliable, secure, and resilient. +Good luck, Communication Officer. The fleet's success depends on you. diff --git a/exercises/practice/space-pirates/.docs/introduction.md b/exercises/practice/space-pirates/.docs/introduction.md new file mode 100644 index 00000000..51641d3a --- /dev/null +++ b/exercises/practice/space-pirates/.docs/introduction.md @@ -0,0 +1,10 @@ +# Introduction + +In the 30th century, interstellar travel has become commonplace, and with it, the age-old profession of piracy has expanded to the stars. Captains with fast ships and loyal crews plunder trade routes across the galaxy, always staying one step ahead of the Imperial Fleet. + +You've recently joined the crew of the notorious Captain Nova, whose fleet of pirate ships is known throughout the Perseus Arm. As the new communications officer, you've been tasked with implementing their distributed messaging system. The previous officer was spaced after a critical failure that left three ships stranded during a raid on an Imperial convoy. + +"Listen carefully," Captain Nova tells you on your first day. "Our ships operate across vast distances. A fleet is only as strong as its communication network. When a ship goes down—and they do go down—I need to know immediately. If a ship sends a distress signal, it needs to reach command with proper authentication. No Imperial interference, no message spoofing. And most importantly, when we lose a ship, I need an immediate replacement dispatched. Understood?" + +You gulp nervously. "I'll need to use **Erlang**'s concurrency model—processes, links, monitors, and references." +Captain Nova grins. "*Precisely*. Now get to work before I find another communications officer." diff --git a/exercises/practice/space-pirates/.meta/config.json b/exercises/practice/space-pirates/.meta/config.json new file mode 100644 index 00000000..2bfafaca --- /dev/null +++ b/exercises/practice/space-pirates/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [ + "Maartz" + ], + "files": { + "solution": [ + "src/space_pirates.erl" + ], + "test": [ + "test/space_pirates_tests.erl" + ], + "example": [ + ".meta/example.erl" + ] + }, + "blurb": "Learn Erlang's process model by building a communication system for a fleet of space pirates." +} diff --git a/exercises/practice/space-pirates/.meta/example.erl b/exercises/practice/space-pirates/.meta/example.erl new file mode 100644 index 00000000..e4abcf59 --- /dev/null +++ b/exercises/practice/space-pirates/.meta/example.erl @@ -0,0 +1,313 @@ +-module(example). + +-export([ + start_command_ship/0, + spawn_pirate_ship/2, + ship_exists/2, + create_mission_ref/0, + send_message/3, + message_received/2, + get_last_message/2, + enable_distress_beacon/1, + link_ships/2, + destroy_ship/2, + get_last_distress_signal/1, + enable_auto_replacement/2, + count_ships_by_name/2, + get_ship_by_name/2, + send_distress_signal/4, + get_distress_signal_by_ref/2, + create_auth_ref/2, + send_authenticated_message/4, + authenticate_message/2 +]). + +%% Internal ship state record +-record(ship_state, { + name, + messages = [], + received_refs = [], + auth_refs = [], + trap_exit = false, + distress_signals = [], + ships_registry = [], + auto_replace = false, + last_distress = undefined +}). + +%% Start a command ship process that can act as a supervisor +start_command_ship() -> + spawn_link(fun() -> command_ship_loop(#ship_state{name="Command"}) end). + +%% Spawn a new pirate ship process with the given name +spawn_pirate_ship(CommandShip, Name) -> + PirateShip = spawn(fun() -> pirate_ship_loop(#ship_state{name=Name}) end), + CommandShip ! {register_ship, Name, PirateShip}, + PirateShip. + +%% Check if a ship with the given name exists in the command ship's registry +ship_exists(CommandShip, Name) -> + CommandShip ! {ship_exists, Name, self()}, + receive + {ship_exists_response, Result} -> Result + after 1000 -> + false + end. + +%% Create a unique mission reference +create_mission_ref() -> + make_ref(). + +%% Send a message from one ship to another +send_message(FromShip, ToShip, Message) -> + ToShip ! {message, FromShip, Message}, + ok. + +%% Check if a message with the given reference was received +message_received(Ship, Ref) -> + Ship ! {check_received, Ref, self()}, + receive + {received_check, Result} -> Result + after 1000 -> + false + end. + +%% Retrieve the last message with the given reference +get_last_message(Ship, Ref) -> + Ship ! {get_message, Ref, self()}, + receive + {message_response, Message} -> Message + after 1000 -> + undefined + end. + +%% Enable the command ship to trap exit signals from linked ships +enable_distress_beacon(CommandShip) -> + CommandShip ! {enable_distress_beacon}, + ok. + +%% Link two ship processes +link_ships(ShipA, ShipB) -> + link_process(ShipA, ShipB), + ok. + +%% Helper function to link processes +link_process(Ship, ToLink) -> + Ship ! {link_to, ToLink}, + ok. + +%% Destroy a ship with a reason +destroy_ship(Ship, Reason) -> + exit(Ship, {ship_destroyed, Reason}), + ok. + +%% Get the last distress signal received by the command ship +get_last_distress_signal(CommandShip) -> + CommandShip ! {get_last_distress, self()}, + receive + {last_distress, Signal} -> Signal + after 1000 -> + undefined + end. + +%% Enable or disable automatic replacement of destroyed ships +enable_auto_replacement(CommandShip, Enable) -> + CommandShip ! {auto_replace, Enable}, + ok. + +%% Count the number of ships with a specific name +count_ships_by_name(CommandShip, Name) -> + CommandShip ! {count_ships, Name, self()}, + receive + {ship_count, Count} -> Count + after 1000 -> + 0 + end. + +%% Get the process ID of a ship by its name +get_ship_by_name(CommandShip, Name) -> + CommandShip ! {get_ship, Name, self()}, + receive + {ship_pid, Pid} -> Pid + after 1000 -> + undefined + end. + +%% Send a distress signal with a reference from a ship to the command ship +send_distress_signal(Ship, CommandShip, Ref, Message) -> + CommandShip ! {distress_signal, Ship, Ref, Message}, + ok. + +%% Get a specific distress signal by its reference +get_distress_signal_by_ref(CommandShip, Ref) -> + CommandShip ! {get_distress_ref, Ref, self()}, + receive + {distress_ref_response, Signal} -> Signal + after 1000 -> + undefined + end. + +%% Create an authentication reference for secure communication between ships +create_auth_ref(FromShip, ToShip) -> + Ref = make_ref(), + FromShip ! {register_auth_ref, Ref}, + ToShip ! {register_auth_ref, Ref}, + Ref. + +%% Send an authenticated message between ships +send_authenticated_message(FromShip, ToShip, AuthRef, Message) -> + ToShip ! {auth_message, FromShip, AuthRef, Message}, + ok. + +%% Verify if a message has a valid authentication reference +authenticate_message(Ship, AuthRef) -> + Ship ! {auth_check, AuthRef, self()}, + receive + {auth_result, Result} -> Result + after 1000 -> + false + end. + +%% Main loop for the command ship process +command_ship_loop(State) -> + receive + {enable_distress_beacon} -> + process_flag(trap_exit, true), + command_ship_loop(State#ship_state{trap_exit=true}); + + {register_ship, Name, Pid} -> + NewRegistry = [{Name, Pid} | State#ship_state.ships_registry], + command_ship_loop(State#ship_state{ships_registry=NewRegistry}); + + {ship_exists, Name, From} -> + Exists = lists:keymember(Name, 1, State#ship_state.ships_registry), + From ! {ship_exists_response, Exists}, + command_ship_loop(State); + + {link_to, Ship} -> + link(Ship), + command_ship_loop(State); + + {auto_replace, Enable} -> + command_ship_loop(State#ship_state{auto_replace=Enable}); + + {get_last_distress, From} -> + From ! {last_distress, State#ship_state.last_distress}, + command_ship_loop(State); + + {count_ships, Name, From} -> + Count = length([X || {N, _} = X <- State#ship_state.ships_registry, N =:= Name]), + From ! {ship_count, Count}, + command_ship_loop(State); + + {get_ship, Name, From} -> + case lists:keyfind(Name, 1, State#ship_state.ships_registry) of + {Name, Pid} -> From ! {ship_pid, Pid}; + false -> From ! {ship_pid, undefined} + end, + command_ship_loop(State); + + {distress_signal, Ship, Ref, Message} -> + case lists:keyfind(Ship, 2, State#ship_state.ships_registry) of + {Name, Ship} -> + DistressSignals = [{distress, Name, Message, Ref} | State#ship_state.distress_signals], + command_ship_loop(State#ship_state{ + distress_signals=DistressSignals, + last_distress={distress, Name, Message, Ref} + }); + false -> + command_ship_loop(State) + end; + + {get_distress_ref, Ref, From} -> + Signal = lists:keyfind(Ref, 4, State#ship_state.distress_signals), + From ! {distress_ref_response, Signal}, + command_ship_loop(State); + + {'EXIT', Pid, {ship_destroyed, Reason}} -> + case lists:keyfind(Pid, 2, State#ship_state.ships_registry) of + {Name, Pid} -> + NewState = State#ship_state{ + last_distress={ship_lost, Name, Reason}, + ships_registry=lists:keydelete(Pid, 2, State#ship_state.ships_registry) + }, + + % Auto-replace if enabled + FinalState = case NewState#ship_state.auto_replace of + true -> + NewShip = spawn(fun() -> pirate_ship_loop(#ship_state{name=Name}) end), + NewRegistry = [{Name, NewShip} | NewState#ship_state.ships_registry], + link(NewShip), + NewState#ship_state{ships_registry=NewRegistry}; + false -> + NewState + end, + + command_ship_loop(FinalState); + false -> + command_ship_loop(State) + end; + + {register_auth_ref, Ref} -> + AuthRefs = [Ref | State#ship_state.auth_refs], + command_ship_loop(State#ship_state{auth_refs=AuthRefs}); + + {auth_check, Ref, From} -> + Result = lists:member(Ref, State#ship_state.auth_refs), + From ! {auth_result, Result}, + command_ship_loop(State); + + _Other -> + command_ship_loop(State) + end. + +%% Main loop for pirate ship processes +pirate_ship_loop(State) -> + receive + {message, FromShip, {Ref, Content}} -> + NewRefs = [Ref | State#ship_state.received_refs], + NewMessages = [{Ref, Content} | State#ship_state.messages], + pirate_ship_loop(State#ship_state{ + received_refs=NewRefs, + messages=NewMessages + }); + + {check_received, Ref, From} -> + Result = lists:member(Ref, State#ship_state.received_refs), + From ! {received_check, Result}, + pirate_ship_loop(State); + + {get_message, Ref, From} -> + Message = case lists:keyfind(Ref, 1, State#ship_state.messages) of + {Ref, Content} -> Content; + false -> undefined + end, + From ! {message_response, Message}, + pirate_ship_loop(State); + + {link_to, Ship} -> + link(Ship), + pirate_ship_loop(State); + + {register_auth_ref, Ref} -> + AuthRefs = [Ref | State#ship_state.auth_refs], + pirate_ship_loop(State#ship_state{auth_refs=AuthRefs}); + + {auth_message, FromShip, AuthRef, Message} -> + % Store the auth message if we have the ref registered + case lists:member(AuthRef, State#ship_state.auth_refs) of + true -> + NewMessages = [{AuthRef, Message} | State#ship_state.messages], + pirate_ship_loop(State#ship_state{messages=NewMessages}); + false -> + pirate_ship_loop(State) + end; + + {auth_check, Ref, From} -> + Result = lists:member(Ref, State#ship_state.auth_refs), + From ! {auth_result, Result}, + pirate_ship_loop(State); + + _Other -> + pirate_ship_loop(State) + end. diff --git a/exercises/practice/space-pirates/hints/README.md b/exercises/practice/space-pirates/hints/README.md new file mode 100644 index 00000000..045ab306 --- /dev/null +++ b/exercises/practice/space-pirates/hints/README.md @@ -0,0 +1,4 @@ +# Readme + +This directory contains some hints to help you complete this exercise. The +higher the number of a hint file, the more explicit the hint. diff --git a/exercises/practice/space-pirates/hints/hint_1.md b/exercises/practice/space-pirates/hints/hint_1.md new file mode 100644 index 00000000..c5b926ca --- /dev/null +++ b/exercises/practice/space-pirates/hints/hint_1.md @@ -0,0 +1,9 @@ +# hint_1 + +The Basics of Process Communication + +In Erlang, every communication between processes happens through message passing. Think of your ships not as objects with methods, but as independent beings that can only talk to each other by sending messages. + +When a ship goes down, it doesn't call a function - it simply stops receiving messages. How would other ships know it's gone? You'll need to establish connections between your processes that can detect when one disappears. + +Remember: in space, no one can hear you scream - unless you've set up the right message handling! diff --git a/exercises/practice/space-pirates/hints/hint_2.md b/exercises/practice/space-pirates/hints/hint_2.md new file mode 100644 index 00000000..045be52b --- /dev/null +++ b/exercises/practice/space-pirates/hints/hint_2.md @@ -0,0 +1,15 @@ +# hint_2 + +Monitoring Your Fleet + +To build a robust fleet communication system, you'll need to use these key Erlang concepts: + +link/1 - Creates a bidirectional connection between processes +process_flag(trap_exit, true) - Makes a process receive notifications when linked processes die +spawn_link/1 - Creates a new process that's automatically linked to its parent + +When setting up your command ship, make sure it can "trap exits" so it receives notifications when ships are destroyed. + +Your message pattern matching should look for {'EXIT', Pid, Reason} messages that indicate a ship has gone down. + +For secure communications, remember that make_ref/0 creates a unique reference that can't be guessed - perfect for authenticating messages between ships! diff --git a/exercises/practice/space-pirates/hints/hint_3.md b/exercises/practice/space-pirates/hints/hint_3.md new file mode 100644 index 00000000..e5d19a46 --- /dev/null +++ b/exercises/practice/space-pirates/hints/hint_3.md @@ -0,0 +1,62 @@ +# hint_3 + +Implementation Blueprint + +Here's a detailed structure for your solution: + +```erlang + +%% Command ship state management +-record(command_state, { + ships = #{}, % Map of ship name to Pid + trap_exit = false, % Is the ship currently trapping exits? + distress_log = [], % List of received distress signals + auth_refs = sets:new() % Set of valid authentication references +}). + +%% Pirate ship state management +-record(ship_state, { + name, + messages = #{}, % Map of Ref to message content + auth_refs = sets:new() % Set of valid authentication refs +}). + +%% Handle the 'EXIT' signal in your command ship loop +command_loop(State) -> + receive + {'EXIT', Pid, {ship_destroyed, Reason}} -> + %% 1. Find the ship's name from its Pid + ShipName = maps:fold(fun(Name, P, Acc) -> + if P =:= Pid -> Name; true -> Acc end + end, undefined, State#command_state.ships), + + %% 2. Remove the ship from the registry + NewShips = maps:remove(ShipName, State#command_state.ships), + + %% 3. Log the distress + NewLog = [{ShipName, Reason, erlang:timestamp()} | State#command_state.distress_log], + + %% 4. Spawn replacement if auto-replace is enabled + FinalShips = case State#command_state.auto_replace of + true -> + NewShip = spawn_link(fun() -> ship_loop(#ship_state{name=ShipName}) end), + maps:put(ShipName, NewShip, NewShips); + false -> + NewShips + end, + + command_loop(State#command_state{ships=FinalShips, distress_log=NewLog}); + + %% Handle other messages... + end. + +``` + +For secure message authentication: + +Create references with make_ref() +Register them with both sender and receiver +Verify that incoming messages contain a registered reference +Remember that pirates can't be trusted - always verify before accepting a message! + +The command ship should maintain the fleet registry and be the central point for monitoring ship status. diff --git a/exercises/practice/space-pirates/rebar.config b/exercises/practice/space-pirates/rebar.config new file mode 100644 index 00000000..db5d9076 --- /dev/null +++ b/exercises/practice/space-pirates/rebar.config @@ -0,0 +1,30 @@ +%% Erlang compiler options +{erl_opts, [debug_info, warnings_as_errors]}. + +{deps, [{erl_exercism, "0.1.2"}]}. + +{dialyzer, [ + {warnings, [underspecs, no_return]}, + {get_warnings, true}, + {plt_apps, top_level_deps}, % top_level_deps | all_deps + {plt_extra_apps, []}, + {plt_location, local}, % local | "/my/file/name" + {plt_prefix, "rebar3"}, + {base_plt_apps, [stdlib, kernel, crypto]}, + {base_plt_location, global}, % global | "/my/file/name" + {base_plt_prefix, "rebar3"} +]}. + +%% eunit:test(Tests) +{eunit_tests, []}. +%% Options for eunit:test(Tests, Opts) +{eunit_opts, [verbose]}. + +%% == xref == + +{xref_warnings, true}. + +%% xref checks to run +{xref_checks, [undefined_function_calls, undefined_functions, + locals_not_used, exports_not_used, + deprecated_function_calls, deprecated_functions]}. diff --git a/exercises/practice/space-pirates/src/space_pirates.app.src b/exercises/practice/space-pirates/src/space_pirates.app.src new file mode 100644 index 00000000..ac0160e1 --- /dev/null +++ b/exercises/practice/space-pirates/src/space_pirates.app.src @@ -0,0 +1,10 @@ +{application, space_pirates, + [{description, "exercism.org - space-pirates"}, + {vsn, "0.0.1"}, + {modules, []}, + {registered, []}, + {applications, [kernel, + stdlib]}, + {env, []} + ]}. + diff --git a/exercises/practice/space-pirates/src/space_pirates.erl b/exercises/practice/space-pirates/src/space_pirates.erl new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/space-pirates/test/space_pirates_tests.erl b/exercises/practice/space-pirates/test/space_pirates_tests.erl new file mode 100644 index 00000000..e69de29b From 8ddf726e2d4effbbb147d1de478388a005a9b78c Mon Sep 17 00:00:00 2001 From: Maartz Date: Sun, 30 Mar 2025 21:44:16 +0200 Subject: [PATCH 03/11] add stub --- exercises/practice/space-pirates/src/space_pirates.erl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/practice/space-pirates/src/space_pirates.erl b/exercises/practice/space-pirates/src/space_pirates.erl index e69de29b..042c02b2 100644 --- a/exercises/practice/space-pirates/src/space_pirates.erl +++ b/exercises/practice/space-pirates/src/space_pirates.erl @@ -0,0 +1,2 @@ +-module(space_pirates). +-compile(export_all). From 10f5c8f16c9415f00e625e8180703ab649fc62a0 Mon Sep 17 00:00:00 2001 From: Maartz Date: Sun, 30 Mar 2025 21:44:38 +0200 Subject: [PATCH 04/11] add tests --- .../test/space_pirates_tests.erl | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/exercises/practice/space-pirates/test/space_pirates_tests.erl b/exercises/practice/space-pirates/test/space_pirates_tests.erl index e69de29b..7c704ad3 100644 --- a/exercises/practice/space-pirates/test/space_pirates_tests.erl +++ b/exercises/practice/space-pirates/test/space_pirates_tests.erl @@ -0,0 +1,116 @@ +-module(space_pirates_tests). + +-include_lib("erl_exercism/include/exercism.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +command_ship_start_test_() -> + {"Command ship can be started and is a valid process", + ?_assertMatch(Pid when is_pid(Pid), space_pirates:start_command_ship())}. + +spawn_pirate_ship_test_() -> + {"A pirate ship can be spawned with a name and returns a valid process ID", + fun() -> + CommandShip = space_pirates:start_command_ship(), + PirateShip = space_pirates:spawn_pirate_ship(CommandShip, "Black Pearl"), + ?assert(is_pid(PirateShip)), + ?assertEqual(true, space_pirates:ship_exists(CommandShip, "Black Pearl")) + end}. + +create_mission_ref_test_() -> + {"Mission references are created as unique references", + fun() -> + Ref1 = space_pirates:create_mission_ref(), + Ref2 = space_pirates:create_mission_ref(), + ?assert(is_reference(Ref1)), + ?assert(is_reference(Ref2)), + ?assertNotEqual(Ref1, Ref2) + end}. + +message_sending_test_() -> + {"Ships can send and receive messages with proper authentication", + fun() -> + CommandShip = space_pirates:start_command_ship(), + ShipA = space_pirates:spawn_pirate_ship(CommandShip, "Stellar Blade"), + ShipB = space_pirates:spawn_pirate_ship(CommandShip, "Crimson Raider"), + MissionRef = space_pirates:create_mission_ref(), + Message = "Coordinates to treasure: Sector 7G", + ?assertEqual(ok, space_pirates:send_message(ShipA, ShipB, {MissionRef, Message})), + timer:sleep(50), % Small delay to ensure message delivery + ?assertEqual(true, space_pirates:message_received(ShipB, MissionRef)), + ?assertEqual(Message, space_pirates:get_last_message(ShipB, MissionRef)) + end}. + +enable_distress_beacon_test_() -> + {"Command ship can enable distress beacon to trap exit signals", + fun() -> + CommandShip = space_pirates:start_command_ship(), + ?assertEqual(ok, space_pirates:enable_distress_beacon(CommandShip)), + {status, _, _, StatusInfo} = sys:get_status(CommandShip), + [_, _, _, _, Lists] = StatusInfo, + {data, [{"State", StateData}]} = Lists, + ?assertEqual(true, proplists:get_value(trap_exit, StateData)) + end}. + +ship_destruction_test_() -> + {"When a ship is destroyed, the command ship receives an exit signal", + fun() -> + CommandShip = space_pirates:start_command_ship(), + space_pirates:enable_distress_beacon(CommandShip), + PirateShip = space_pirates:spawn_pirate_ship(CommandShip, "Doomed Vessel"), + ?assertEqual(ok, space_pirates:link_ships(CommandShip, PirateShip)), + ?assertEqual(ok, space_pirates:destroy_ship(PirateShip, "Hit by asteroid")), + timer:sleep(100), % Give time for exit signal to be processed + ?assertEqual( + {ship_lost, "Doomed Vessel", "Hit by asteroid"}, + space_pirates:get_last_distress_signal(CommandShip) + ) + end}. + +ship_replacement_test_() -> + {"Command ship can automatically replace destroyed ships", + fun() -> + CommandShip = space_pirates:start_command_ship(), + space_pirates:enable_distress_beacon(CommandShip), + space_pirates:enable_auto_replacement(CommandShip, true), + OriginalShip = space_pirates:spawn_pirate_ship(CommandShip, "Replaceable"), + space_pirates:link_ships(CommandShip, OriginalShip), + ?assertEqual(1, space_pirates:count_ships_by_name(CommandShip, "Replaceable")), + space_pirates:destroy_ship(OriginalShip, "Battle damage"), + timer:sleep(200), % Give time for replacement to occur + ?assertEqual(1, space_pirates:count_ships_by_name(CommandShip, "Replaceable")), + NewShip = space_pirates:get_ship_by_name(CommandShip, "Replaceable"), + ?assertNotEqual(OriginalShip, NewShip) + end}. + +%% Test distress signal with reference +distress_signal_with_ref_test_() -> + {"Distress signals include unique references for tracking", + fun() -> + CommandShip = space_pirates:start_command_ship(), + space_pirates:enable_distress_beacon(CommandShip), + PirateShip = space_pirates:spawn_pirate_ship(CommandShip, "Imperiled"), + DistressRef = make_ref(), + ?assertEqual(ok, space_pirates:send_distress_signal(PirateShip, CommandShip, DistressRef, "Under attack")), + timer:sleep(50), % Allow time for message processing + ?assertEqual( + {distress, "Imperiled", "Under attack", DistressRef}, + space_pirates:get_distress_signal_by_ref(CommandShip, DistressRef) + ) + end}. + +message_authentication_test_() -> + {"Messages between ships must be properly authenticated with references", + fun() -> + CommandShip = space_pirates:start_command_ship(), + ShipA = space_pirates:spawn_pirate_ship(CommandShip, "Sender"), + ShipB = space_pirates:spawn_pirate_ship(CommandShip, "Receiver"), + AuthRef = space_pirates:create_auth_ref(ShipA, ShipB), + Message = "Secret plans", + ?assertEqual(ok, space_pirates:send_authenticated_message(ShipA, ShipB, AuthRef, Message)), + timer:sleep(50), + ?assertEqual(true, space_pirates:authenticate_message(ShipB, AuthRef)), + FakeRef = make_ref(), + ?assertEqual(ok, space_pirates:send_authenticated_message(ShipA, ShipB, FakeRef, "Fake message")), + timer:sleep(50), + ?assertEqual(false, space_pirates:authenticate_message(ShipB, FakeRef)) + end}. From 6c019c978e009779ada065f7fdebb9c0e80dec23 Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 16:23:28 +0200 Subject: [PATCH 05/11] remove export_all --- exercises/practice/space-pirates/src/space_pirates.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/practice/space-pirates/src/space_pirates.erl b/exercises/practice/space-pirates/src/space_pirates.erl index 042c02b2..a7033b37 100644 --- a/exercises/practice/space-pirates/src/space_pirates.erl +++ b/exercises/practice/space-pirates/src/space_pirates.erl @@ -1,2 +1 @@ -module(space_pirates). --compile(export_all). From 118b333f13839d6e3c807cc5b8dc5e7128a77305 Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 16:35:56 +0200 Subject: [PATCH 06/11] format and comment unused variables --- .../practice/space-pirates/.meta/example.erl | 171 ++++++++---------- 1 file changed, 72 insertions(+), 99 deletions(-) diff --git a/exercises/practice/space-pirates/.meta/example.erl b/exercises/practice/space-pirates/.meta/example.erl index e4abcf59..c7242d4c 100644 --- a/exercises/practice/space-pirates/.meta/example.erl +++ b/exercises/practice/space-pirates/.meta/example.erl @@ -1,47 +1,31 @@ -module(example). --export([ - start_command_ship/0, - spawn_pirate_ship/2, - ship_exists/2, - create_mission_ref/0, - send_message/3, - message_received/2, - get_last_message/2, - enable_distress_beacon/1, - link_ships/2, - destroy_ship/2, - get_last_distress_signal/1, - enable_auto_replacement/2, - count_ships_by_name/2, - get_ship_by_name/2, - send_distress_signal/4, - get_distress_signal_by_ref/2, - create_auth_ref/2, - send_authenticated_message/4, - authenticate_message/2 -]). +-export([start_command_ship/0, spawn_pirate_ship/2, ship_exists/2, create_mission_ref/0, + send_message/3, message_received/2, get_last_message/2, enable_distress_beacon/1, + link_ships/2, destroy_ship/2, get_last_distress_signal/1, enable_auto_replacement/2, + count_ships_by_name/2, get_ship_by_name/2, send_distress_signal/4, + get_distress_signal_by_ref/2, create_auth_ref/2, send_authenticated_message/4, + authenticate_message/2]). %% Internal ship state record --record(ship_state, { - name, - messages = [], - received_refs = [], - auth_refs = [], - trap_exit = false, - distress_signals = [], - ships_registry = [], - auto_replace = false, - last_distress = undefined -}). +-record(ship_state, + {name, + messages = [], + received_refs = [], + auth_refs = [], + trap_exit = false, + distress_signals = [], + ships_registry = [], + auto_replace = false, + last_distress = undefined}). %% Start a command ship process that can act as a supervisor start_command_ship() -> - spawn_link(fun() -> command_ship_loop(#ship_state{name="Command"}) end). + spawn_link(fun() -> command_ship_loop(#ship_state{name = "Command"}) end). %% Spawn a new pirate ship process with the given name spawn_pirate_ship(CommandShip, Name) -> - PirateShip = spawn(fun() -> pirate_ship_loop(#ship_state{name=Name}) end), + PirateShip = spawn(fun() -> pirate_ship_loop(#ship_state{name = Name}) end), CommandShip ! {register_ship, Name, PirateShip}, PirateShip. @@ -49,7 +33,8 @@ spawn_pirate_ship(CommandShip, Name) -> ship_exists(CommandShip, Name) -> CommandShip ! {ship_exists, Name, self()}, receive - {ship_exists_response, Result} -> Result + {ship_exists_response, Result} -> + Result after 1000 -> false end. @@ -67,7 +52,8 @@ send_message(FromShip, ToShip, Message) -> message_received(Ship, Ref) -> Ship ! {check_received, Ref, self()}, receive - {received_check, Result} -> Result + {received_check, Result} -> + Result after 1000 -> false end. @@ -76,7 +62,8 @@ message_received(Ship, Ref) -> get_last_message(Ship, Ref) -> Ship ! {get_message, Ref, self()}, receive - {message_response, Message} -> Message + {message_response, Message} -> + Message after 1000 -> undefined end. @@ -105,7 +92,8 @@ destroy_ship(Ship, Reason) -> get_last_distress_signal(CommandShip) -> CommandShip ! {get_last_distress, self()}, receive - {last_distress, Signal} -> Signal + {last_distress, Signal} -> + Signal after 1000 -> undefined end. @@ -119,7 +107,8 @@ enable_auto_replacement(CommandShip, Enable) -> count_ships_by_name(CommandShip, Name) -> CommandShip ! {count_ships, Name, self()}, receive - {ship_count, Count} -> Count + {ship_count, Count} -> + Count after 1000 -> 0 end. @@ -128,7 +117,8 @@ count_ships_by_name(CommandShip, Name) -> get_ship_by_name(CommandShip, Name) -> CommandShip ! {get_ship, Name, self()}, receive - {ship_pid, Pid} -> Pid + {ship_pid, Pid} -> + Pid after 1000 -> undefined end. @@ -142,7 +132,8 @@ send_distress_signal(Ship, CommandShip, Ref, Message) -> get_distress_signal_by_ref(CommandShip, Ref) -> CommandShip ! {get_distress_ref, Ref, self()}, receive - {distress_ref_response, Signal} -> Signal + {distress_ref_response, Signal} -> + Signal after 1000 -> undefined end. @@ -163,7 +154,8 @@ send_authenticated_message(FromShip, ToShip, AuthRef, Message) -> authenticate_message(Ship, AuthRef) -> Ship ! {auth_check, AuthRef, self()}, receive - {auth_result, Result} -> Result + {auth_result, Result} -> + Result after 1000 -> false end. @@ -173,90 +165,78 @@ command_ship_loop(State) -> receive {enable_distress_beacon} -> process_flag(trap_exit, true), - command_ship_loop(State#ship_state{trap_exit=true}); - + command_ship_loop(State#ship_state{trap_exit = true}); {register_ship, Name, Pid} -> NewRegistry = [{Name, Pid} | State#ship_state.ships_registry], - command_ship_loop(State#ship_state{ships_registry=NewRegistry}); - + command_ship_loop(State#ship_state{ships_registry = NewRegistry}); {ship_exists, Name, From} -> Exists = lists:keymember(Name, 1, State#ship_state.ships_registry), From ! {ship_exists_response, Exists}, command_ship_loop(State); - {link_to, Ship} -> link(Ship), command_ship_loop(State); - {auto_replace, Enable} -> - command_ship_loop(State#ship_state{auto_replace=Enable}); - + command_ship_loop(State#ship_state{auto_replace = Enable}); {get_last_distress, From} -> From ! {last_distress, State#ship_state.last_distress}, command_ship_loop(State); - {count_ships, Name, From} -> Count = length([X || {N, _} = X <- State#ship_state.ships_registry, N =:= Name]), From ! {ship_count, Count}, command_ship_loop(State); - {get_ship, Name, From} -> case lists:keyfind(Name, 1, State#ship_state.ships_registry) of - {Name, Pid} -> From ! {ship_pid, Pid}; - false -> From ! {ship_pid, undefined} + {Name, Pid} -> + From ! {ship_pid, Pid}; + false -> + From ! {ship_pid, undefined} end, command_ship_loop(State); - {distress_signal, Ship, Ref, Message} -> case lists:keyfind(Ship, 2, State#ship_state.ships_registry) of {Name, Ship} -> DistressSignals = [{distress, Name, Message, Ref} | State#ship_state.distress_signals], - command_ship_loop(State#ship_state{ - distress_signals=DistressSignals, - last_distress={distress, Name, Message, Ref} - }); + command_ship_loop(State#ship_state{distress_signals = DistressSignals, + last_distress = {distress, Name, Message, Ref}}); false -> command_ship_loop(State) end; - {get_distress_ref, Ref, From} -> Signal = lists:keyfind(Ref, 4, State#ship_state.distress_signals), From ! {distress_ref_response, Signal}, command_ship_loop(State); - {'EXIT', Pid, {ship_destroyed, Reason}} -> case lists:keyfind(Pid, 2, State#ship_state.ships_registry) of {Name, Pid} -> - NewState = State#ship_state{ - last_distress={ship_lost, Name, Reason}, - ships_registry=lists:keydelete(Pid, 2, State#ship_state.ships_registry) - }, - + NewState = + State#ship_state{last_distress = {ship_lost, Name, Reason}, + ships_registry = + lists:keydelete(Pid, 2, State#ship_state.ships_registry)}, + % Auto-replace if enabled - FinalState = case NewState#ship_state.auto_replace of - true -> - NewShip = spawn(fun() -> pirate_ship_loop(#ship_state{name=Name}) end), - NewRegistry = [{Name, NewShip} | NewState#ship_state.ships_registry], - link(NewShip), - NewState#ship_state{ships_registry=NewRegistry}; - false -> - NewState - end, - + FinalState = + case NewState#ship_state.auto_replace of + true -> + NewShip = spawn(fun() -> pirate_ship_loop(#ship_state{name = Name}) end), + NewRegistry = [{Name, NewShip} | NewState#ship_state.ships_registry], + link(NewShip), + NewState#ship_state{ships_registry = NewRegistry}; + false -> + NewState + end, + command_ship_loop(FinalState); false -> command_ship_loop(State) end; - {register_auth_ref, Ref} -> AuthRefs = [Ref | State#ship_state.auth_refs], - command_ship_loop(State#ship_state{auth_refs=AuthRefs}); - + command_ship_loop(State#ship_state{auth_refs = AuthRefs}); {auth_check, Ref, From} -> Result = lists:member(Ref, State#ship_state.auth_refs), From ! {auth_result, Result}, command_ship_loop(State); - _Other -> command_ship_loop(State) end. @@ -264,50 +244,43 @@ command_ship_loop(State) -> %% Main loop for pirate ship processes pirate_ship_loop(State) -> receive - {message, FromShip, {Ref, Content}} -> + {message, _FromShip, {Ref, Content}} -> NewRefs = [Ref | State#ship_state.received_refs], NewMessages = [{Ref, Content} | State#ship_state.messages], - pirate_ship_loop(State#ship_state{ - received_refs=NewRefs, - messages=NewMessages - }); - + pirate_ship_loop(State#ship_state{received_refs = NewRefs, messages = NewMessages}); {check_received, Ref, From} -> Result = lists:member(Ref, State#ship_state.received_refs), From ! {received_check, Result}, pirate_ship_loop(State); - {get_message, Ref, From} -> - Message = case lists:keyfind(Ref, 1, State#ship_state.messages) of - {Ref, Content} -> Content; - false -> undefined - end, + Message = + case lists:keyfind(Ref, 1, State#ship_state.messages) of + {Ref, Content} -> + Content; + false -> + undefined + end, From ! {message_response, Message}, pirate_ship_loop(State); - {link_to, Ship} -> link(Ship), pirate_ship_loop(State); - {register_auth_ref, Ref} -> AuthRefs = [Ref | State#ship_state.auth_refs], - pirate_ship_loop(State#ship_state{auth_refs=AuthRefs}); - - {auth_message, FromShip, AuthRef, Message} -> + pirate_ship_loop(State#ship_state{auth_refs = AuthRefs}); + {auth_message, _FromShip, AuthRef, Message} -> % Store the auth message if we have the ref registered case lists:member(AuthRef, State#ship_state.auth_refs) of true -> NewMessages = [{AuthRef, Message} | State#ship_state.messages], - pirate_ship_loop(State#ship_state{messages=NewMessages}); + pirate_ship_loop(State#ship_state{messages = NewMessages}); false -> pirate_ship_loop(State) end; - {auth_check, Ref, From} -> Result = lists:member(Ref, State#ship_state.auth_refs), From ! {auth_result, Result}, pirate_ship_loop(State); - _Other -> pirate_ship_loop(State) end. From 5ea1b258f1f1e2d69f242c4b0773347debec021a Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 16:58:41 +0200 Subject: [PATCH 07/11] update the test suite --- exercises/practice/space-pirates/.meta/example.erl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/exercises/practice/space-pirates/.meta/example.erl b/exercises/practice/space-pirates/.meta/example.erl index c7242d4c..f63099c1 100644 --- a/exercises/practice/space-pirates/.meta/example.erl +++ b/exercises/practice/space-pirates/.meta/example.erl @@ -70,8 +70,13 @@ get_last_message(Ship, Ref) -> %% Enable the command ship to trap exit signals from linked ships enable_distress_beacon(CommandShip) -> - CommandShip ! {enable_distress_beacon}, - ok. + CommandShip ! {enable_distress_beacon, self()}, + receive + {distress_beacon_enabled, true} -> + ok + after 1000 -> + {error, timeout} + end. %% Link two ship processes link_ships(ShipA, ShipB) -> @@ -163,8 +168,9 @@ authenticate_message(Ship, AuthRef) -> %% Main loop for the command ship process command_ship_loop(State) -> receive - {enable_distress_beacon} -> + {enable_distress_beacon, From} -> process_flag(trap_exit, true), + From ! {distress_beacon_enabled, true}, command_ship_loop(State#ship_state{trap_exit = true}); {register_ship, Name, Pid} -> NewRegistry = [{Name, Pid} | State#ship_state.ships_registry], From 58d26d67eb9667613d7ebc7701feaa4c769c178a Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 17:09:44 +0200 Subject: [PATCH 08/11] make the test easier --- .../space-pirates/test/space_pirates_tests.erl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/exercises/practice/space-pirates/test/space_pirates_tests.erl b/exercises/practice/space-pirates/test/space_pirates_tests.erl index 7c704ad3..0fc456dd 100644 --- a/exercises/practice/space-pirates/test/space_pirates_tests.erl +++ b/exercises/practice/space-pirates/test/space_pirates_tests.erl @@ -42,14 +42,10 @@ message_sending_test_() -> enable_distress_beacon_test_() -> {"Command ship can enable distress beacon to trap exit signals", - fun() -> + fun() -> CommandShip = space_pirates:start_command_ship(), - ?assertEqual(ok, space_pirates:enable_distress_beacon(CommandShip)), - {status, _, _, StatusInfo} = sys:get_status(CommandShip), - [_, _, _, _, Lists] = StatusInfo, - {data, [{"State", StateData}]} = Lists, - ?assertEqual(true, proplists:get_value(trap_exit, StateData)) - end}. + ?assertEqual(ok, space_pirates:enable_distress_beacon(CommandShip)) + end}. ship_destruction_test_() -> {"When a ship is destroyed, the command ship receives an exit signal", @@ -61,7 +57,7 @@ ship_destruction_test_() -> ?assertEqual(ok, space_pirates:destroy_ship(PirateShip, "Hit by asteroid")), timer:sleep(100), % Give time for exit signal to be processed ?assertEqual( - {ship_lost, "Doomed Vessel", "Hit by asteroid"}, + {ship_lost, "Doomed Vessel", "Hit by asteroid"}, space_pirates:get_last_distress_signal(CommandShip) ) end}. From 9bd94a2cd2bbdca8d944ace2a45391cbf708a4b5 Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 17:10:59 +0200 Subject: [PATCH 09/11] relaunch CI From d1a3160ab8d3240e12ba6d5a43fdc01fa3732df0 Mon Sep 17 00:00:00 2001 From: Maartz Date: Mon, 31 Mar 2025 17:40:11 +0200 Subject: [PATCH 10/11] properly link ship's processes --- exercises/practice/space-pirates/.meta/example.erl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/exercises/practice/space-pirates/.meta/example.erl b/exercises/practice/space-pirates/.meta/example.erl index f63099c1..6a0cc1b6 100644 --- a/exercises/practice/space-pirates/.meta/example.erl +++ b/exercises/practice/space-pirates/.meta/example.erl @@ -80,12 +80,9 @@ enable_distress_beacon(CommandShip) -> %% Link two ship processes link_ships(ShipA, ShipB) -> - link_process(ShipA, ShipB), - ok. - -%% Helper function to link processes -link_process(Ship, ToLink) -> - Ship ! {link_to, ToLink}, + ShipA ! {link_to, ShipB}, + ShipB ! {link_to, ShipA}, + timer:sleep(10), % Small delay to ensure linking ok. %% Destroy a ship with a reason From 839301d5e2aee7abb1a0252ffa036abc0615910f Mon Sep 17 00:00:00 2001 From: Maartz Date: Fri, 25 Apr 2025 10:43:10 +0200 Subject: [PATCH 11/11] update hints and md line length based on review and exercism policies --- .../{hints/hint_3.md => .docs/hints.md} | 34 ++++++++++++- .../space-pirates/.docs/instructions.md | 50 +++++++++++-------- .../practice/space-pirates/hints/README.md | 4 -- .../practice/space-pirates/hints/hint_1.md | 9 ---- .../practice/space-pirates/hints/hint_2.md | 15 ------ 5 files changed, 61 insertions(+), 51 deletions(-) rename exercises/practice/space-pirates/{hints/hint_3.md => .docs/hints.md} (59%) delete mode 100644 exercises/practice/space-pirates/hints/README.md delete mode 100644 exercises/practice/space-pirates/hints/hint_1.md delete mode 100644 exercises/practice/space-pirates/hints/hint_2.md diff --git a/exercises/practice/space-pirates/hints/hint_3.md b/exercises/practice/space-pirates/.docs/hints.md similarity index 59% rename from exercises/practice/space-pirates/hints/hint_3.md rename to exercises/practice/space-pirates/.docs/hints.md index e5d19a46..9bc6211b 100644 --- a/exercises/practice/space-pirates/hints/hint_3.md +++ b/exercises/practice/space-pirates/.docs/hints.md @@ -1,4 +1,34 @@ -# hint_3 +## General + +The Basics of Process Communication + +In Erlang, every communication between processes happens through message passing. + +Think of your ships not as objects with methods, but as independent beings that can only talk to each other by sending messages. + +When a ship goes down, it doesn't call a function - it simply stops receiving messages. + +How would other ships know it's gone? You'll need to establish connections between your processes that can detect when one disappears. + +Remember: in space, no one can hear you scream - unless you've set up the right message handling! + +___ + +Monitoring Your Fleet + +To build a robust fleet communication system, you'll need to use these key Erlang concepts: + +`link/1`` - Creates a bidirectional connection between processes +`process_flag(trap_exit, true)`` - Makes a process receive notifications when linked processes die +`spawn_link/1` - Creates a new process that's automatically linked to its parent + +When setting up your command ship, make sure it can "trap exits" so it receives notifications when ships are destroyed. + +Your message pattern matching should look for `{'EXIT', Pid, Reason}` messages that indicate a ship has gone down. + +For secure communications, remember that `make_ref/0` creates a unique reference that can't be guessed - perfect for authenticating messages between ships! + +___ Implementation Blueprint @@ -54,7 +84,7 @@ command_loop(State) -> For secure message authentication: -Create references with make_ref() +Create references with `make_ref()` Register them with both sender and receiver Verify that incoming messages contain a registered reference Remember that pirates can't be trusted - always verify before accepting a message! diff --git a/exercises/practice/space-pirates/.docs/instructions.md b/exercises/practice/space-pirates/.docs/instructions.md index 5c0d6f7c..66fecf92 100644 --- a/exercises/practice/space-pirates/.docs/instructions.md +++ b/exercises/practice/space-pirates/.docs/instructions.md @@ -1,34 +1,42 @@ # Instructions -Your task is to implement a reliable communication system for Captain Nova's pirate fleet using Erlang's process model. The system needs to handle ship-to-ship communication, distress signals, authentication, and automatic ship replacement when one is destroyed. +Your task is to implement a reliable communication system for Captain Nova's pirate fleet using Erlang's process model. + +The system needs to handle ship-to-ship communication, distress signals, authentication, and automatic ship replacement when one is destroyed. + Core Requirements -Command Ship - Implement a central command ship process that acts as a supervisor for the fleet -Pirate Ships - Create ship processes that can send and receive authenticated messages -Distress Beacon - Enable command ships to trap exit signals when pirate ships are destroyed -Message Authentication - Use Erlang references to create secure communications between ships -Ship Registry - Maintain a registry of active ships in the fleet -Auto-Replacement - When a ship is destroyed, automatically spawn a replacement +- Command Ship - Implement a central command ship process that acts as a supervisor for the fleet +- Pirate Ships - Create ship processes that can send and receive authenticated messages +- Distress Beacon - Enable command ships to trap exit signals when pirate ships are destroyed +- Message Authentication - Use Erlang references to create secure communications between ships +- Ship Registry - Maintain a registry of active ships in the fleet +- Auto-Replacement - When a ship is destroyed, automatically spawn a replacement Concepts You'll Be Using -Process Creation: Using spawn/1 and spawn_link/1 to create processes -Message Passing: Sending messages between processes using the ! operator -Process Linking: Using link/1 to connect processes -Exit Signal Handling: Using process_flag(trap_exit, true) to catch process termination -References: Using make_ref/0 to create unique identifiers for messages -Process Registration: Maintaining a registry of processes +- Process Creation: Using spawn/1 and spawn_link/1 to create processes +- Message Passing: Sending messages between processes using the `!` operator +- Process Linking: Using link/1 to connect processes +- Exit Signal Handling: Using process_flag(trap_exit, true) to catch process termination +- References: Using make_ref/0 to create unique identifiers for messages +- Process Registration: Maintaining a registry of processes ## Your Task -Implement the functions in the space_pirates.erl module to create a robust, fault-tolerant messaging system that can: +Implement the functions in the `space_pirates.erl` module to create a robust, fault-tolerant messaging system that can: + +- Start a command ship that can monitor other ships +- Spawn pirate ships that can communicate with each other +- Enable the distress beacon to trap exit signals +- Create unique references for mission authentication +- Send authenticated messages between ships +- Handle ship destruction and automatic replacement + +Remember, in deep space, there's no room for failure. + +Ships get destroyed, communications get intercepted, and the Empire is always watching. -Start a command ship that can monitor other ships -Spawn pirate ships that can communicate with each other -Enable the distress beacon to trap exit signals -Create unique references for mission authentication -Send authenticated messages between ships -Handle ship destruction and automatic replacement +Your system needs to be reliable, secure, and resilient. -Remember, in deep space, there's no room for failure. Ships get destroyed, communications get intercepted, and the Empire is always watching. Your system needs to be reliable, secure, and resilient. Good luck, Communication Officer. The fleet's success depends on you. diff --git a/exercises/practice/space-pirates/hints/README.md b/exercises/practice/space-pirates/hints/README.md deleted file mode 100644 index 045ab306..00000000 --- a/exercises/practice/space-pirates/hints/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Readme - -This directory contains some hints to help you complete this exercise. The -higher the number of a hint file, the more explicit the hint. diff --git a/exercises/practice/space-pirates/hints/hint_1.md b/exercises/practice/space-pirates/hints/hint_1.md deleted file mode 100644 index c5b926ca..00000000 --- a/exercises/practice/space-pirates/hints/hint_1.md +++ /dev/null @@ -1,9 +0,0 @@ -# hint_1 - -The Basics of Process Communication - -In Erlang, every communication between processes happens through message passing. Think of your ships not as objects with methods, but as independent beings that can only talk to each other by sending messages. - -When a ship goes down, it doesn't call a function - it simply stops receiving messages. How would other ships know it's gone? You'll need to establish connections between your processes that can detect when one disappears. - -Remember: in space, no one can hear you scream - unless you've set up the right message handling! diff --git a/exercises/practice/space-pirates/hints/hint_2.md b/exercises/practice/space-pirates/hints/hint_2.md deleted file mode 100644 index 045be52b..00000000 --- a/exercises/practice/space-pirates/hints/hint_2.md +++ /dev/null @@ -1,15 +0,0 @@ -# hint_2 - -Monitoring Your Fleet - -To build a robust fleet communication system, you'll need to use these key Erlang concepts: - -link/1 - Creates a bidirectional connection between processes -process_flag(trap_exit, true) - Makes a process receive notifications when linked processes die -spawn_link/1 - Creates a new process that's automatically linked to its parent - -When setting up your command ship, make sure it can "trap exits" so it receives notifications when ships are destroyed. - -Your message pattern matching should look for {'EXIT', Pid, Reason} messages that indicate a ship has gone down. - -For secure communications, remember that make_ref/0 creates a unique reference that can't be guessed - perfect for authenticating messages between ships!