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