ConnectionDelegate.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.transport;
022 
023 import org.apache.qpid.transport.util.Logger;
024 
025 import static org.apache.qpid.transport.Connection.State.*;
026 
027 
028 /**
029  * ConnectionDelegate
030  *
031  @author Rafael H. Schloming
032  */
033 
034 /**
035  * Currently only implemented client specific methods
036  * the server specific methods are dummy impls for testing
037  *
038  * the connectionClose is kind of different for both sides
039  */
040 public abstract class ConnectionDelegate
041     extends MethodDelegate<Connection>
042     implements ProtocolDelegate<Connection>
043 {
044 
045     private static final Logger log = Logger.get(ConnectionDelegate.class);
046 
047     public void control(Connection conn, Method method)
048     {
049         method.dispatch(conn, this);
050     }
051 
052     public void command(Connection conn, Method method)
053     {
054         method.dispatch(conn, this);
055     }
056 
057     public void error(Connection conn, ProtocolError error)
058     {
059         conn.exception(new ConnectionException(error.getMessage()));
060     }
061 
062     public void handle(Connection conn, Method method)
063     {
064         conn.dispatch(method);
065     }
066 
067     @Override public void connectionHeartbeat(Connection conn, ConnectionHeartbeat hearbeat)
068     {
069         // do nothing
070     }
071 
072     @Override public void connectionClose(Connection conn, ConnectionClose close)
073     {
074         conn.connectionCloseOk();
075         conn.getSender().close();
076         conn.closeCode(close);
077         conn.setState(CLOSE_RCVD);
078     }
079 
080     @Override public void connectionCloseOk(Connection conn, ConnectionCloseOk ok)
081     {
082         conn.getSender().close();
083     }
084 
085     @Override public void sessionDetach(Connection conn, SessionDetach dtc)
086     {
087         Session ssn = conn.getSession(dtc.getChannel());
088         conn.unmap(ssn);
089         ssn.sessionDetached(dtc.getName(), SessionDetachCode.NORMAL);
090         ssn.closed();
091     }
092 
093     @Override public void sessionDetached(Connection conn, SessionDetached dtc)
094     {
095         Session ssn = conn.getSession(dtc.getChannel());
096         if (ssn != null)
097         {
098             conn.unmap(ssn);
099             ssn.closed();
100         }
101     }
102 
103 }