Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Assignment #3

CSCI 201 Spring 2023

Title

SalTickets v2.0

Topics Covered

Networking

Multi-Threading

Concurrency Issues

API Querying

Introduction

After a stressful and sweaty pitch, the Council of SAL is satisfied with your trade-scheduling prototype demo application and has unanimously decided to give you the green light to fully implement your application. This involves pulling real data from Ticketmaster, issuing tickets sales and purchases from SalTickets trading platform to agents in real time over the network, and chaining trades for ticket agents. A successful implementation will gain you 5 points and possibly a good word from the director.

TICKETMASTER API

In addition to reading in data from two hard-coded CSV files, you will use a “cloud”   API to retrieve ticket information for events from Ticketmaster. For this assignment you will not call the TicketMaster API directly. Instead you will contact a site in the cloud that has already cached some of this information. In other words, the ticket price and other event information should be pulled from this cloud API, and you will   only have to read from the CSV files for the schedule of ticket trades and initial agent balances, and sequence of agents. The data will be returned in JSON format.

Assignment

In this assignment, you will create two different programs - a server and a client. You will implement a networked delivery system where clients (which represent ticket agents) receive ticket trades from the server (which represents the trading platform), and the agents will complete the ticket trades of the appropriate event (artist/venue).

There will be some concurrency issues since agents will have to wait on other agents before starting the trade tickets. There will be many ways to design the program, so it would be wise to spend some time designing the program before you begin coding.

Similar to the previous Assignment #2, here is a sample schedule.csv file:

0,vvG1IZ9KBiqNAT,4

0,G5viZ9_e2_wK_,1

0,vvG1iZ94EbSeQa,3

0,vvG1IZ9pNeVNoR,-2

0,G5viZ9_e2_wK_,1

0,G5eVZ9bUUS4P-,5

2,vvG1iZ94EbSeQa,-2

3,G5viZ9_e2_wK_,1

5,vvG1IZ9pNeVNoR,1

The fields have changed a little bit.

●   The first field indicates when the ticket trade is to be initiated, measured in seconds from the start of trading tickets.

●   The second field indicates the Event Id associated with a specific event (artist name/tour/date) whose tickets are being bought or sold.

●   The third field indicates how many tickets are being bought or sold, where a positive number indicates a ticket buy, and a negative number indicates a ticket sale.

Notice that this file no longer includes the ticket price for the corresponding tickets. Instead of the price, the 2nd field contains an Event Id. You will use this Event Id to obtain the ticket price from Ticketmaster, and other information about the event (the artist name, tour name, date of the event and venue).

As we mentioned above, instead of contacting Ticketmaster directly, SalTickets has created a Web Service on Google Cloud to retrieve the event information, including ticket price, based on the Event Id.

The URL to use to obtain the event informationis this one:

https://us-west2-csci201-376723.cloudfunctions.net/events/{id}

as in:

https://us-west2-csci201-376723.cloudfunctions.net/events/k7vGFKzleBdwS

The Web Service will respond with a single JSON entry, if the corresponding event  exists. The format of the response will be in JSON format, similar to the format used in Assignment 2. The format of the JSON response for a successful request (“200    OK”)  is as follows:

{

"name" : "Ed Sheeran",

"tour" : "+-=÷x Tour",

"localDate" : "2023-09-23",

"venue" : "SoFi Stadium",

"price" : 298

}

Notice that the web service will return a single entry, and the last key is now the price of a ticket. Since the format of the response is very similar to the format of the response in Assignment 2 (except for the change of the last key), you should be able to reuse the same classes set up with GSON, with minimal changes.

If there is no event for the given EventId, the service will respond with a “404 Not Found,”  and the following JSON object:

{

"fault" :{

"faultstring" :"no such event found"

}

}

If the EventId is missing, the service will respond with a “401 Unauthorized”, and the following JSON object:

{

"fault" :{

"faultstring" :"eventid required"

}

}

In either of the above error cases, the faultstring should be extracted, displayed in the output, and the server should disregard the entry in the schedule.csv file, and continue parsing any additional lines in the schedule.csv file.

Aside from the schedule.csv file, you will need to read in a second file named agents.csv, also in CSV format. Below is a layout of agents.csv, which contains information about the ticket agents:

1, 2500

2, 5000

On each line of the CSV file, you will have the following fields:

The first field indicates the serial number of the agent.

●   The second field indicates the initial starting balance of the agent, in US dollars.

Hints

The Java Classes to be used to connect to the Web Service API are

URLConnection, HttpURLConnection and URL, documented here:

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/net/URLConnectio

n.html

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/net/HttpURLConn

ection.html https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/net/URL.html

You should learn how to use the connect() method of URLConnection, and the

