[31] | 1 | package geniusweb.runserver;
|
---|
| 2 |
|
---|
| 3 | import java.util.Collection;
|
---|
| 4 | import java.util.Collections;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 | import java.util.logging.Level;
|
---|
| 8 |
|
---|
| 9 | import tudelft.utilities.listener.DefaultListenable;
|
---|
| 10 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 11 | import tudelft.utilities.logging.Reporter;
|
---|
| 12 | import tudelft.utilities.repository.Repository;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Central repo for all currently running sessions.
|
---|
| 16 | */
|
---|
| 17 | public class RunningNegotiationsRepo extends DefaultListenable<String>
|
---|
| 18 | implements Repository<String, RunningNego> {
|
---|
| 19 |
|
---|
| 20 | private final Map<String, RunningNego> negotiations;
|
---|
| 21 | private final static RunningNegotiationsRepo instance = new RunningNegotiationsRepo(
|
---|
| 22 | new HashMap<>(), new ReportToLogger("runserver"));
|
---|
| 23 | private final Reporter log;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Only for testing and internal use
|
---|
| 27 | *
|
---|
| 28 | * @param newnegos the currently running negotiations
|
---|
| 29 | */
|
---|
| 30 | protected RunningNegotiationsRepo(Map<String, RunningNego> newnegos,
|
---|
| 31 | Reporter reporter) {
|
---|
| 32 | this.negotiations = newnegos;
|
---|
| 33 | this.log = reporter;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public static RunningNegotiationsRepo instance() {
|
---|
| 37 | return instance;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @Override
|
---|
| 41 | public RunningNego get(String id) {
|
---|
| 42 | return negotiations.get(id);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public Collection<RunningNego> list() {
|
---|
| 47 | return Collections.unmodifiableCollection(negotiations.values());
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
| 51 | public void put(RunningNego entity) {
|
---|
| 52 | if (negotiations.containsKey(entity.getID())) {
|
---|
| 53 | throw new IllegalArgumentException(
|
---|
| 54 | "Negotiation " + entity.getID() + " is already running");
|
---|
| 55 | }
|
---|
| 56 | negotiations.put(entity.getID(), entity);
|
---|
| 57 | notifyListeners(entity.getID());
|
---|
| 58 | log.log(Level.FINEST, "started " + entity.getID());
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | @Override
|
---|
| 62 | public void remove(String id) {
|
---|
| 63 | negotiations.remove(id);
|
---|
| 64 | notifyListeners(id);
|
---|
| 65 | log.log(Level.FINEST, "stopped negotiation " + id);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @Override
|
---|
| 69 | public void replace(RunningNego entity) {
|
---|
| 70 | if (!negotiations.containsKey(entity.getID())) {
|
---|
| 71 | throw new IllegalArgumentException(
|
---|
| 72 | "Negotiation " + entity.getID() + " is not running");
|
---|
| 73 | }
|
---|
| 74 | negotiations.put(entity.getID(), entity);
|
---|
| 75 | notifyListeners(entity.getID());
|
---|
| 76 | log.log(Level.FINEST, "replaced " + entity.getID());
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | }
|
---|