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
026 /**
027 * Represents an abstract connection. It can be a client side or server side connection.
028 *
029 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
030 */
031 public interface Transport {
032
033 /**
034 * Starts the service. Executes the onComplete runnable once the service has fully started up.
035 *
036 * @param onComplete my be set to null if not interested in a callback.
037 */
038 void start(Runnable onComplete);
039
040 /**
041 * Stops the service. Executes the onComplete runnable once the service has fully stopped.
042 *
043 * @param onComplete my be set to null if not interested in a callback.
044 */
045 void stop(Runnable onComplete);
046
047 boolean full();
048
049 /**
050 * A one way asynchronous send of a command. Only sent if the the transport is not full.
051 *
052 * @param command
053 * @return true if the command was accepted.
054 */
055 boolean offer(Object command);
056
057 /**
058 * Forces a flush of any output buffers. Once the flush completes the listener's
059 * 'onRefill()' method will execute.
060 */
061 public void flush();
062
063 /**
064 * Returns the current transport listener
065 *
066 * @return
067 */
068 TransportListener getTransportListener();
069
070 /**
071 * Registers an inbound command listener
072 *
073 * @param transportListener
074 */
075 void setTransportListener(TransportListener transportListener);
076
077 /**
078 * Returns the dispatch queue used by the transport
079 *
080 * @return
081 */
082 DispatchQueue getDispatchQueue();
083
084 /**
085 * Sets the dispatch queue used by the transport
086 *
087 * @param queue
088 */
089 void setDispatchQueue(DispatchQueue queue);
090
091 /**
092 * suspend delivery of commands.
093 */
094 void suspendRead();
095
096 /**
097 * resume delivery of commands.
098 */
099 void resumeRead();
100
101 /**
102 * @return the remote address for this connection
103 */
104 SocketAddress getRemoteAddress();
105
106 /**
107 * @return the remote address for this connection
108 */
109 SocketAddress getLocalAddress();
110
111 /**
112 * @return true if the transport is closed/stopped.
113 */
114 boolean isClosed();
115
116 /**
117 * @return true if the transport is connected
118 */
119 boolean isConnected();
120
121 /**
122 * @return The protocol codec for the transport.
123 */
124 ProtocolCodec getProtocolCodec();
125
126 /**
127 * Sets the protocol codec for the transport
128 * @param protocolCodec
129 */
130 void setProtocolCodec(ProtocolCodec protocolCodec) throws Exception;
131
132 }