SetRequestMethod(), SetRequestProperty(), and getResponseCode() methods of

HttpURLConnection.

Server Functionality

When your server first runs, prompt the user for the name of the schedule file. Then  prompt the user for the name of the agent's file. The number of agents dispatched     will be equal to the number of records in the agents.csv file. For example, if there are 2 records corresponding to 2 agents in the agents.csv file that means the server will  not send out any orders until 2 clients (i.e., 2 agents) have connected to the server.   Afterwards, the server should begin listening on port 3456 for client connections.       Every time a client connects to the server, verify that the connection was made by     printing an output from the server, similar to the sample output below.

Client Functionality

The client will begin by welcoming the user to the program and will prompt the user for the server hostname and port. If a valid connection is made, the program should let the user know how many more agents are needed before the ticket trades are    started. For example, in the case when there are 2 records in the agents.csv file, if  there is currently only 1 connection, the client should print a message saying that 1 more agent is needed before the ticket trades can be started.

Once all agents have connected, the ticket trades are initiated, and the client will be  responsible for completing the ticket trade of the appropriate event. The client should also be handling the returned data from the API calls to determine the ticket prices of each event.

Program Execution

Unlike the previous homework assignment, agents can take up and be responsible for more than one trade at a time. Additionally, agents can take up and be responsible for ticket trades of multiple events at a time.

An agent should take up as many ticket trades as possible at the moment of initiation, provided that the sum obtained on totalling the money required for executing the ticket purchase is less than or equal to the current balance of the agent. This means that if there are 5 ticket trades that all have the same timestamp   and all are ticket purchases of value $100 each, an agent having a current remaining balance of $400 would be able to pick up and be responsible for only 4 of those 5 ticket purchases. As long as there are available agents, ticket trades should be assigned promptly. If there are no available agents, the ticket trade request will remain in the queue until an agent completes their trade. Once an agent completes,  the agent should pick up as many of the queued trade requests (with respect to the   current time) as are permissible under that agent’s balance limit.

Points to keep in mind:

1.  The execution of a ticket purchase should result in the reduction of the current balance of the agent who executed that trade, by an amount equal to the cost of that ticket purchase.

2. The execution of ticket sales will not result in any changes to the current balance of the trader. You could consider the initial balance of each agent as a limit that they should not cross for total ticket purchases.

3.  If at a point in time there are multiple free agents, the assignment of ticket trades should be done in the ascending order of the serial numbers of the agents.

4.  The program should sleep for 1 second for each ticket trade that is being executed.

5.  The ticket trades should be assigned in the order they are present in the schedule.csv file.

6.  Consider the situation where the following thicket trade is not executed because none of the traders have enough balance:

a.   12, Shania Twain , 10, k7vGFKzleBdwS

At the end you need to print them as follows:

Incomplete ticket purchase: (12, Shania Twain, 10, 2023/01/30 16:39:08)

[StartTime, Artist, Quantity, Date & Time of Ticket

Purchase]

If there are no incomplete trades, then print:

Incomplete ticket purchases: NONE

7.  At the end also print the total profit earned by each client(trader) which is the sum of all the sales that the trader did.

Please follow the sample execution below for the proper output format.

NOTE: It is not a requirement for the timestamps to start from all 0s, but you could do it as a learning exercise. But no points will be deducted for this. The time stamp format should be correct.

Sample Executions

schedule1.csv, agents1.csv

Server Output

What is the path of the schedule le?

schedule1.csv

The schedule le has been properly read.

What is the path of the agents le?

agents1.csv

The agents le has been properly read.

Listening on port 3456.

Waiting for agents...

Connection from: /127.0.0.1

Waiting for 1 more agent(s)...

Connection from: /127.0.0.1

Starting service.

Processing complete.

Client 1 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more agent is needed before the service can begin.

Waiting...

All agents have arrived!

Starting service.

[00:00:00.056] Assigned purchase of 4 ticket(s) of Ed Sheeran. Total cost estimate = 150.00 * 4 = 600.00. [00:00:00.057] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 * 1 = 449.00. [00:00:00.058] Assigned purchase of 3 ticket(s) of Shania Twain. Total cost estimate = 300.00 * 3 = 900.00. [00:00:00.058] Assigned sale of 2 ticket(s) of P!NK. Total gain estimate = 405.00 * 2 = 810.00. [00:00:00.059] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 * 1 = 449.00. [00:00:00.062] Starting purchase of 4 ticket(s) of Ed Sheeran. Total cost = 150.00 * 4 = 600.00. [00:00:01.068] Finished purchase of 4 ticket(s) of Ed Sheeran.

