// Create a new flight, or add seats to existing flight. // Note: if flightPrice <= 0 and the flight already exists, it maintains // its current price. @Override public boolean addFlight(int id, int flightNumber, int numSeats, int flightPrice) { Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") called."); Flight curObj = (Flight) readData(id, Flight.getKey(flightNumber)); synchronized (syncLock) { if (curObj == null) { // Doesn't exist; add it. Flight newObj = new Flight(flightNumber, numSeats, flightPrice); writeData(id, newObj.getKey(), newObj); Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") OK."); } else { // Add seats to existing flight and update the price. curObj.setCount(curObj.getCount() + numSeats); if (flightPrice > 0) { curObj.setPrice(flightPrice); } writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") OK: " + "seats = " + curObj.getCount() + ", price = $" + flightPrice); } return (true); } }
// Returns price of this flight public ReturnTuple<Integer> queryFlightPrice(int id, int flightNum, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting queryFlightPrice to master"); return this.getMaster().queryFlightPrice(id, flightNum, timestamp); } else { timestamp.stamp(); QueryFlightPriceRMICommand qfp = new QueryFlightPriceRMICommand(flightGroup, id, flightNum); qfp.setTimestampObject(timestamp); ReturnTuple<Integer> result = null; try { overseer.validTransaction(id); lm.Lock(id, Flight.getKey(flightNum), qfp.getRequiredLock()); qfp.execute(); result = qfp.price; } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
public ReturnTuple<Boolean> deleteFlight(int id, int flightNum, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting deleteFlight to master"); return this.getMaster().deleteFlight(id, flightNum, timestamp); } else { timestamp.stamp(); DeleteFlightRMICommand df = new DeleteFlightRMICommand(flightGroup, id, flightNum); df.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Flight.getKey(flightNum), df.getRequiredLock()); df.execute(); result = df.success; if (result.result) overseer.addCommandToTransaction(id, df); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Create a new flight, or add seats to existing flight // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice) throws RemoteException, InvalidTransactionException, TransactionAbortedException { Trace.info( "RM::addFlight(" + id + ", " + flightNum + ", $" + flightPrice + ", " + flightSeats + ") called"); Flight curObj = (Flight) readData(id, Flight.getKey(flightNum)); if (curObj == null) { // doesn't exist...add it Flight newObj = new Flight(flightNum, flightSeats, flightPrice); writeData(id, newObj.getKey(), newObj); String key = newObj.getKey(); if (readDataFromLog(id, key, id) == null) { Flight logObj = (Flight) newObj.clone(); logObj.setCount(-1); logObj.type = 0; writeDataToLog(id, key, logObj); } Trace.info( "RM::addFlight(" + id + ") created new flight " + flightNum + ", seats=" + flightSeats + ", price=$" + flightPrice); } else { // add seats to existing flight and update the price... Flight logObj = (Flight) curObj.clone(); if (readDataFromLog(id, curObj.getKey(), id) == null) writeDataToLog(id, curObj.getKey(), logObj); curObj.setCount(curObj.getCount() + flightSeats); if (flightPrice > 0) { curObj.setPrice(flightPrice); } // if writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addFlight(" + id + ") modified existing flight " + flightNum + ", seats=" + curObj.getCount() + ", price=$" + flightPrice); } // else return (true); }
/* reserve an itinerary */ public ReturnTuple<Boolean> itinerary( int id, int customer, Vector flightNumbers, String location, boolean Car, boolean Room, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting itinerary to master"); return this.getMaster() .itinerary(id, customer, flightNumbers, location, Car, Room, timestamp); } else { timestamp.stamp(); ItineraryRMICommand i = new ItineraryRMICommand( carGroup, flightGroup, roomGroup, id, customer, flightNumbers, location, Car, Room); i.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Customer.getKey(customer), i.getRequiredLock()); lm.Lock(id, ResImpl.Car.getKey(location), i.getRequiredLock()); lm.Lock(id, Hotel.getKey(location), i.getRequiredLock()); for (Object flightNo : flightNumbers) { int flightNum = Integer.parseInt((String) flightNo); lm.Lock(id, Flight.getKey(flightNum), i.getRequiredLock()); } i.execute(); result = i.success; if (result.result && !i.error()) overseer.addCommandToTransaction(id, i); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// return a bill public ReturnTuple<String> queryCustomerInfo(int id, int customerID, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting queryCustomerInfo to master"); return this.getMaster().queryCustomerInfo(id, customerID, timestamp); } else { timestamp.stamp(); QueryCustomerInfoRMICommand qci = new QueryCustomerInfoRMICommand(carGroup, flightGroup, roomGroup, id, customerID); qci.setTimestampObject(timestamp); ReturnTuple<String> result = null; try { Vector<Integer> flightNos; Vector<String> locations; overseer.validTransaction(id); lm.Lock(id, Customer.getKey(customerID), qci.getRequiredLock()); qci.execute(); flightNos = qci.getCustomerFlightReservations(); for (int flightNo : flightNos) { lm.Lock(id, Flight.getKey(flightNo), qci.getRequiredLock()); } locations = qci.getCustomerRoomReservations(); for (String location : locations) { lm.Lock(id, Hotel.getKey(location), qci.getRequiredLock()); } locations = qci.getCustomerCarReservations(); for (String location : locations) { lm.Lock(id, Car.getKey(location), qci.getRequiredLock()); } qci.execute(); result = qci.customerInfo; } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
@Override public boolean cancelReserveFlight(int id, int customerId, int flightNumber) { return cancelReserveItem( id, customerId, Flight.getKey(flightNumber), String.valueOf(flightNumber)); }
// Returns price of this flight. public int queryFlightPrice(int id, int flightNumber) { return queryPrice(id, Flight.getKey(flightNumber)); }
// Returns the number of empty seats on this flight. @Override public int queryFlight(int id, int flightNumber) { return queryNum(id, Flight.getKey(flightNumber)); }
@Override public boolean deleteFlight(int id, int flightNumber) { return deleteItem(id, Flight.getKey(flightNumber)); }
// Adds flight reservation to this customer. public boolean reserveFlight(int id, int customerID, int flightNum) throws RemoteException { return reserveItem(id, customerID, Flight.getKey(flightNum), String.valueOf(flightNum)); }
// Returns price of this flight public int queryFlightPrice(int id, int flightNum) throws RemoteException { return queryPrice(id, Flight.getKey(flightNum)); }
public boolean deleteFlight(int id, int flightNum) throws RemoteException { return deleteItem(id, Flight.getKey(flightNum)); }
/** Test of getKey method, of class Flight. */ @Test public void testGetKey() { System.out.println("getKey"); assertEquals(f2.getKey(), 700); }