代写 concurrency Toggle Sidebar

Toggle Sidebar
Toggle Theme
Assignment 5
v1.0.0

PAGES

API Reference
CS 3342 – Fall 2019 – Assignment 5
Top
Introduction
Setup
Implementing Assignment 5
Modules
UserModule
ShoppingListStore
UserStore
ShoppingListServer
Submission Instructions
Marking
CS 3342 – Fall 2019 – Assignment 5
Due: Tuesday April 09, 2019 at 23:59:59
Introduction
In this assignment, we will implement a shopping list server. A client will connect to the server and perform various tasks related to shopping lists, including:
• Creating a new user with a username and password
• Adding an item to a user’s shopping list (requires authentication)
• Removing an item from a user’s shopping list (requires authentication)
• Displaying a user’s shopping list (requires authentication)
• Clearing all shopping lists and users
• Shutting down the server
The client has been written for you. In this assignment, you’ll implement several modules which comprise the server. Your server will use multiple processes to give you practice with concurrent programming and to ensure it can handle multiple, concurrent connections. As it receives messages from the client, it will spawn processes as necessary to handle the requests, and then send responses back to the client.
A sample run of the program is shown to give you an idea of what you’ll be implementing:
# In one terminal window, start the server

$ ./server
13:55:48.324 [info] Starting server. Press Ctrl+C to exit…

# Assume the rest of the commands are executed in another terminal window

$ ./client user add joe password
User created successfully

$ ./client user add joe password
Error: User already exists

$ ./client user add jane secret
User created successfully

$ ./client users
jane
joe

$ ./client list add joe pass apples
Error: Authentication failed

$ ./client list add joe password apples
Item ‘apples’ added to shopping list

$ ./client list add joe password pears
Item ‘pears’ added to shopping list

$ ./client list add joe password pears
Error: Item ‘pears’ already exists

$ ./client list add jane secret bananas
Item ‘bananas’ added to shopping list

$ ./client list joe password
apples
pears

$ ./client list jane secret
bananas

$ ./client list del joe password pears
Item ‘pears’ deleted from shopping list

$ ./client list del joe password pears
Error: Item ‘pears’ not found

$ ./client clear
All data cleared

$ ./client shutdown
Server shutdown command sent
We will discuss concurrency in class on April 03. If you wish to get started before that (recommended), please see Chapter 15 in Programming Elixir 1.6 (available for free!). Alternatively, you can read the Processes guide on the Elixir web site.
Setup
On the class server, cs3342b.gaul.csd.uwo.ca, clone your repository (if you haven’t already) and change to it:
git clone https://repo.csd.uwo.ca/scm/compsci3342_w2019/USERNAME.git
cd USERNAME
If your repository was already cloned, change to it and pull down any pending commits:
cd USERNAME
git pull
From the root of your repository, run the setup script to create your assignment 5 directory and file structure, and to download the provided code:
curl -L https://bit.ly/2YGRz09 | bash
This will create the following structure:
$ find asn5 -type f
asn5/.gitignore
asn5/server
asn5/.formatter.exs
asn5/lib/cli.exs
asn5/lib/shopping_list_store.ex
asn5/lib/user.ex
asn5/lib/client.exs
asn5/lib/shopping_list_server.ex
asn5/lib/user_store.ex
asn5/client
asn5/config/config.exs
asn5/mix.exs
asn5/test/user_store_test.exs
asn5/test/shopping_list_store_test.exs
asn5/test/test_helper.exs
asn5/test/user_test.exs
asn5/test/shopping_list_server_test.exs
In this assignment, we’re creating our project as a mix project. The layout of the skeleton code is the standard layout of a mix project. Mix is a helpful build tool for Elixir that allows us to easily compile, run, test, and debug our code.
You should now commit and push your initial code:
git add .
git commit -m “Pushing initial assignment 5 code”
git push
You are expected to put the code from this assignment in the provided files in lib. Failure to adhere to the directory structure and/or naming convention given will result in a deduction of 10% of your assignment grade. There will be no exceptions to this rule. This is because using non-standard directory or filenames hinders the automated tests that we use to ensure we get your assignments returned to you as quickly as possible.
Before proceeding, let’s get acquainted with Mix and how to debug and test your code.
Implementing Assignment 5
A skeleton for your program has been provided in the starter code. This is implemented as a mix project, which is a useful build tool.
Compiling Code
To compile your code, execute the following from the asn5 directory:
mix compile
Testing in IEx
To load all of your modules in IEx, execute the following:
iex -S mix
This will automatically compile and load all of your modules, which you can then interact with in the IEx REPL. For example, the following shows an example of spawning a process to run UserStore.start(), sending a message to the process, and then displaying the message sent back. The flush() function in IEx is quite useful for seeing what messages you receive.
$ iex -S mix
Interactive Elixir (1.8.1) – press Ctrl+C to exit (type h() ENTER for help)

iex(1)> pid = spawn(UserStore, :start, []) # Spawn UserStore.start() in a new process
#PID<0.137.0>

iex(2)> send(pid, {self(), :clear}) # Send a :clear message to it
{#PID<0.133.0>, :clear}

iex(3)> flush() # Flush any response messages received
{#PID<0.137.0>, :cleared}
It is strongly recommended that you build and test your code in IEx and then add concurrency with processes afterward. For instance, in the UserStore module, focus on getting the code to read and write the user list working before you attempt to introduce processes into the mix. Test your functions in IEx as you implement them.
Running the Unit Tests
To run the provided unit tests, execute the following:
# Run the tests for the User module
mix test test/user_test.exs

# Run the tests for the UserStore module
mix test test/user_store_test.exs

# Run the tests for the ShoppingListStore module
mix test test/shopping_list_store_test.exs

# Run the tests for the ShoppingListServer module
mix test test/shopping_list_server_test.exs

# Run all tests
mix test
As you develop each module, run its tests as shown above to validate your implementation. Note that some tests may hang if you haven’t implemented certain responses, so don’t run the tests too early. Get your code working and responding to messages before you attempt to run the tests.
Running the Executables
Once you have the tests passing, you can run the server using the provided server script in one terminal window:
$ ./server
13:55:48.324 [info] Starting server. Press Ctrl+C to exit…
You can then run the client in another terminal window. Run it without arguments to see its usage:
$ ./client
Usage: ./client [COMMAND] [PARAMS]

Commands

clear – Clear all data on the server
list add USER PASS ITEM – Add ITEM to USER’s list
list del USER PASS ITEM – Remove ITEM from USER’s list
list USER PASS – Show list for USER
shutdown – Shut down the server
user add USER PASS – Add new user
users – List users
Modules
You will implement the following reasonably short modules, comprising the server:
• User
• ShoppingListStore
• UserStore
• ShoppingListServer
Built using ExDoc (v0.19.3), designed by Friedel Ziegelmayer.