Advertisement
MatthijsFontys

Event runner Big Idea

Jan 13th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package nl.fhict.i400433.websocketservice.logic.events;
  2.  
  3. import nl.fhict.i400433.websocketservice.logic.Auction;
  4. import nl.fhict.i400433.websocketservice.logic.LogicFactory;
  5. import nl.fhict.i400433.websocketservice.logic.components.AuctionHolder;
  6. import nl.fhict.i400433.websocketservice.logic.components.ItemHolder;
  7. import nl.fhict.i400433.websocketservice.logic.components.MarketManager;
  8. import nl.fhict.i400433.websocketservice.logic.item.Item;
  9. import nl.fhict.i400433.websocketservice.websockets.WebSocketServer;
  10.  
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.concurrent.*;
  14.  
  15. public class EventRunner implements Runnable {
  16.  
  17.     private ScheduledExecutorService threadPool;
  18.     private final Map<Future, IEvent> futureToEventMap;
  19.     private boolean isRunning;
  20.  
  21.  
  22.     public EventRunner(MarketManager marketManager) {
  23.         threadPool = Executors.newScheduledThreadPool(10);
  24.         ((ScheduledThreadPoolExecutor) threadPool).setRemoveOnCancelPolicy(true);
  25.         futureToEventMap = new HashMap<>();
  26.  
  27.         // Test auction for startup
  28.         Auction auction = new Auction.Builder(new Item.Builder("Cowboy hat").build(),
  29.                 100000, marketManager.getAuctionHolder().getAuctionMessageSender())
  30.                 .setStartingBid(100)
  31.                 .setMinIncrement(50).build();
  32.  
  33.         marketManager.getAuctionHolder().addAuction(auction);
  34.  
  35.         addEvent(new AuctionEvent(auction));
  36.         addEvent(new CreateAuctionEvent(marketManager.getAuctionHolder(), marketManager.getItemHolder()));
  37.         addEvent(new GiveBudgetEvent(marketManager));
  38.  
  39.  
  40.     }
  41.  
  42.     public void addEvent(IEvent event){
  43.         Future future = threadPool.scheduleWithFixedDelay(event, event.getInitialDelay(), event.getDelay(), TimeUnit.MILLISECONDS);
  44.         synchronized (futureToEventMap){
  45.             futureToEventMap.put(future, event);
  46.         }
  47.     }
  48.  
  49.  
  50.     @Override
  51.     public void run() {
  52.         isRunning = true;
  53.         while(isRunning) {
  54.             try {
  55.                 Thread.sleep(60_000);
  56.             } catch (InterruptedException ignored) {
  57.                 Thread.currentThread().interrupt();
  58.             }
  59.  
  60.             for (Map.Entry<Future, IEvent> futureEvent : futureToEventMap.entrySet()) {
  61.                 if (futureEvent.getValue().shouldDelete()) {
  62.                     futureEvent.getKey().cancel(false);
  63.                     synchronized (futureToEventMap) {
  64.                         futureToEventMap.remove(futureEvent.getKey());
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.     public void stop(){
  72.         isRunning = false;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement