geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject cvs commit: incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control State.java ControlClientProtocol.java ControlServerProtocol.java
Date Sat, 24 Apr 2004 22:34:02 GMT
adc         2004/04/24 15:34:02

  Modified:    modules/network/src/java/org/apache/geronimo/network/protocol/control
                        ControlClientProtocol.java
                        ControlServerProtocol.java
  Added:       modules/network/src/java/org/apache/geronimo/network/protocol/control
                        State.java
  Log:
  Removed most locks.  Removed reliance on startup dependency.
  
  Revision  Changes    Path
  1.7       +69 -73    incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control/ControlClientProtocol.java
  
  Index: ControlClientProtocol.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control/ControlClientProtocol.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ControlClientProtocol.java	24 Apr 2004 06:29:01 -0000	1.6
  +++ ControlClientProtocol.java	24 Apr 2004 22:34:01 -0000	1.7
  @@ -17,15 +17,14 @@
   
   package org.apache.geronimo.network.protocol.control;
   
  +import EDU.oswego.cs.dl.util.concurrent.Latch;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +
   import org.apache.geronimo.network.protocol.DownPacket;
  -import org.apache.geronimo.network.protocol.Protocol;
   import org.apache.geronimo.network.protocol.ProtocolException;
   import org.apache.geronimo.network.protocol.UpPacket;
   
  -import EDU.oswego.cs.dl.util.concurrent.Latch;
  -
   
   /**
    * @version $Revision$ $Date$
  @@ -35,25 +34,9 @@
       final static private Log log = LogFactory.getLog(ControlClientProtocol.class);
   
       private ControlClientListener listener;
  -    private ClassLoader classLoader;
  -    private Latch sendLatch = new Latch();  //todo: replace with something that uses no
locks
  -    private Latch shutdownLatch = new Latch();
       private long timeout;
   
  -    private final int STARTED = 0;
  -    private final int STOPPED = 1;
  -    private int state = STOPPED;
  -
  -    /**
  -	 * @see org.apache.geronimo.network.protocol.AbstractProtocol#cloneProtocol()
  -	 */
  -	public Protocol cloneProtocol() throws CloneNotSupportedException {
  -		ControlClientProtocol p = (ControlClientProtocol)super.cloneProtocol();
  -		p.sendLatch = new Latch();
  -		p.shutdownLatch = new Latch();
  -		return p;
  -	}
  -    
  +
       public ControlClientListener getListener() {
           return listener;
       }
  @@ -62,14 +45,6 @@
           this.listener = listener;
       }
   
  -    public ClassLoader getClassLoader() {
  -        return classLoader;
  -    }
  -
  -    public void setClassLoader(ClassLoader classLoader) {
  -        this.classLoader = classLoader;
  -    }
  -
       public long getTimeout() {
           return timeout;
       }
  @@ -80,20 +55,14 @@
   
       public void setup() throws ProtocolException {
           log.trace("Starting");
  +
           getDownProtocol().sendDown(new BootRequestDownPacket()); //todo: this is probably
dangerous, put in thread pool
  -        state = STARTED;
       }
   
       public void drain() throws ProtocolException {
           log.trace("Stopping");
  -        if (state == STARTED) {
  +        if (state == RUN) {
               getDownProtocol().sendDown(new ShutdownRequestDownPacket());
  -            try {
  -                shutdownLatch.acquire();
  -            } catch (InterruptedException e) {
  -                throw new ProtocolException(e);
  -            }
  -            state = STOPPED;
           }
       }
   
  @@ -101,51 +70,78 @@
       }
   
       public void sendUp(UpPacket packet) throws ProtocolException {
  -        UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  -        if (p instanceof PassthroughUpPacket) {
  -            log.trace("PASSTHROUGH");
  -            getUpProtocol().sendUp(packet);
  -        } else if (p instanceof BootResponseUpPacket) {
  +        state.sendUp(packet);
  +    }
  +
  +    public void sendDown(DownPacket packet) throws ProtocolException {
  +        state.sendDown(packet);
  +    }
  +
  +    private final State START = new State(this) {
  +        Latch startupLatch = new Latch();
  +
  +        public void sendUp(UpPacket packet) throws ProtocolException {
  +            UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  +            if (p instanceof BootResponseUpPacket) {
  +                try {
  +                    log.trace("BOOT RESPONSE");
  +                    listener.serveUp(((BootResponseUpPacket) p).getMenu());
  +                    getDownProtocol().sendDown(new BootSuccessDownPacket());
  +                    log.trace("RELEASING " + startupLatch);
  +                    state = RUN;
  +                    startupLatch.release();
  +                    log.trace("RELEASED " + startupLatch);
  +                } catch (ControlException e) {
  +                    throw new ProtocolException(e);
  +                }
  +            } else if (p instanceof NoBootUpPacket) {
  +                log.trace("NO BOOT");
  +                listener.shutdown();
  +            }
  +        }
  +
  +        public void sendDown(DownPacket packet) throws ProtocolException {
               try {
  -                log.trace("BOOT RESPONSE");
  -                listener.serveUp(((BootResponseUpPacket) p).getMenu());
  -                getDownProtocol().sendDown(new BootSuccessDownPacket());
  -                log.trace("RELEASING " + sendLatch);
  -                sendLatch.release();
  -                log.trace("RELEASED " + sendLatch);
  -            } catch (ControlException e) {
  +                log.trace("AQUIRING " + startupLatch);
  +                if (!startupLatch.attempt(timeout)) throw new ProtocolException("Send timeout");
  +                log.trace("AQUIRED " + startupLatch);
  +
  +                PassthroughDownPacket passthtough = new PassthroughDownPacket();
  +                passthtough.setBuffers(packet.getBuffers());
  +
  +                getDownProtocol().sendDown(passthtough);
  +            } catch (InterruptedException e) {
                   throw new ProtocolException(e);
               }
  -        } else if (p instanceof NoBootUpPacket) {
  -            log.trace("NO BOOT");
  -            state = STOPPED;
  -            listener.shutdown();
  -        } else if (p instanceof ShutdownRequestUpPacket) {
  -            log.trace("SHUTDOWN_REQ");
  -            getDownProtocol().sendDown(new ShutdownAcknowledgeDownPacket());
  -            state = STOPPED;
  -            listener.shutdown();
  -        } else if (p instanceof ShutdownAcknowledgeUpPacket) {
  -            log.trace("SHUTDOWN_ACK");
  -            shutdownLatch.release();
  -            state = STOPPED;
  -            listener.shutdown();
           }
  -    }
  +    };
   
  -    public void sendDown(DownPacket packet) throws ProtocolException {
  -        try {
  -            log.trace("AQUIRING " + sendLatch);
  -            if (!sendLatch.attempt(timeout)) throw new ProtocolException("Send timeout");
  -            log.trace("AQUIRED " + sendLatch);
  +    private final State RUN = new State(this) {
  +
  +        public void sendUp(UpPacket packet) throws ProtocolException {
  +            UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  +            if (p instanceof PassthroughUpPacket) {
  +                log.trace("PASSTHROUGH");
  +                getUpProtocol().sendUp(packet);
  +            } else if (p instanceof ShutdownRequestUpPacket) {
  +                log.trace("SHUTDOWN_REQ");
  +                getDownProtocol().sendDown(new ShutdownAcknowledgeDownPacket());
  +                listener.shutdown();
  +                state = START;
  +            } else if (p instanceof ShutdownAcknowledgeUpPacket) {
  +                log.trace("SHUTDOWN_ACK");
  +                listener.shutdown();
  +                state = START;
  +            }
  +        }
   
  +        public void sendDown(DownPacket packet) throws ProtocolException {
               PassthroughDownPacket passthtough = new PassthroughDownPacket();
               passthtough.setBuffers(packet.getBuffers());
   
               getDownProtocol().sendDown(passthtough);
  -
  -        } catch (InterruptedException e) {
  -            throw new ProtocolException(e);
           }
  -    }
  +    };
  +
  +    private volatile State state = START;
   }
  
  
  
  1.6       +80 -43    incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control/ControlServerProtocol.java
  
  Index: ControlServerProtocol.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control/ControlServerProtocol.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ControlServerProtocol.java	24 Apr 2004 06:29:01 -0000	1.5
  +++ ControlServerProtocol.java	24 Apr 2004 22:34:01 -0000	1.6
  @@ -19,17 +19,18 @@
   
   import java.util.Collection;
   
  +import EDU.oswego.cs.dl.util.concurrent.Latch;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +
   import org.apache.geronimo.network.SelectorManager;
   import org.apache.geronimo.network.protocol.DownPacket;
  +import org.apache.geronimo.network.protocol.Protocol;
   import org.apache.geronimo.network.protocol.ProtocolException;
   import org.apache.geronimo.network.protocol.UpPacket;
   import org.apache.geronimo.system.ClockPool;
   import org.apache.geronimo.system.ThreadPool;
   
  -import EDU.oswego.cs.dl.util.concurrent.Latch;
  -
   
   /**
    * @version $Revision$ $Date$
  @@ -43,13 +44,8 @@
       private ThreadPool threadPool;
       private ClockPool clockPool;
       private SelectorManager selectorManager;
  -    private Latch sendLatch;  //todo: replace with something that uses no locks
       private long timeout;
   
  -    private final int STARTED = 0;
  -    private final int STOPPED = 1;
  -    private int state = STOPPED;
  -
       public ControlServerListener getControlServerListener() {
           return controlServerListener;
       }
  @@ -98,17 +94,22 @@
           this.timeout = timeout;
       }
   
  +    public Protocol cloneProtocol() throws CloneNotSupportedException {
  +        ControlServerProtocol result = (ControlServerProtocol) super.clone();
  +        result.START.setParent(result);
  +        result.RUN.setParent(result);
  +        return result;
  +    }
  +
       public void setup() throws ProtocolException {
           log.trace("Starting");
  -        sendLatch = new Latch();
  -        state = STARTED;
       }
   
       public void drain() throws ProtocolException {
           log.trace("Stopping");
  -        if (state == STARTED) {
  +
  +        if (getState() == RUN) {
               getDownProtocol().sendDown(new ShutdownRequestDownPacket());
  -            state = STOPPED;
           }
       }
   
  @@ -116,40 +117,11 @@
       }
   
       public void sendUp(UpPacket packet) throws ProtocolException {
  -        UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  -        if (p instanceof PassthroughUpPacket) {
  -            log.trace("PASSTHROUGH");
  -            getUpProtocol().sendUp(packet);
  -        } else if (p instanceof BootRequestUpPacket) {
  -            log.trace("BOOT REQUEST");
  -            getDownProtocol().sendDown(constructBootPacket());
  -        } else if (p instanceof BootSuccessUpPacket) {
  -            log.trace("BOOT SUCCESS");
  -            log.trace("RELEASING " + sendLatch);
  -            sendLatch.release();
  -            log.trace("RELEASED " + sendLatch);
  -        } else if (p instanceof ShutdownRequestUpPacket) {
  -            log.trace("SHUTDOWN_REQ");
  -            getDownProtocol().sendDown(new ShutdownAcknowledgeDownPacket());
  -            state = STOPPED;
  -            controlServerListener.shutdown();
  -        }
  +        getState().sendUp(packet);
       }
   
       public void sendDown(DownPacket packet) throws ProtocolException {
  -        try {
  -            log.trace("AQUIRING " + sendLatch);
  -            if (!sendLatch.attempt(timeout)) throw new ProtocolException("Send timeout.");
  -            log.trace("AQUIRED " + sendLatch);
  -
  -            PassthroughDownPacket passthtough = new PassthroughDownPacket();
  -            passthtough.setBuffers(packet.getBuffers());
  -
  -            getDownProtocol().sendDown(passthtough);
  -
  -        } catch (InterruptedException e) {
  -            throw new ProtocolException(e);
  -        }
  +        getState().sendDown(packet);
       }
   
       protected DownPacket constructBootPacket() {
  @@ -170,4 +142,69 @@
           }
       }
   
  +    private final State START = new State(this) {
  +        Latch startupLatch = new Latch();
  +
  +        public void sendUp(UpPacket packet) throws ProtocolException {
  +            UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  +            if (p instanceof BootRequestUpPacket) {
  +                log.trace("BOOT REQUEST");
  +                getDownProtocol().sendDown(constructBootPacket());
  +            } else if (p instanceof BootSuccessUpPacket) {
  +                log.trace("BOOT SUCCESS");
  +                log.trace("RELEASING " + startupLatch);
  +                ((ControlServerProtocol)getParent()).setState(RUN);
  +                startupLatch.release();
  +                log.trace("RELEASED " + startupLatch);
  +            }
  +        }
  +
  +        public void sendDown(DownPacket packet) throws ProtocolException {
  +            try {
  +                log.trace("AQUIRING " + startupLatch);
  +                if (!startupLatch.attempt(timeout)) throw new ProtocolException("Send timeout");
  +                log.trace("AQUIRED " + startupLatch);
  +
  +                PassthroughDownPacket passthtough = new PassthroughDownPacket();
  +                passthtough.setBuffers(packet.getBuffers());
  +
  +                getDownProtocol().sendDown(passthtough);
  +            } catch (InterruptedException e) {
  +                throw new ProtocolException(e);
  +            }
  +        }
  +    };
  +
  +    private final State RUN = new State(this) {
  +
  +        public void sendUp(UpPacket packet) throws ProtocolException {
  +            UpPacket p = ControlPacketReader.getInstance().read(packet.getBuffer());
  +            if (p instanceof PassthroughUpPacket) {
  +                log.trace("PASSTHROUGH");
  +                getUpProtocol().sendUp(packet);
  +            } else if (p instanceof ShutdownRequestUpPacket) {
  +                log.trace("SHUTDOWN_REQ");
  +                getDownProtocol().sendDown(new ShutdownAcknowledgeDownPacket());
  +                ((ControlServerProtocol)getParent()).setState(START);
  +                controlServerListener.shutdown();
  +            }
  +        }
  +
  +        public void sendDown(DownPacket packet) throws ProtocolException {
  +            PassthroughDownPacket passthtough = new PassthroughDownPacket();
  +            passthtough.setBuffers(packet.getBuffers());
  +
  +            getDownProtocol().sendDown(passthtough);
  +        }
  +    };
  +
  +    public State getState() {
  +        return state;
  +    }
  +
  +    public void setState(State state) {
  +        this.state = state;
  +    }
  +
  +    private volatile State state = START;
   }
  
  
  
  1.1                  incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/control/State.java
  
  Index: State.java
  ===================================================================
  /**
   *
   * Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.geronimo.network.protocol.control;
  
  import org.apache.geronimo.network.protocol.DownPacket;
  import org.apache.geronimo.network.protocol.Protocol;
  import org.apache.geronimo.network.protocol.ProtocolException;
  import org.apache.geronimo.network.protocol.UpPacket;
  
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/04/24 22:34:01 $
   */
  public abstract class State {
  
      private Protocol parent;
  
      protected State(Protocol parent) {
          this.parent = parent;
      }
  
      public Protocol getParent() {
          return parent;
      }
  
      public void setParent(Protocol parent) {
          this.parent = parent;
      }
  
      protected Protocol getUpProtocol() {
          return getParent().getUpProtocol();
      }
  
      protected Protocol getDownProtocol() {
          return getParent().getDownProtocol();
      }
  
      public abstract void sendUp(UpPacket packet) throws ProtocolException;
  
      public abstract void sendDown(DownPacket packet) throws ProtocolException;
  }
  
  
  

Mime
View raw message