[00:00:01.069] Starting purchase of 1 ticket(s) of Eagles. Total cost = 449.00 * 1 = 449.00.

[00:00:02.070] Finished purchase of 1 ticket(s) of Eagles.

[00:00:02.071] Starting purchase of 3 ticket(s) of Shania Twain. Total cost = 300.00 * 3 = 900.00.

[00:00:03.077] Finished purchase of 3 ticket(s) of Shania Twain.

[00:00:03.079] Starting sale of 2 ticket(s) of P!NK. Total gain = 405.00 * 2 = 810.00.

[00:00:04.085] Finished sale of 2 ticket(s) of P!NK.

[00:00:04.085] Starting purchase of 1 ticket(s) of Eagles. Total cost = 449.00 * 1 = 449.00.

[00:00:05.091] Finished purchase of 1 ticket(s) of Eagles.

[00:00:05.096] Incomplete Trades: NONE

Total Profit Earned: $810.00.

Processing complete.

Client 2 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All agents have arrived!

Starting service.

[00:00:00.052] Assigned purchase of 5 ticket(s) of Eagles. Total cost estimate = 529.00 * 5 = 2645.00. [00:00:00.053] Starting purchase of 5 ticket(s) of Eagles. Total cost = 529.00 * 5 = 2645.00. [00:00:01.060] Finished purchase of 5 ticket(s) of Eagles.

[00:00:01.970] Assigned sale of 2 ticket(s) of Shania Twain. Total gain estimate = 300.00 * 2 = 600.00. [00:00:01.971] Starting sale of 2 ticket(s) of Shania Twain. Total gain = 300.00 * 2 = 600.00. [00:00:02.977] Finished sale of 2 ticket(s) of Shania Twain.

[00:00:02.991] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 * 1 = 449.00. [00:00:02.992] Starting purchase of 1 ticket(s) of Eagles. Total cost = 449.00 * 1 = 449.00. [00:00:03.997] Finished purchase of 1 ticket(s) of Eagles.

[00:00:04.968] Assigned purchase of 1 ticket(s) of P!NK. Total cost estimate = 405.00 * 1 = 405.00. [00:00:04.969] Starting purchase of 1 ticket(s) of P!NK. Total cost = 405.00 * 1 = 405.00. [00:00:05.979] Finished purchase of 1 ticket(s) of P!NK.

[00:00:05.983] Incomplete Trades: NONE

Total Profit Earned: $600.00.

Processing complete.

schedule2.csv, agents2.csv

Server Output

What is the path of the schedule le?

schedule2.csv

The schedule le has been properly read.

What is the path of the agents le?

agents2.csv

The agents le has been properly read.

Listening on port 3456.

Waiting for agents...

Connection from: /127.0.0.1

Waiting for 1 more agent(s)...

Connection from: /127.0.0.1

Starting service.

Processing complete.

Client 1 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more agent is needed before the service can begin.

Waiting...

All agents have arrived!

Starting service.

[00:00:00.057] Assigned purchase of 2 ticket(s) of Eagles. Total cost estimate = 449.00 * 2 = 898.00. [00:00:00.059] Assigned sale of 1 ticket(s) of Eagles. Total gain estimate = 449.00 * 1 = 449.00. [00:00:00.060] Assigned purchase of 5 ticket(s) of P!NK. Total cost estimate = 405.00 * 5 = 2025.00. [00:00:00.061] Starting purchase of 2 ticket(s) of Eagles. Total cost = 449.00 * 2 = 898.00. [00:00:01.066] Finished purchase of 2 ticket(s) of Eagles.

[00:00:01.067] Starting sale of 1 ticket(s) of Eagles. Total gain = 449.00 * 1 = 449.00.

[00:00:02.073] Finished sale of 1 ticket(s) of Eagles.

[00:00:02.073] Starting purchase of 5 ticket(s) of P!NK. Total cost = 405.00 * 5 = 2025.00.

[00:00:03.079] Finished purchase of 5 ticket(s) of P!NK.

[00:00:08.994] Assigned sale of 1 ticket(s) of Shania Twain. Total gain estimate = 300.00 * 1 = 300.00. [00:00:08.994] Assigned purchase of 1 ticket(s) of Shania Twain. Total cost estimate = 300.00 * 1 = 300.00. [00:00:08.995] Starting sale of 1 ticket(s) of Shania Twain. Total gain = 300.00 * 1 = 300.00. [00:00:10.001] Finished sale of 1 ticket(s) of Shania Twain.

[00:00:10.002] Starting purchase of 1 ticket(s) of Shania Twain. Total cost = 300.00 * 1 = 300.00.

[00:00:11.007] Finished purchase of 1 ticket(s) of Shania Twain.

