Changeset 8 for src


Ignore:
Timestamp:
11/28/19 14:41:10 (5 years ago)
Author:
bart
Message:

Release 1.1.0

Location:
src
Files:
8 added
9 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/geniusweb/partiesserver/RunParty.java

    r1 r8  
    6060                String name = request.getPathInfo().substring(1);
    6161                log.log(Level.INFO, "request to run " + name);
    62 
     62                System.setSecurityManager(new SimpleSecurityManager());
    6363                AvailableParty party = AvailablePartiesRepo.instance().get(name);
    6464                if (party == null) {
  • src/main/java/geniusweb/partiesserver/repository/AvailablePartiesRepo.java

    r1 r8  
    1212 * Stores all currently available parties that can be run.
    1313 */
    14 public class AvailablePartiesRepo extends DefaultListenable<String> implements Repository<String, AvailableParty> {
     14public class AvailablePartiesRepo extends DefaultListenable<String>
     15                implements Repository<String, AvailableParty> {
    1516        private static final AvailablePartiesRepo instance = new AvailablePartiesRepo();
    1617
     
    3738        public void put(AvailableParty entity) {
    3839                availableParties.put(entity.getID(), entity);
    39                 notifyChange(entity.getID());
     40                notifyListeners(entity.getID());
    4041        }
    4142
     
    4344        public void remove(String id) {
    4445                availableParties.remove(id);
    45                 notifyChange(id);
     46                notifyListeners(id);
    4647        }
    4748
  • src/main/java/geniusweb/partiesserver/repository/RunningPartiesRepo.java

    r1 r8  
    7474                        runningParties.put(id, newParty);
    7575                }
    76                 notifyChange(id);
     76                notifyListeners(id);
    7777        }
    7878
     
    8787                        runningParties.put(id, newParty);
    8888                }
    89                 notifyChange(id);
     89                notifyListeners(id);
    9090
    9191        }
     
    125125                        }
    126126                }
    127                 notifyChange(id);
     127                notifyListeners(id);
    128128        }
    129129
  • src/main/java/geniusweb/partiesserver/repository/RunningParty.java

    r4 r8  
    99import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
    1010
    11 import geniusweb.actions.Action;
    1211import geniusweb.actions.PartyId;
    1312import geniusweb.connection.DefaultConnection;
    1413import geniusweb.partiesserver.RunningPartiesUpdater;
     14import geniusweb.partiesserver.websocket.PartySocket;
    1515import geniusweb.party.Party;
    1616import geniusweb.party.inform.Inform;
     
    4040         * set when someone contacts our socket. null if not set.
    4141         */
    42         private final transient DefaultConnection<Inform, Action> connection;
     42        private final transient PartySocket connection;
    4343
    4444        /**
     
    5858         */
    5959        protected RunningParty(Party party, PartyId id, String name, Date start,
    60                         Date end, DefaultConnection conn) {
     60                        Date end, PartySocket conn) {
    6161                if (party == null || id == null || name == null || start == null
    6262                                || end == null) {
     
    101101         * @param name         The name, this should match the filename in the repo
    102102         *                     (without the .jar).
    103          * @param maxRunTimeMS the maximum runtime for this party (ms). The {@link RunningPartiesUpdater}
    104          * will keep an eye on the time and handle removal after time is up.
     103         * @param maxRunTimeMS the maximum runtime for this party (ms). The
     104         *                     {@link RunningPartiesUpdater} will keep an eye on the
     105         *                     time and handle removal after time is up.
    105106         *
    106107         */
     
    212213         */
    213214        public void inform(Inform info) {
    214                 connection.notifyChange(info);
     215                connection.notifyListeners(info);
    215216        }
    216217
     
    223224         * @throws IllegalStateException if the party is already connected.
    224225         */
    225         public RunningParty withConnection(
    226                         DefaultConnection<Inform, Action> newconn) {
     226        public RunningParty withConnection(PartySocket newconn) {
    227227                if (newconn == null)
    228228                        throw new NullPointerException("newconn must be not null");
  • src/main/java/geniusweb/partiesserver/websocket/PartySocket.java

    r4 r8  
    55import java.net.URI;
    66import java.util.Date;
     7import java.util.List;
     8import java.util.concurrent.CopyOnWriteArrayList;
    79import java.util.logging.Level;
    810
     
    2325import geniusweb.actions.Action;
    2426import geniusweb.actions.PartyId;
    25 import geniusweb.connection.DefaultConnection;
     27import geniusweb.connection.ConnectionEnd;
    2628import geniusweb.partiesserver.repository.RunningPartiesRepo;
    2729import geniusweb.partiesserver.repository.RunningParty;
    2830import geniusweb.party.Party;
    29 import geniusweb.party.inform.Finished;
    3031import geniusweb.party.inform.Inform;
    3132import geniusweb.party.inform.Settings;
     
    4142 */
    4243@ServerEndpoint("/party/{party}")
    43 public class PartySocket extends DefaultConnection<Inform, Action> {
     44public class PartySocket implements ConnectionEnd<Inform, Action> {
    4445        private final static ObjectMapper jackson = new ObjectMapper();
    4546        private final Reporter log;
    4647        private final RunningPartiesRepo runningparties;
     48        private final List<Listener<Inform>> listeners = new CopyOnWriteArrayList<Listener<Inform>>();
    4749
    4850        // should all be final, except that we can only set them when start is
     
    8688                                                runningparties.removeListener(this);
    8789                                                session.close(new CloseReason(CloseCodes.GOING_AWAY,
    88                                                                 "party died"));
     90                                                                "detected that party vanished"));
    8991                                        } catch (IOException e) {
    9092                                                log.log(Level.WARNING,
     
    131133                                party.inform(info);
    132134                        } catch (Throwable e) {
    133                                 // simple sandbox
    134                                 log.log(Level.WARNING, "Party failed on inform:", e);
     135                                log.log(Level.WARNING, "Party failed on inform.", e);
     136                                // we don't use jackson.writeValueAsString(e) here because
     137                                // CloseReason has 123 char limit.
     138                                // The error will be too large to fit and will be truncated
     139                                // therefore we send a plain string and hope that enough
     140                                // will arrive at the other side (the protocol)
     141                                session.close(new CloseReason(
     142                                                CloseReason.CloseCodes.CLOSED_ABNORMALLY,
     143                                                "party threw exception: " + collectReasons(e)));
    135144                        }
    136                 } // else dead but that's handled in the listener above
     145                } // else party is dead but that's handled in the listener above
     146        }
     147
     148        /**
     149         *
     150         * @param e an exception
     151         * @return collect all reasons and sub-reasons
     152         */
     153        private String collectReasons(Throwable e) {
     154                String reason = "";
     155                do {
     156                        reason = reason + " : " + e.getClass().getSimpleName() + ":"
     157                                        + e.getMessage();
     158                        e = e.getCause();
     159                } while (e != null);
     160                return reason;
    137161        }
    138162
     
    145169         */
    146170        private void sniff(Inform info, RunningParty party) throws IOException {
    147                 if (info instanceof Finished) {
    148                         session.close(new CloseReason(CloseCodes.GOING_AWAY,
    149                                         "session reached agreement"));
    150                 } else if (info instanceof Settings) {
     171                // do NOT sniff Finished messages and close sockets. Parties and their
     172                // sockets need to stay running till all messages are handled
     173                if (info instanceof Settings) {
    151174                        Date end = ((Settings) info).getProgress().getTerminationTime();
    152175                        runningparties.replace(party.withEndTime(end));
     
    209232        }
    210233
     234        @Override
     235        public void addListener(Listener<Inform> l) {
     236                listeners.add(l);
     237        }
     238
     239        @Override
     240        public void removeListener(Listener<Inform> l) {
     241                listeners.remove(l);
     242        }
     243
     244        /**
     245         * This should only be called by the owner of the listenable, not by
     246         * listeners or others. Avoid calling this from synchronized blocks as a
     247         * notified listener might immediately make more calls to you.
     248         * <p>
     249         * Any listeners that throw an exception will be intercepted and their
     250         * stacktrace is printed.
     251         *
     252         * @param data information about the change.
     253         */
     254        public void notifyListeners(Inform data) {
     255                for (Listener<Inform> l : listeners) {
     256                        l.notifyChange(data);
     257                }
     258        }
     259
    211260}
  • src/test/java/geniusweb/partiesserver/AvailablePartiesUpdaterTest.java

    r1 r8  
    3131
    3232public class AvailablePartiesUpdaterTest {
     33        private static final String RANDOMPARTY = "target/jars/randomparty-1.1.0.jar";
    3334        private static final int TESTRATE = 200; // check file changes every 200ms
    3435        @SuppressWarnings("unchecked")
     
    146147        public void randomPartyJarTest() throws IOException, InterruptedException {
    147148                init();
    148                 Files.copy(Paths.get("target/jars/randomparty-1.0.0.jar"),
    149                                 tmpdir.resolve(RNDPARTY));
     149                Files.copy(Paths.get(RANDOMPARTY), tmpdir.resolve(RNDPARTY));
    150150
    151151                Thread.sleep(2 * TESTRATE);
     
    157157        public void removeJarTest() throws IOException, InterruptedException {
    158158                init();
    159                 Files.copy(Paths.get("target/jars/randomparty-1.0.0.jar"), RNDPARTY);
     159                Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
    160160                Thread.sleep(2 * TESTRATE);
    161161                assertEquals(1, repo.list().size());
     
    169169        public void removeRootTest() throws IOException, InterruptedException {
    170170                init();
    171                 Files.copy(Paths.get("target/jars/randomparty-1.0.0.jar"), RNDPARTY);
     171                Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
    172172                Thread.sleep(2 * TESTRATE);
    173173                assertEquals(1, repo.list().size());
  • src/test/java/geniusweb/partiesserver/JavaClientTest.java

    r7 r8  
    3939 */
    4040public class JavaClientTest {
    41 //      private static final String JSON = "{\"jobs\":[\"jobs1\"]}";
     41        private static final String RANDOMPARTY = "http://localhost:8080/partiesserver/run/randomparty-1.1.0";
     42        // private static final String JSON = "{\"jobs\":[\"jobs1\"]}";
    4243//      private static final String JOBS1PROFILE = "{\"LinearAdditiveUtilitySpace\":{\"issueUtilities\":{\"lease car\":{\"discreteutils\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},\"permanent contract\":{\"discreteutils\":{\"valueUtilities\":{\"no\":0,\"yes\":1}}},\"career development opportunities\":{\"discreteutils\":{\"valueUtilities\":{\"high\":1,\"low\":0,\"medium\":0.5}}},\"fte\":{\"discreteutils\":{\"valueUtilities\":{\"1.0\":0.75,\"0.6\":0.25,\"0.8\":0.5}}},\"salary\":{\"discreteutils\":{\"valueUtilities\":{\"4000\":1.0,\"2500\":0.25,\"3500\":0.75,\"2000\":0,\"3000\":0.3}}},\"work from home\":{\"discreteutils\":{\"valueUtilities\":{\"1\":0.5,\"2\":0.666666666666,\"0\":0.333333333}}}},\"issueWeights\":{\"lease car\":0.06,\"permanent contract\":0.16,\"career development opportunities\":0.04,\"fte\":0.32,\"salary\":0.24,\"work from home\":0.18},\"domain\":{\"name\":\"jobs\",\"issuesValues\":{\"lease car\":{\"values\":[\"yes\",\"no\"]},\"permanent contract\":{\"values\":[\"yes\",\"no\"]},\"career development opportunities\":{\"values\":[\"low\",\"medium\",\"high\"]},\"fte\":{\"values\":[\"0.6\",\"0.8\",\"1.0\"]},\"salary\":{\"values\":[\"2000\",\"2500\",\"3000\",\"3500\",\"4000\"]},\"work from home\":{\"values\":[\"0\",\"1\",\"2\"]}}},\"name\":\"jobs1\"}}";
    4344        private EmbeddedTomcat tomcat = new EmbeddedTomcat();
     
    6061        public void after() throws Throwable {
    6162                Thread.sleep(2000);
     63                System.setSecurityManager(null);
    6264                tomcat.stop();
    6365        }
     
    157159         */
    158160        private String startParty() throws IOException, InterruptedException {
    159                 URL url = new URL(
    160                                 "http://localhost:8080/partiesserver/run/randomparty-1.0.0");
     161                URL url = new URL(RANDOMPARTY);
    161162                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    162163                conn.setRequestMethod("GET");
  • src/test/java/geniusweb/partiesserver/repository/RunningPartyTest.java

    r1 r8  
    1616import com.fasterxml.jackson.databind.ObjectMapper;
    1717
    18 import geniusweb.actions.Action;
    1918import geniusweb.actions.PartyId;
    20 import geniusweb.connection.DefaultConnection;
    21 import geniusweb.partiesserver.repository.AvailableParty;
    22 import geniusweb.partiesserver.repository.RunningParty;
     19import geniusweb.partiesserver.websocket.PartySocket;
    2320import geniusweb.party.Capabilities;
    2421import geniusweb.party.DefaultParty;
     
    9087                RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
    9188                                null);
    92                 DefaultConnection<Inform, Action> connection = mock(
    93                                 DefaultConnection.class);
     89                PartySocket connection = mock(PartySocket.class);
    9490                rparty = rparty.withConnection(connection);
    9591        }
     
    9995                RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
    10096                                null);
    101                 DefaultConnection<Inform, Action> connection = mock(
    102                                 DefaultConnection.class);
     97                PartySocket connection = mock(PartySocket.class);
    10398                rparty = rparty.withConnection(connection);
    10499                rparty = rparty.withConnection(connection);
     
    112107                RunningParty rparty = new RunningParty(party1, id1, "name1", now, end,
    113108                                null);
    114                 DefaultConnection<Inform, Action> connection = mock(
    115                                 DefaultConnection.class);
     109                PartySocket connection = mock(PartySocket.class);
    116110                rparty = rparty.withConnection(connection);
    117111
     
    120114
    121115                ArgumentCaptor<Inform> argument = ArgumentCaptor.forClass(Inform.class);
    122                 verify(connection, times(1)).notifyChange(argument.capture());
     116                verify(connection, times(1)).notifyListeners(argument.capture());
    123117                assertEquals(info, argument.getValue());
    124118        }
  • src/test/java/geniusweb/partiesserver/websocket/PartySocketTest.java

    r1 r8  
    2323import geniusweb.actions.EndNegotiation;
    2424import geniusweb.actions.PartyId;
    25 import geniusweb.connection.Connection;
     25import geniusweb.connection.ConnectionEnd;
    2626import geniusweb.partiesserver.repository.RunningPartiesRepo;
    2727import geniusweb.partiesserver.repository.RunningParty;
    28 import geniusweb.partiesserver.websocket.PartySocket;
    2928import geniusweb.party.Party;
    3029import geniusweb.party.inform.Inform;
     
    8382
    8483                @SuppressWarnings("unchecked")
    85                 Connection<Inform, Action> connection = mock(Connection.class);
     84                ConnectionEnd<Inform, Action> connection = mock(ConnectionEnd.class);
    8685                /*
    8786                 * we must call runningparty directly on this one because
Note: See TracChangeset for help on using the changeset viewer.