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