-
-
Notifications
You must be signed in to change notification settings - Fork 89
Space pirates exercise #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Maartz
wants to merge
11
commits into
exercism:main
Choose a base branch
from
Maartz:space-pirates-exercise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
25ceb53
add space-pirates to the config
Maartz 2eb9d25
add space pirates exercise
Maartz 8ddf726
add stub
Maartz 10f5c8f
add tests
Maartz 6c019c9
remove export_all
Maartz 118b333
format and comment unused variables
Maartz 5ea1b25
update the test suite
Maartz 58d26d6
make the test easier
Maartz 9bd94a2
relaunch CI
Maartz d1a3160
properly link ship's processes
Maartz 839301d
update hints and md line length based on review and exercism policies
Maartz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| ## 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 | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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. | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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." | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Globally, Exercism prefers one sentence per line for Markdown (https://exercism.org/docs/building/markdown/markdown#h-one-sentence-per-line).