Ignore:
Timestamp:
01/28/20 10:19:54 (4 years ago)
Author:
bart
Message:

Update 28 jan 2020

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocol/src/main/java/geniusweb/protocol/session/saop/SAOPState.java

    r9 r10  
    1616import geniusweb.progress.ProgressRounds;
    1717import geniusweb.protocol.ProtocolException;
     18import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
    1819import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
    19 import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
    2020import geniusweb.protocol.session.DefaultSessionState;
    2121import geniusweb.protocol.session.SessionSettings;
     
    6161
    6262        /**
    63          * @param actor         the actor that did this action. Can be used to check
    64          *                      if action is valid. NOTICE caller has to make sure
    65          *                      the current state is not final.
    66          * @param action        the action that was proposed by actor.
    67          * @param currentTimeMs the current time in ms since 1970, see
    68          *                      {@link System#currentTimeMillis()}
    69          * @return new SessionState with the action added as last action.
    70          */
    71         public SAOPState with(PartyId actor, Action action) {
    72                 try {
    73                         return with1(actor, action);
    74                 } catch (ProtocolException e) {
    75                         List<Action> newactions = new LinkedList<>(getActions());
    76                         newactions.add(action);
    77                         return new SAOPState(newactions, getConnections(), getProgress(),
    78                                         getSettings(), getPartyProfiles(), e);
    79                 }
    80         }
    81 
    82         /**
    8363         *
    8464         * @param connection   the new {@link ProtocolToPartyConn}
     
    164144
    165145        /**
     146         * @param actor         the actor that did this action. Can be used to check
     147         *                      if action is valid. NOTICE caller has to make sure
     148         *                      the current state is not final.
     149         * @param action        the action that was proposed by actor.
     150         * @param currentTimeMs the current time in ms since 1970, see
     151         *                      {@link System#currentTimeMillis()}
     152         * @return new SessionState with the action added as last action.
     153         */
     154
     155        public SAOPState with(PartyId actor, Action action) {
     156
     157                if (actor == null) { // this is a bug
     158                        throw new IllegalArgumentException("actor must not be null");
     159                }
     160                if (!actor.equals(getNextActor())) {
     161                        throw new IllegalArgumentException("Party does not have the turn ");
     162
     163                }
     164                if (action == null) {
     165                        throw new IllegalArgumentException("action is null");
     166                }
     167                if (!actor.equals(action.getActor())) {
     168                        throw new IllegalArgumentException(
     169                                        "act contains wrong credentials: " + action);
     170                }
     171
     172                // check protocol is followed for specific actions
     173                if (action instanceof Accept) {
     174                        Bid bid = getLastBid();
     175                        if (bid == null) {
     176                                throw new IllegalArgumentException(
     177                                                "Accept without a recent offer");
     178                        }
     179                        if (!bid.equals(((Accept) action).getBid())) {
     180                                throw new IllegalArgumentException(
     181                                                "Party accepts a bid differing from the last offer ="
     182                                                                + bid + ", action=" + action + ")");
     183                        }
     184                } else if (action instanceof Offer) {
     185                        // offer is always fine. We don't check if bid is actually in domain
     186                        // or complete etc. We would need domain for that
     187                } else if (action instanceof EndNegotiation) {
     188                        // just act, we will get into the final state then.
     189                } else {
     190                        throw new IllegalArgumentException(
     191                                        "Action " + action + " is not allowed in SAOP");
     192                }
     193
     194                List<Action> newactions = new LinkedList<>(getActions());
     195                newactions.add(action);
     196                return new SAOPState(newactions, getConnections(), advanceProgress(),
     197                                getSettings(), getPartyProfiles(), null);
     198        }
     199
     200        /**
    166201         * Check up to nparticipants-1 steps back if there was an offer.
    167202         *
     
    182217        }
    183218
    184         private SAOPState with1(PartyId actor, Action action)
    185                         throws ProtocolException {
    186 
    187                 if (actor == null) { // this is a bug
    188                         throw new IllegalArgumentException("actor must not be null");
    189                 }
    190                 if (!actor.equals(getNextActor())) {
    191                         throw new ProtocolException("Party does not have the turn ",
    192                                         actor.getName());
    193 
    194                 }
    195                 if (action == null) {
    196                         throw new ProtocolException("action is null", actor.getName());
    197                 }
    198                 if (!actor.equals(action.getActor())) {
    199                         throw new ProtocolException(
    200                                         "act contains wrong credentials: " + action,
    201                                         actor.getName());
    202                 }
    203 
    204                 // check protocol is followed for specific actions
    205                 if (action instanceof Accept) {
    206                         Bid bid = getLastBid();
    207                         if (bid == null) {
    208                                 throw new ProtocolException("Accept without a recent offer",
    209                                                 actor.getName());
    210                         }
    211                         if (!bid.equals(((Accept) action).getBid())) {
    212                                 throw new ProtocolException(
    213                                                 "Party accepts a bid differing from the last offer ="
    214                                                                 + bid + ", action=" + action + ")",
    215                                                 actor.getName());
    216                         }
    217                 } else if (action instanceof Offer) {
    218                         // offer is always fine. We don't check if bid is actually in domain
    219                         // or complete etc. We would need domain for that
    220                 } else if (action instanceof EndNegotiation) {
    221                         // just act, we will get into the final state then.
    222                 } else {
    223                         throw new ProtocolException(
    224                                         "Action " + action + " is not allowed in SAOP",
    225                                         actor.getName());
    226                 }
    227 
    228                 List<Action> newactions = new LinkedList<>(getActions());
    229                 newactions.add(action);
    230                 return new SAOPState(newactions, getConnections(), advanceProgress(),
    231                                 getSettings(), getPartyProfiles(), null);
    232         }
    233 
    234219        /**
    235220         *
Note: See TracChangeset for help on using the changeset viewer.