[00:00:12.029] Incomplete Trades: (12, vvG1IZ99j1TVfD [Metallica], 10, 2023/02/11 14:19:13) (12, vvG1IZ99j1TVfD [Metallica], 5, 2023/02/11 14:19:13)

Total Profit Earned: $749.00.

Processing complete.

Client 2 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All agents have arrived!

Starting service.

[00:00:03.016] Assigned sale of 1 ticket(s) of Shania Twain. Total gain estimate = 300.00 * 1 = 300.00. [00:00:03.018] Assigned sale of 2 ticket(s) of Shania Twain. Total gain estimate = 300.00 * 2 = 600.00. [00:00:03.018] Starting sale of 1 ticket(s) of Shania Twain. Total gain = 300.00 * 1 = 300.00. [00:00:04.025] Finished sale of 1 ticket(s) of Shania Twain.

[00:00:04.026] Starting sale of 2 ticket(s) of Shania Twain. Total gain = 300.00 * 2 = 600.00.

[00:00:05.032] Finished sale of 2 ticket(s) of Shania Twain.

[00:00:08.970] Assigned purchase of 2 ticket(s) of Metallica. Total cost estimate = 538.00 * 2 = 1076.00. [00:00:08.971] Starting purchase of 2 ticket(s) of Metallica. Total cost = 538.00 * 2 = 1076.00. [00:00:09.977] Finished purchase of 2 ticket(s) of Metallica.

[00:00:12.007] Incomplete Trades: (12, vvG1IZ99j1TVfD [Metallica], 10, 2023/02/11 14:19:13) (12, vvG1IZ99j1TVfD [Metallica], 5, 2023/02/11 14:19:13)

Total Profit Earned: $900.00.

Processing complete.

schedule3.csv, agents3.csv

Server Output

What is the path of the schedule le?

schedule3.csv

The schedule le has been properly read.

What is the path of the agents le?

agents3.csv

The agents le has been properly read.

Listening on port 3456.

Waiting for agents...

Connection from: /127.0.0.1

Waiting for 1 more agent(s)...

Connection from: /127.0.0.1

Starting service.

Invalid eventId (vvG1IZ9pNeVN). Discard this transaction and continue...

Processing complete.

Client 1 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more agent is needed before the service can begin.

Waiting...

All agents have arrived!

Starting service.

[00:00:00.009] Assigned purchase of 1 ticket(s) of Ed Sheeran. Total cost estimate = 150.00 * 1 = 150.00. [00:00:00.010] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 * 1 = 449.00. [00:00:00.011] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 * 1 = 449.00. [00:00:00.013] Starting purchase of 1 ticket(s) of Ed Sheeran. Total cost = 150.00 * 1 = 150.00. [00:00:01.020] Finished purchase of 1 ticket(s) of Ed Sheeran.

[00:00:01.020] Starting purchase of 1 ticket(s) of Eagles. Total cost = 449.00 * 1 = 449.00.

[00:00:02.026] Finished purchase of 1 ticket(s) of Eagles.

[00:00:02.026] Starting purchase of 1 ticket(s) of Eagles. Total cost = 449.00 * 1 = 449.00.

[00:00:03.032] Finished purchase of 1 ticket(s) of Eagles.

[00:00:04.995] Assigned purchase of 1 ticket(s) of P!NK. Total cost estimate = 405.00 * 1 = 405.00. [00:00:04.996] Starting purchase of 1 ticket(s) of P!NK. Total cost = 405.00 * 1 = 405.00. [00:00:06.007] Finished purchase of 1 ticket(s) of P!NK.

[00:00:06.010] Incomplete Trades: (0, vvG1IZ9pNeVN [N/A], -2, 2023/02/13 15:16:14)

Total Profit Earned: $0.00.

Processing complete.

Client 2 Output

Welcome to SalTickets v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All agents have arrived!

Starting service.

[00:00:00.008] Assigned purchase of 3 ticket(s) of Eagles. Total cost estimate = 529.00 * 3 = 1587.00. [00:00:00.009] Starting purchase of 3 ticket(s) of Eagles. Total cost = 529.00 * 3 = 1587.00. [00:00:01.015] Finished purchase of 3 ticket(s) of Eagles.

[00:00:01.972] Assigned sale of 2 ticket(s) of Shania Twain. Total gain estimate = 300.00 * 2 = 600.00. [00:00:01.974] Starting sale of 2 ticket(s) of Shania Twain. Total gain = 300.00 * 2 = 600.00. [00:00:02.977] Finished sale of 2 ticket(s) of Shania Twain.

[00:00:02.980] Assigned purchase of 1 ticket(s) of Eagles. Total cost estimate = 449.00 <