Code example for AtomicReference

Methods: get, set

0
    } 
 
    @Override 
    public void silent() { 
        if (log.isTraceEnabled()) { 
            log.trace(format("silent is being called [%s]", messageConnection));
        } 
        announceTerm = false;
    } 
 
    @Override 
    public void terminate() { 
        if (log.isTraceEnabled()) { 
            log.trace(format("terminate is being called [%s]",
                             messageConnection), new Exception());
        } 
        announceTerm = false;
        shutdown(); 
    } 
 
    @Override 
    public String toString() {
        return String.format("Message connection %s",
                             messageConnection == null ? "inbound, unestablished"
                                                      : messageConnection);
    } 
 
    private void initialMsg(TimedMsg tm) {
 
        if (log.isTraceEnabled()) { 
            log.trace(format("initialMsg is being called [%s]",
                             messageConnection));
        } 
 
        Object obj = tm;
        TimedMsg bytes = tm;
 
        /** 
         * must be a heartbeat message 
         */ 
        if (!(obj instanceof HeartbeatMsg)) {
            log.error(format("%s did not receive a heartbeat message first - shutdown",
                             me));
            shutdown(); 
            return; 
        } 
 
        HeartbeatMsg hbmsg = (HeartbeatMsg) obj;
 
        /** 
         * There must be a valid connection (heartbeat connection) 
         */ 
        if (!connectionSet.contains(hbmsg.getSender())) {
            if (log.isInfoEnabled()) { 
                log.info(format("%s did not have incoming connection for %s in the connection set",
                                me, hbmsg.getSender()));
            } 
            shutdown(); 
            return; 
        } 
 
        Connection con = connectionSet.getConnection(hbmsg.getSender());
 
        /** 
         * If it is a message connection then attempt to assign this impl to 
         * that connection. If successful then record the message connection so 
         * all further messages go directly to it. If not successful then 
         * shutdown the this implementation object and abort. 
         */ 
        if (con instanceof MessageConnection) {
            if (((MessageConnection) con).assignImpl(this)) {
                messageConnection.set((MessageConnection) con);
                messageConnection.get().deliver(bytes);
            } else { 
                log.error(format("Failed to assign existing msg connection impl: %s",
                                 con));
                silent(); 
                shutdown(); 
            } 
            return; 
        } 
 
        /** 
         * By now we should be left with a heartbeat connection - sanity check 
         */ 
        if (!(con instanceof HeartbeatConnection)) {
            log.error(format("%s ?!? incoming connection is in connection set, but not heartbeat or message type",
                             this));
            shutdown(); 
            return; 
        } 
        HeartbeatConnection hbcon = (HeartbeatConnection) con;
 
        /** 
         * If the connection is a heartbeat connection then the other end must 
         * be setting up the connection without this end having requested it. 
         * That means the other end must want it, so check the msgLink field for 
         * this end is set - this is a sanity check. 
         *  
         * ********************************************************************* 
         *  
         * The case can happen, so the above comment is incorrect. If the user 
         * does a connect and then disconnect without sending a message, then 
         * the other end could initiate a connection neither end needs in 
         * response to the initial connect. Do not count this as an error, but 
         * do log its occurance. 
         */ 
        if (!hbmsg.getMsgLinks().contains(me.id)) {
            if (log.isTraceEnabled()) { 
                log.trace(format("%s incoming connection from %s when neither end wants the connection",
                                 me, con.getSender()));
            } 
        } 
 
        /** 
         * Now we are left with a valid heartbeat connection and the other end 
         * is initiating a message connection, so create this end. 
         *  
         * Note that the connection set only finds out about the newly created 
         * message connection when it is informed by the call to 
         * connectionSet.useNewMessageConnection(), so it can not terminate the 
         * connection before the call to messageConnection.assignImpl(). Also, 
         * we created the message connection, so we know it does not yet have an 
         * impl. Hence we can assume it will succeed in assigning the impl. 
         */ 
        messageConnection.set(new MessageConnection(me, connectionSet,
                                                    hbcon.getProtocol(),
                                                    hbcon.getCandidate()));
        if (!messageConnection.get().assignImpl(this)) {
            log.error(format("Failed to assign incoming connection on heartbeat: %s",
                             messageConnection));
            silent(); 
            shutdown(); 
        } 
        messageConnection.get().deliver(bytes);
 
        /** 
         * if the call to connectionSet.useNewMessageConnection() then a 
         * connection has been created since we checked for it above with 
         * connectionSet.getConnection(). The other end will not make two 
         * connection attempts at the same time, but if this thread is delayed 
         * during the last 20 lines of code for long enough for the following to 
         * happen: 1. other end time out connection + 2. quiesence period + 3. 
         * this end rediscover other end in multicast heartbeats + 4. other end 
         * initiates new connection attempt + 5. new connection attempt gets 
         * accepted (new thread created for it) + 6. read first heartbeat and 
         * get through this code in the new thread. Then it could beat this 
         * thread to it. If all this happens (and based on the premise 
         * "if it can happen it will happen") then this thread should rightly 
         * comit suicide in disgust!!!! 
         */ 
        if (!connectionSet.useNewMessageConnection(messageConnection.get())) {
            if (log.isInfoEnabled()) { 
                log.info(format("%s Concurrent creation of message connections from %s",
                                me, messageConnection.get().getSender()));
            } 
            shutdown(); 
            return; 
        } 
    }