001    /**
002     * Copyright (C) 2012 FuseSource, Inc.
003     * http://fusesource.com
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *    http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.fusesource.hawtdispatch.transport;
019    
020    import java.io.IOException;
021    import java.net.SocketAddress;
022    import java.net.URI;
023    
024    import org.fusesource.hawtdispatch.DispatchQueue;
025    import org.fusesource.hawtdispatch.Task;
026    
027    /**
028     * Represents an abstract connection.  It can be a client side or server side connection.
029     * 
030     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
031     */
032    public interface Transport {
033    
034        /**
035         * Starts the service.  Executes the onComplete runnable once the service has fully started up.
036         *
037         * @param onComplete my be set to null if not interested in a callback.
038         */
039        @Deprecated
040        void start(Runnable onComplete);
041    
042        /**
043         * Stops the service.  Executes the onComplete runnable once the service has fully stopped.
044         *
045         * @param onComplete my be set to null if not interested in a callback.
046         */
047        @Deprecated
048        void stop(Runnable onComplete);
049    
050        /**
051         * Starts the service.  Executes the onComplete runnable once the service has fully started up.
052         *
053         * @param onComplete my be set to null if not interested in a callback.
054         */
055        void start(Task onComplete);
056    
057        /**
058         * Stops the service.  Executes the onComplete runnable once the service has fully stopped.
059         *
060         * @param onComplete my be set to null if not interested in a callback.
061         */
062        void stop(Task onComplete);
063    
064        boolean full();
065    
066        /**
067         * A one way asynchronous send of a command.  Only sent if the the transport is not full.
068         * 
069         * @param command
070         * @return true if the command was accepted.
071         */
072        boolean offer(Object command);
073    
074        /**
075         * Forces a flush of any output buffers.  Once the flush completes the listener's
076         * 'onRefill()' method will execute.
077         */
078        public void flush();
079    
080        /**
081         * Returns the current transport listener
082         *
083         * @return
084         */
085        TransportListener getTransportListener();
086    
087        /**
088         * Registers an inbound command listener
089         *
090         * @param transportListener
091         */
092        void setTransportListener(TransportListener transportListener);
093    
094        /**
095         * Returns the dispatch queue used by the transport
096         *
097         * @return
098         */
099        DispatchQueue getDispatchQueue();
100    
101        /**
102         * Sets the dispatch queue used by the transport
103         *
104         * @param queue
105         */
106        void setDispatchQueue(DispatchQueue queue);
107    
108        /**
109         * suspend delivery of commands.
110         */
111        void suspendRead();
112    
113        /**
114         * resume delivery of commands.
115         */
116        void resumeRead();
117    
118        /**
119         * @return the remote address for this connection
120         */
121        SocketAddress getRemoteAddress();
122    
123        /**
124         * @return the remote address for this connection
125         */
126        SocketAddress getLocalAddress();
127    
128        /**
129         * @return true if the transport is closed/stopped.
130         */
131        boolean isClosed();
132        
133        /**
134         * @return true if the transport is connected
135         */
136        boolean isConnected();
137        
138        /**
139         * @return The protocol codec for the transport.
140         */
141        ProtocolCodec getProtocolCodec();
142    
143        /**
144         * Sets the protocol codec for the transport
145         * @param protocolCodec
146         */
147        void setProtocolCodec(ProtocolCodec protocolCodec) throws Exception;
148    
149    }