// Test method public LinkedList<Integer> getLiveAuctionsIds() throws RemoteException { LinkedList<Integer> ll = new LinkedList<Integer>(); for (AuctionItem t : liveAuctionItems.values()) { ll.add(t.getAuctionId()); } return ll; }
@Override public LinkedList<String> getExistingAuctions() throws RemoteException { LinkedList<String> ll = new LinkedList<String>(); for (AuctionItem i : liveAuctionItems.values()) { ll.add(i.toString()); } return ll; }
public static void show(Long id) { System.out.printf("=== id: %d\n", id); List<AuctionItem> items = AuctionItem.findAll(); for (AuctionItem item : items) { System.out.printf("--- id: %d\n", item.id); } AuctionItem item = AuctionItem.findById(id); item.viewCount++; item.save(); render(item); }
@Override // Event though the data structure for live auctions is concurrent the method still needs to be // synchronized because the creation // of auction items concurrently produces auction items with the same id. public synchronized boolean registerAuctionItem( String name, double min_item_value, Date closing_date, int client) throws RemoteException { AuctionItem item = new AuctionItem(name, min_item_value, closing_date, client, this); liveAuctionItems.put(item.getAuctionId(), item); Thread t = new Thread(item); t.start(); return true; }
public static void doCreateItem(@Valid AuctionItem item) throws IOException { if (validation.hasErrors()) { System.out.println(validation.errorsMap()); render("@createAuctionItem", item); } // set the user based on the logged in user item.createdBy = Authenticate.getLoggedInUser(); item.save(); show(item.id); }
@Override public boolean moveToExpiredAuctions(int auctionId) { if (!liveAuctionItems.containsKey(auctionId)) { System.out.println("This auction does not exist\n"); return false; } AuctionItem temp; if ((temp = liveAuctionItems.remove(auctionId)) != null) { System.out.println("Auction successfully removed from list with live auctions\n"); // Change the auction id to correspond to the index expiredAuctions.put(temp.getAuctionId(), temp); Thread t = new Thread(temp); t.start(); return true; } return false; }
public static void search(String search, Integer page) { validation.required(search).message("You must enter something to search for"); if (validation.hasErrors()) { render(); } page = page == null ? 1 : page; SearchResults results = AuctionItem.search(search, page); render(results, page, search); }
public static void newBids(Long id) { // count new bids long newBids = Bid.count( "from AuctionItem a join a.bids as b " + "where a.id = ? AND b.date > ?", id, request.date); // wait if needed if (newBids == 0) { await("1s"); } // return the JSON output of the new bids AuctionItem item = AuctionItem.findById(id); JsonObject json = new JsonObject(); json.addProperty("next", item.getNextBidPrice()); json.addProperty("top", item.getCurrentTopBid()); renderJSON(json.toString()); }
@Override public void notifyResult(AuctionItem item) { if (registeredUsers.containsKey(item.getCreator_id())) { RMIClientIntf creator = registeredUsers.get(item.getCreator_id()); try { creator.callBack("Your Auction has produced the following results :\n "); creator.callBack(item.getResult()); } catch (RemoteException e) { System.out.println("Creator no longer online\n"); } } else { System.out.println("Creator id does not exist\n"); } // Normally an auction with no creator registered should not exist but since this is a // simplified implementation // We allow bids on auctions with no creator (usually the initialised ones) for testing // purposes. if (item.numberOfBids() != 0) { System.out.println("Notifying bidders"); Iterator<Bid> it = item.getBidders(); while (it.hasNext()) { Bid bidder = it.next(); if (registeredUsers.containsKey(bidder.getUserId())) { RMIClientIntf client = registeredUsers.get(bidder.getUserId()); try { client.callBack(item.getResult()); } catch (RemoteException e) { System.out.println("Bidder with id " + bidder.getUserId() + " no longer online\n"); } } else { System.out.println("User id does not exist\n"); } } } }
public static void main(String args[]) { String host = "localhost"; int port = 1099; if (args.length > 0) { host = args[0]; port = Integer.parseInt(args[1]); } // Initialise auctions from permanent storage BufferedReader br = null; BufferedWriter bw = null; try { AuctionServer rmiServer = new AuctionServer(); Registry reg = LocateRegistry.getRegistry(host, port); reg.rebind("auctionServer", rmiServer); System.out.println("AuctionServer is ready"); System.out.println( "Select one of the two options :\n1: To save live auctions\n2: To load test auctions that expire in a minute (note you need to start 5 clients first) "); Scanner sc = new Scanner(System.in); bw = new BufferedWriter(new FileWriter("auctionsSaved.dat")); switch (sc.nextInt()) { case 1: for (AuctionItem item : rmiServer.liveAuctionItems.values()) { bw.write(item.toString() + "\n"); } System.out.println("Saved auctions state."); break; case 2: GenerateAuctions generator = new GenerateAuctions(); generator.generateList(); br = new BufferedReader(new FileReader("auctionsBackup.dat")); String currentLine; while ((currentLine = br.readLine()) != null) { String[] array = currentLine.split(","); String name = array[0]; double min_item_value = Double.parseDouble(array[1]); Date closing_date = new SimpleDateFormat("d/M/y H:mm:s").parse(array[2]); int client = Integer.parseInt(array[3]); rmiServer.registerAuctionItem(name, min_item_value, closing_date, client); } System.out.println("Initialised auctions..."); break; } } catch (RemoteException e) { System.out.println("Exception in AuctionServer.main " + e); } catch (MalformedURLException ue) { System.out.println("MalformedURLException in RMIServerImp.main " + ue); } catch (IOException e) { System.out.println("Could not open file "); e.printStackTrace(); } catch (ParseException e) { System.out.println("Unable to parse date"); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { System.out.println("Unable to close file."); } } if (bw != null) { try { bw.close(); } catch (IOException e) { System.out.println("Unable to close file."); } } } }
public static void showImage(Long id) { AuctionItem item = AuctionItem.findById(id); response.setContentTypeIfNotSet(item.photo.type()); renderBinary(item.photo.get()); }
public static void index() { List<AuctionItem> mostPopular = AuctionItem.getMostPopular(5); List<AuctionItem> endingSoon = AuctionItem.getEndingSoon(5); render(mostPopular, endingSoon); }
public static void showPDF(Long id) { AuctionItem item = AuctionItem.findById(id); item.viewCount++; item.save(); renderPDF(item); }
public static void recentlyAdded() { List<AuctionItem> items = AuctionItem.recentlyAdded(50); render(items); }
public static void addBid(Long id, Float amount) { AuctionItem item = AuctionItem.findById(id); item.addBid(amount); item.save(); }