List of usage examples for org.apache.commons.exec Watchdog Watchdog
public Watchdog(final long timeout)
From source file:edu.biu.scapi.comm.twoPartyComm.SocketCommunicationSetup.java
/** * Initiates the creation of the actual sockets connections between the parties. If this function succeeds, the * application may use the send and receive functions of the created channels to pass messages. * @throws TimeoutException in case a timeout has occurred before all channels have been connected. *///from w ww.j a v a2 s .c om @Override public Map<String, Channel> prepareForCommunication(String[] connectionsIds, long timeOut) throws TimeoutException { //Start the watch dog with the given timeout. watchdog = new Watchdog(timeOut); //Add this instance as the observer in order to receive the event of time out. watchdog.addTimeoutObserver(this); watchdog.start(); //Establish the connections. establishConnections(connectionsIds); //Verify that all connections have been connected. connector.verifyConnectingStatus(); //If we already know that all the connections were established we can stop the watchdog. watchdog.stop(); //In case of timeout, throw a TimeoutException if (bTimedOut) { throw new TimeoutException("timeout has occurred"); } //Set Nagle algorithm. if (enableNagle) connector.enableNagle(); //Update the number of the created connections. connectionsNumber += connector.getConnectionsCount(); //Return the map of channels held in the established connection object. Map<String, Channel> connections = connector.getConnections(); connector.reset(); return connections; }
From source file:edu.biu.scapi.comm.CommunicationSetup.java
/** * This function is package private and is called by the public prepareFunctions. It requests a KeyExchangeProtocol that has not been implemented yet. * It initiates the creation of the final actual socket connections between the parties. If this function succeeds, the * application may use the send and receive functions of the created channels to pass messages. * // ww w. jav a 2 s . com * @param listOfParties the original list of parties to connect to. As a convention, we will set the <B>first party</B> in the list to be the <B>requesting party</B>, that is, * the party represented by the application. * @param keyExchange the key exchange algorithm protocol to use after a channel is connected * @param successLevel the ConnectivitySuccessVerifier algorithm to use * @param timeOut the maximum amount of time we allow for the connection stage * @return a set of connected and ready channels to be used by the parties to send and receive data, it may be null if none succeeded */ Map<InetSocketAddress, Channel> prepareForCommunication(List<Party> listOfParties, KeyExchangeProtocol keyExchange, ConnectivitySuccessVerifier successLevel, long timeOut) { //set parameters partiesList = listOfParties; keyExchangeProtocol = keyExchange; connectivitySuccessVerifier = successLevel; establishedConnections = new EstablishedConnections(); //initialize the threadVector and the map of the key exchange outputs threadsVector = new Vector<SecuringConnectionThread>(); keyExchangeMap = new HashMap<InetSocketAddress, KeyExchangeOutput>(); //start the watch dog with timeout watchdog = new Watchdog(timeOut); //add this instance as the observer in order to receive the event of time out. watchdog.addTimeoutObserver(this); watchdog.start(); //establish connections. try { establishAndSecureConnections(); } catch (DuplicatePartyException e) { Logging.getLogger().log(Level.SEVERE, e.toString()); } //verify connection verifyConnectingStatus(); //run success function if (!runSuccessAlgo()) { //remove connections from the list of established connections //Eventually, when different ConnectivitySuccessVerifiers are implemented there might be an impact what happens to the connections that did succeed. //For example, assume that only 80% of the connections requested were established but the ConnectivitySuccessVerifier specified requires 100%. Then, obviously //the function didn't succeed and null should be returned. However, it would be a pity to let go (delete and remove) the 80% of connections that did succeed. //It may be possible (and this will depend on the requirements of the ConnectivitySuccessVerifier) to keep the already established connections and only try //to establish what is missing. In some other cases it will be necessary to start everything over form the beginning. As a conclusion, this is left for future implementation. return null; } //remove all connections with not READY state establishedConnections.removeNotReadyConnections(); //set nagle algorithm establishedConnections.enableNagle(enableNagle); //update the security level for each connection //setSecurityLevel(); //return the map of channels held in the established connection object. return establishedConnections.getConnections(); }
From source file:edu.biu.scapi.comm.twoPartyComm.QueueCommunicationSetup.java
@Override public Map<String, Channel> prepareForCommunication(String[] connectionsIds, long timeOut) throws TimeoutException { //Start the watch dog with the given timeout. watchdog = new Watchdog(timeOut); //Add this instance as the observer in order to receive the event of time out. watchdog.addTimeoutObserver(this); watchdog.start();//from w w w . j ava 2s. co m //Create a map to hold each created channel. Map<String, Channel> connectedChannels = new HashMap<String, Channel>(); //For each connection between the two parties, create a Queue channel. int size = connectionsIds.length; for (int i = 0; i < size && !bTimedOut; i++) { QueueChannel channel = new QueueChannel(me, other, connection, connectionsIds[i], destroyer); //put the created channel in the map. connectedChannels.put(connectionsIds[i], channel); } watchdog.stop(); if (bTimedOut) { Object[] channels = connectedChannels.values().toArray(); int len = channels.length; for (int i = 0; i < len; i++) { ((Channel) channels[i]).close(); } throw new TimeoutException("timeout has occurred"); } return connectedChannels; }