public int newCustomer(int id) throws RemoteException { Trace.info("INFO: RM::newCustomer(" + id + ") called"); // Generate a globally unique ID for the new customer int cid = Integer.parseInt( String.valueOf(id) + String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND)) + String.valueOf(Math.round(Math.random() * 100 + 1))); Customer cust = new Customer(cid); writeData(id, cust.getKey(), cust); Customer temp = cust.clone(); temp.setID(-1); temp.setType(1); writeDataToLog(id, cust.getKey(), temp); Trace.info("RM::newCustomer(" + cid + ") returns ID=" + cid); return cid; }
// deletes the entire item protected boolean deleteItem(int id, String key) { Trace.info("RM::deleteItem(" + id + ", " + key + ") called"); ReservableItem curObj = (ReservableItem) readData(id, key); Flight tempItem = new Flight(Integer.parseInt(curObj.getLocation()), curObj.getCount(), curObj.getPrice()); tempItem.setReserved(curObj.getReserved()); // Check if there is such an item in the storage if (curObj == null) { Trace.warn("RM::deleteItem(" + id + ", " + key + ") failed--item doesn't exist"); return false; } else { if (curObj.getReserved() == 0) { tempItem.setType(0); writeDataToLog(id, curObj.getKey(), tempItem); removeData(id, curObj.getKey()); Trace.info("RM::deleteItem(" + id + ", " + key + ") item deleted"); return true; } else { Trace.info( "RM::deleteItem(" + id + ", " + key + ") item can't be deleted because some customers reserved it"); return false; } } // if }
// Delete the entire item. protected boolean deleteItem(int id, String key) { Trace.info("RM::deleteItem(" + id + ", " + key + ") called."); synchronized (syncLock) { ReservableItem curObj = (ReservableItem) readData(id, key); // Check if there is such an item in the storage. if (curObj == null) { Trace.warn("RM::deleteItem(" + id + ", " + key + ") failed: " + " item doesn't exist."); return false; } else { if (curObj.getReserved() == 0) { removeData(id, curObj.getKey()); Trace.info("RM::deleteItem(" + id + ", " + key + ") OK."); return true; } else { Trace.info( "RM::deleteItem(" + id + ", " + key + ") failed: " + "some customers have reserved it."); return false; } } } }
public void run() { try { ObjectOutputStream outputStream = new ObjectOutputStream(_clientSocket.getOutputStream()); outputStream.flush(); DataInputStream inputStream = new DataInputStream(_clientSocket.getInputStream()); while (true) { String request = inputStream.readUTF(); Trace.info("Received request: " + request); RMResult response = processIfComposite(request); if (response == null) { response = processIfCIDRequired(request); if (response == null) { response = processAtomicRequest(request); } } outputStream.writeObject(response); outputStream.flush(); } } catch (EOFException eof) { Trace.info("A client closed a connection."); } catch (IOException e) { e.printStackTrace(); } }
// Deletes customer from the database. public boolean deleteCustomer(int id, int customerID) throws RemoteException { Trace.info("RM::deleteCustomer(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::deleteCustomer(" + id + ", " + customerID + ") failed--customer doesn't exist"); return false; } else { Customer temp = cust.clone(); // Increase the reserved numbers of all reservable items which the customer reserved. RMHashtable reservationHT = cust.getReservations(); for (Enumeration e = reservationHT.keys(); e.hasMoreElements(); ) { String reservedkey = (String) (e.nextElement()); ReservedItem reserveditem = cust.getReservedItem(reservedkey); Trace.info( "RM::deleteCustomer(" + id + ", " + customerID + ") has reserved " + reserveditem.getKey() + " " + reserveditem.getCount() + " times"); ReservableItem item = (ReservableItem) readData(id, reserveditem.getKey()); Trace.info( "RM::deleteCustomer(" + id + ", " + customerID + ") has reserved " + reserveditem.getKey() + "which is reserved" + item.getReserved() + " times and is still available " + item.getCount() + " times"); Flight tempItem = new Flight(Integer.parseInt(item.getLocation()), item.getCount(), item.getPrice()); tempItem.setReserved(item.getReserved()); tempItem.setType(0); item.setReserved(item.getReserved() - reserveditem.getCount()); item.setCount(item.getCount() + reserveditem.getCount()); if (readDataFromLog(id, item.getKey(), id) == null) writeDataToLog(id, item.getKey(), tempItem); } // remove the customer from the storage temp.setType(1); if (readDataFromLog(id, cust.getKey(), id) == null) writeDataToLog(id, cust.getKey(), temp); removeData(id, cust.getKey()); Trace.info("RM::deleteCustomer(" + id + ", " + customerID + ") succeeded"); return true; } // if }
// 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); }
// Delete customer from the database. @Override public boolean deleteCustomer(int id, int customerId) { Trace.info("RM::deleteCustomer(" + id + ", " + customerId + ") called."); synchronized (syncLock) { Customer cust = (Customer) readData(id, Customer.getKey(customerId)); removeData(id, cust.getKey()); Trace.info("RM::deleteCustomer(" + id + ", " + customerId + ") OK."); return true; } }
// query the price of an item protected int queryPrice(int id, String key) { Trace.info("RM::queryCarsPrice(" + id + ", " + key + ") called"); ReservableItem curObj = (ReservableItem) readData(id, key); int value = 0; if (curObj != null) { value = curObj.getPrice(); } // else Trace.info("RM::queryCarsPrice(" + id + ", " + key + ") returns cost=$" + value); return value; }
// 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); } }
// Query the price of an item. protected int queryPrice(int id, String key) { Trace.info("RM::queryPrice(" + id + ", " + key + ") called."); synchronized (syncLock) { ReservableItem curObj = (ReservableItem) readData(id, key); int value = 0; if (curObj != null) { value = curObj.getPrice(); } Trace.info("RM::queryPrice(" + id + ", " + key + ") OK: $" + value); return value; } }
@Override public int newCustomer(int id) { Trace.info("INFO: RM::newCustomer(" + id + ") called."); // Generate a globally unique Id for the new customer. int customerId = Integer.parseInt( String.valueOf(id) + String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND)) + String.valueOf(Math.round(Math.random() * 100 + 1))); Customer cust = new Customer(customerId); writeData(id, cust.getKey(), cust); Trace.info("RM::newCustomer(" + id + ") OK: " + customerId); return customerId; }
// This method makes testing easier. @Override public boolean newCustomerId(int id, int customerId) { Trace.info("INFO: RM::newCustomer(" + id + ", " + customerId + ") called."); Customer cust = (Customer) readData(id, Customer.getKey(customerId)); if (cust == null) { cust = new Customer(customerId); writeData(id, cust.getKey(), cust); Trace.info("INFO: RM::newCustomer(" + id + ", " + customerId + ") OK."); return true; } else { Trace.info( "INFO: RM::newCustomer(" + id + ", " + customerId + ") failed: customer already exists."); return false; } }
// return a bill public String queryCustomerInfo(int id, int customerID) throws RemoteException { Trace.info("RM::queryCustomerInfo(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::queryCustomerInfo(" + id + ", " + customerID + ") failed--customer doesn't exist"); return ""; // NOTE: don't change this--WC counts on this value indicating a customer does not // exist... } else { String s = cust.printBill(); Trace.info("RM::queryCustomerInfo(" + id + ", " + customerID + "), bill follows..."); System.out.println(s); return s; } // if }
public MbeanGenerator(ResourceManager mgr, MibNode aGroup, Context ctxt) throws IOException { super(mgr, aGroup, ctxt); gentype = ctxt.gentype; // Specify oid of the current bean ... // oid = node.getComputedOid(); // Try to find a symbol to associate to the group // varName = node.getSymbolName(); if (varName == null) varName = getClassName(node.getComputedOid()); symboleName = getNodeSymbolName(node); Trace.info(MessageHandler.getMessage("generate.info.var", varName)); // Open the file which will represent the M-bean. // out = openFile(symboleName + Def.JAVA); // Write generic header ... // writeHeader(); // write our own header ... // writeClassDeclaration(); // write the beginning of the constructor // buildConstructorHeader(); }
// Return data structure containing customer reservation info. // Returns null if the customer doesn't exist. // Returns empty RMHashtable if customer exists but has no reservations. public RMHashtable getCustomerReservations(int id, int customerId) { Trace.info("RM::getCustomerReservations(" + id + ", " + customerId + ") called."); synchronized (syncLock) { Customer cust = (Customer) readData(id, Customer.getKey(customerId)); if (cust == null) { Trace.info( "RM::getCustomerReservations(" + id + ", " + customerId + ") failed: customer doesn't exist."); return null; } else { return cust.getReservations(); } } }
// I opted to pass in customerID instead. This makes testing easier public boolean newCustomer(int id, int customerID) throws RemoteException { Trace.info("INFO: RM::newCustomer(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { cust = new Customer(customerID); writeData(id, cust.getKey(), cust); Customer temp = cust.clone(); temp.setType(1); temp.setID(-1); writeDataToLog(id, cust.getKey(), temp); Trace.info("INFO: RM::newCustomer(" + id + ", " + customerID + ") created a new customer"); return true; } else { Trace.info( "INFO: RM::newCustomer(" + id + ", " + customerID + ") failed--customer already exists"); return false; } // else }
public void start(int backoff, long lastConnect) { if (state.getAndSet(State.STARTING) == State.STARTING) { Trace.debug("In the process of starting watch already. Returning early"); return; } this.backoff = backoff; this.lastConnect = lastConnect; Trace.info("Starting watch on project {0} for kind {1}", project.getName(), kind); IClient client = getClientFor(project); if (client != null) { new RestartWatchJob(client).schedule(); } }
// Return a bill. @Override public String queryCustomerInfo(int id, int customerId) { Trace.info("RM::queryCustomerInfo(" + id + ", " + customerId + ") called."); synchronized (syncLock) { Customer cust = (Customer) readData(id, Customer.getKey(customerId)); if (cust == null) { Trace.warn( "RM::queryCustomerInfo(" + id + ", " + customerId + ") failed: customer doesn't exist."); // Returning an empty bill means that the customer doesn't exist. return ""; } else { String s = cust.printBill(); Trace.info("RM::queryCustomerInfo(" + id + ", " + customerId + "): \n"); System.out.println(s); return s; } } }
// Create a new car location or add cars to an existing location. // Note: if price <= 0 and the car location already exists, it maintains // its current price. @Override public boolean addCars(int id, String location, int numCars, int carPrice) { Trace.info( "RM::addCars(" + id + ", " + location + ", " + numCars + ", $" + carPrice + ") called."); synchronized (syncLock) { Car curObj = (Car) readData(id, Car.getKey(location)); if (curObj == null) { // Doesn't exist; add it. Car newObj = new Car(location, numCars, carPrice); writeData(id, newObj.getKey(), newObj); Trace.info( "RM::addCars(" + id + ", " + location + ", " + numCars + ", $" + carPrice + ") OK."); } else { // Add count to existing object and update price. curObj.setCount(curObj.getCount() + numCars); if (carPrice > 0) { curObj.setPrice(carPrice); } writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addCars(" + id + ", " + location + ", " + numCars + ", $" + carPrice + ") OK: " + "cars = " + curObj.getCount() + ", price = $" + carPrice); } return (true); } }
// Create a new room location or add rooms to an existing location. // Note: if price <= 0 and the room location already exists, it maintains // its current price. @Override public boolean addRooms(int id, String location, int numRooms, int roomPrice) { Trace.info( "RM::addRooms(" + id + ", " + location + ", " + numRooms + ", $" + roomPrice + ") called."); Room curObj = (Room) readData(id, Room.getKey(location)); synchronized (syncLock) { if (curObj == null) { // Doesn't exist; add it. Room newObj = new Room(location, numRooms, roomPrice); writeData(id, newObj.getKey(), newObj); Trace.info( "RM::addRooms(" + id + ", " + location + ", " + numRooms + ", $" + roomPrice + ") OK."); } else { // Add count to existing object and update price. curObj.setCount(curObj.getCount() + numRooms); if (roomPrice > 0) { curObj.setPrice(roomPrice); } writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addRooms(" + id + ", " + location + ", " + numRooms + ", $" + roomPrice + ") OK: " + "rooms = " + curObj.getCount() + ", price = $" + roomPrice); } return (true); } }
// Returns data structure containing customer reservation info. Returns null if the // customer doesn't exist. Returns empty RMHashtable if customer exists but has no // reservations. public RMHashtable getCustomerReservations(int id, int customerID) throws RemoteException { Trace.info("RM::getCustomerReservations(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::getCustomerReservations failed(" + id + ", " + customerID + ") failed--customer doesn't exist"); return null; } else { return cust.getReservations(); } // if }
@Override protected IStatus run(IProgressMonitor monitor) { try { connect(client); } catch (Exception e) { Trace.debug( "Exception starting watch on project {0} and {1} kind", e, project.getName(), kind); backoff++; if (backoff >= FIBONACCI.length) { Trace.info( "Exceeded backoff attempts trying to reconnect watch for {0} and kind {1}", project.getName(), kind); watches.remove(project); state.set(State.DISCONNECTED); return Status.OK_STATUS; } final long delay = FIBONACCI[backoff] * BACKOFF_MILLIS; Trace.debug( "Delaying watch restart by {0}ms for project {1} and kinds {2} ", delay, project.getName(), kind); new RestartWatchJob(client).schedule(delay); } return Status.OK_STATUS; }
// reserve an item protected boolean reserveItem(int id, int customerID, String key, String location) { Trace.info( "RM::reserveItem( " + id + ", customer=" + customerID + ", " + key + ", " + location + " ) called"); // Read customer object if it exists (and read lock it) Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::reserveCar( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); return false; } // check if the item is available ReservableItem item = (ReservableItem) readData(id, key); if (item == null) { Trace.warn( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); return false; } else if (item.getCount() == 0) { Trace.warn( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); return false; } else { Flight tempItem = new Flight(Integer.parseInt(item.getLocation()), item.getCount(), item.getPrice()); tempItem.setReserved(item.getReserved()); Customer temp = cust.clone(); temp.setType(1); if (readDataFromLog(id, cust.getKey(), id) == null) { writeDataToLog(id, cust.getKey(), temp); } cust.reserve(key, location, item.getPrice()); writeData(id, cust.getKey(), cust); // decrease the number of available items in the storage item.setCount(item.getCount() - 1); item.setReserved(item.getReserved() + 1); if (readDataFromLog(id, item.getKey(), id) == null) { tempItem.setType(0); writeDataToLog(id, item.getKey(), tempItem); } Trace.info( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") succeeded"); return true; } }