Create a multi-threaded server that handles a client\'s transaction until the tr
ID: 674129 • Letter: C
Question
Create a multi-threaded server that handles a client's transaction until the transaction is completed. The ‘transaction’ is a series of requests from the client until the client sends a ‘terminate’ request to the server that signals the transaction is complete. The idea is to simulate a complicated server such as a DBMS. The client makes a series of requests which constitute a transaction. The server is a simple version of a graphics or game server. The server creates a 25 by 25 character grid (initialized to spaces, with grid cell (0, 0) in the upper left and grid cell (24, 24) in the lower right) and the client sends ‘draw’ requests to the server. For the sake of simplicity, the starting position of a client upon creation is the center of the 25 by 25 grid (use an 'X' to mark the entry). The client then proceeds to simulate a random walk by sending an integer between 1 and 8 signifying a direction to move: • 0 –> up {from (i, j) to (i - 1, j)}, • 1 -> diagonally up to the right {from (i, j) to (i - 1, j + 1)}, • 2-> right (from (i, j) to (i , j + 1)}, • 3 -> diagonally down to the right (from (i, j) to (i + 1, j + 1)}, • 4 -> down (from (i , j) to (i + 1, j)}, • 5 -> diagonally down to the left (from (i , j) to (i + 1, j - 1)}, • 6 -> left (from (i , j) to (i - 1, j)}, and • 7 -> diagonally up to the left (from (i, j) to (i - 1, j - 1)}. For each move, the server should mark the square moved to with an 'X'. At the end, the grid will contain the history of all moves made by the client. For each client that connects, the server should create a new thread to handle the client's transaction. The client should first send the name of a file to the server into which the server thread will write the results of the client's operations before quitting. The server thread should open this file for writing. Then, the server thread will receive series of moves from the client, using the integer values above. If the move is impossible (a move off the grid for example, or an invalid move) the server should inform the client and the client will send a retry move. The requests should be chosen at random (you can use the Java Random class). The trace of the walk should be a random line. When the server receives a negative move, it concludes the transaction, by closing the connection, write the grid to the file, closing the file, and returning from the thread method. Each client will have it's own separate file and it's own separate grid for its transaction. In order to handle invalid moves, the server and client must have a protocol: the client sends the server its move request and awaits a status response. The server returns a success status and awaits the next request, or a failed status and awaits the client retry. When the status response arrives, the client submits a retry on a failed status and awaits the status response or continues to the next move for a success status response. The server and client could go through a number of failed-retry responses. Failure-retries do not count against the number of moves the client is sending; only successful moves count. The server must keep the current state (the grid) of the transaction and when the terminate request is sent by the client, the server process should write the grid out to a file. Your client program should connect to the server, send the name of the file to use, and then then pick a random number of moves to send, between 5 and 30 moves. The client will then enter a loop, sending random moves (between 0 and 7, inclusive) to the server. If the server sends the failed status, rather then retrying with a random move, the client should add one modulo 8 to its previous command to retry. The client should continue adding one modulo 8 on a fail status until the server responds with success. When the client is finish with its moves, it should send a negative move to the server and close its connection to the server. Your server should be able to handle multiple clients simultaneously
Explanation / Answer
public void run(){
...
Connection connection = DriverManager.getConnection("....", info);
PreparedStatement ps = connection.prepareStatement("SELECT SUMMARY FROM MODEL.WORK_ITEM");
ResultSet resultSet = ps.executeQuery();
int i = 1;
while(resultSet.next()) {
String summary = resultSet.getString(1);
System.out.println(summary);
}
resultSet.close();
ps.close();
}
public class TravelAgentBean implements javax.ejb.SessionBean {
public Customer customer;
public Cruise cruise;
public Cabin cabin;
public javax.ejb.SessionContext ejbContext;
...
public void setCruiseID(int cruiseID)
throws javax.ejb.FinderException{
// EJB 1.0: also throws RemoteException
try {
ejbContext.getUserTransaction().begin();
CruiseHome home = (CruiseHome)getHome("CruiseHome", CruiseHome.class);
cruise = home.findByPrimaryKey(new CruisePK(cruiseID));
} catch(Exception re) {
// EJB 1.0: throw new RemoteException("",re);
throw new EJBException(re);
}
}
public Ticket bookPassage(CreditCard card, double price)
throws IncompleteConversationalState { // EJB 1.0: also throws RemoteException
try {
if (ejbContext.getUserTransaction().getStatus() !=
javax.transaction.Status.STATUS_ACTIVE) {
// EJB 1.0: throw new RemoteException("Transaction is not active");
throw new EJBException("Transaction is not active");
}
} catch(javax.transaction.SystemException se) {
// EJB 1.0: throw new RemoteException("",se);
throw new EJBException(se);
}
if (customer == null || cruise == null || cabin == null) {
throw new IncompleteConversationalState();
}
try {
ReservationHome resHome = (ReservationHome)
getHome("ReservationHome",ReservationHome.class);
Reservation reservation =
resHome.create(customer, cruise, cabin, price);
ProcessPaymentHome ppHome = (ProcessPaymentHome)
getHome("ProcessPaymentHome",ProcessPaymentHome.class);
ProcessPayment process = ppHome.create();
process.byCredit(customer, card, price);
Ticket ticket = new Ticket(customer,cruise,cabin,price);
ejbContext.getUserTransaction().commit();
return ticket;
} catch(Exception e) {
// EJB 1.0: throw new RemoteException("",e);
throw new EJBException(e);
}
}
...
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.