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.nio.channels.ReadableByteChannel;
022    import java.nio.channels.WritableByteChannel;
023    
024    
025    /**
026     * Interface to encode and decode commands in and out of a a non blocking channel.
027     *
028     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
029     */
030    public interface ProtocolCodec {
031    
032        ///////////////////////////////////////////////////////////////////
033        //
034        // Methods related with reading from the channel
035        //
036        ///////////////////////////////////////////////////////////////////
037    
038        /**
039         * @param channel
040         */
041        public void setReadableByteChannel(ReadableByteChannel channel) throws Exception;
042    
043        /**
044         * Non-blocking channel based decoding.
045         * 
046         * @return
047         * @throws IOException
048         */
049        Object read() throws IOException;
050    
051        /**
052         * Pushes back a buffer as being unread.
053         *
054         * @param buffer
055         */
056        void unread(byte[] buffer);
057    
058        /**
059         * @return The number of bytes received.
060         */
061        public long getReadCounter();
062    
063        /**
064         * @return The number of bytes read in the last read io performed.
065         */
066        public long getLastReadSize();
067    
068    
069        ///////////////////////////////////////////////////////////////////
070        //
071        // Methods related with writing to the channel
072        //
073        ///////////////////////////////////////////////////////////////////
074    
075    
076        enum BufferState {
077            EMPTY,
078            WAS_EMPTY,
079            NOT_EMPTY,
080            FULL,
081        }
082    
083        public void setWritableByteChannel(WritableByteChannel channel) throws Exception;
084    
085        public int getReadBufferSize();
086        public int getWriteBufferSize();
087    
088        /**
089         * Non-blocking channel based encoding.
090         *
091         * @return true if the write completed.
092         * @throws IOException
093         */
094        BufferState write(Object value) throws IOException;
095    
096        /**
097         * Attempts to complete the previous write which did not complete.
098         * @return
099         * @throws IOException
100         */
101        BufferState flush() throws IOException;
102    
103        /**
104         * Is the codec's buffer full?
105         * @return
106         */
107        boolean full();
108    
109        /**
110         * @return The number of bytes written.
111         */
112        public long getWriteCounter();
113    
114        /**
115         * @return The number of bytes read in the last write io performed.
116         */
117        public long getLastWriteSize();
118    
119    
120    }