Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed
---- Licensed to the Apache Software Foundation (ASF) under one-- or more contributor license agreements. See the NOTICE file-- distributed with this work for additional information-- regarding copyright ownership. The ASF licenses this file-- to you under the Apache License, Version 2.0 (the-- "License"); you may not use this file except in compliance-- with the License. You may obtain a copy of the License at---- http://www.apache.org/licenses/LICENSE-2.0---- Unless required by applicable law or agreed to in writing,-- software distributed under the License is distributed on an-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY-- KIND, either express or implied. See the License for the-- specific language governing permissions and limitations-- under the License.--module Thrift.Protocol( Protocol(..), skip, MessageType(..), ThriftType(..), ProtocolExn(..), ProtocolExnType(..)) whereimport Control.Monad ( replicateM_, unless )import Control.Exceptionimport Data.Typeable ( Typeable )import Data.Intimport Thrift.Transportdata ThriftType= T_STOP| T_VOID| T_BOOL| T_BYTE| T_DOUBLE| T_I16| T_I32| T_I64| T_STRING| T_STRUCT| T_MAP| T_SET| T_LISTderiving ( Eq )instance Enum ThriftType wherefromEnum T_STOP = 0fromEnum T_VOID = 1fromEnum T_BOOL = 2fromEnum T_BYTE = 3fromEnum T_DOUBLE = 4fromEnum T_I16 = 6fromEnum T_I32 = 8fromEnum T_I64 = 10fromEnum T_STRING = 11fromEnum T_STRUCT = 12fromEnum T_MAP = 13fromEnum T_SET = 14fromEnum T_LIST = 15toEnum 0 = T_STOPtoEnum 1 = T_VOIDtoEnum 2 = T_BOOLtoEnum 3 = T_BYTEtoEnum 4 = T_DOUBLEtoEnum 6 = T_I16toEnum 8 = T_I32toEnum 10 = T_I64toEnum 11 = T_STRINGtoEnum 12 = T_STRUCTtoEnum 13 = T_MAPtoEnum 14 = T_SETtoEnum 15 = T_LISTdata MessageType= M_CALL| M_REPLY| M_EXCEPTIONderiving ( Eq )instance Enum MessageType wherefromEnum M_CALL = 1fromEnum M_REPLY = 2fromEnum M_EXCEPTION = 3toEnum 1 = M_CALLtoEnum 2 = M_REPLYtoEnum 3 = M_EXCEPTIONclass Protocol a wheregetTransport :: Transport t => a t -> twriteMessageBegin :: Transport t => a t -> (String, MessageType, Int) -> IO ()writeMessageEnd :: Transport t => a t -> IO ()writeStructBegin :: Transport t => a t -> String -> IO ()writeStructEnd :: Transport t => a t -> IO ()writeFieldBegin :: Transport t => a t -> (String, ThriftType, Int) -> IO ()writeFieldEnd :: Transport t => a t -> IO ()writeFieldStop :: Transport t => a t -> IO ()writeMapBegin :: Transport t => a t -> (ThriftType, ThriftType, Int) -> IO ()writeMapEnd :: Transport t => a t -> IO ()writeListBegin :: Transport t => a t -> (ThriftType, Int) -> IO ()writeListEnd :: Transport t => a t -> IO ()writeSetBegin :: Transport t => a t -> (ThriftType, Int) -> IO ()writeSetEnd :: Transport t => a t -> IO ()writeBool :: Transport t => a t -> Bool -> IO ()writeByte :: Transport t => a t -> Int -> IO ()writeI16 :: Transport t => a t -> Int -> IO ()writeI32 :: Transport t => a t -> Int -> IO ()writeI64 :: Transport t => a t -> Int64 -> IO ()writeDouble :: Transport t => a t -> Double -> IO ()writeString :: Transport t => a t -> String -> IO ()writeBinary :: Transport t => a t -> String -> IO ()readMessageBegin :: Transport t => a t -> IO (String, MessageType, Int)readMessageEnd :: Transport t => a t -> IO ()readStructBegin :: Transport t => a t -> IO StringreadStructEnd :: Transport t => a t -> IO ()readFieldBegin :: Transport t => a t -> IO (String, ThriftType, Int)readFieldEnd :: Transport t => a t -> IO ()readMapBegin :: Transport t => a t -> IO (ThriftType, ThriftType, Int)readMapEnd :: Transport t => a t -> IO ()readListBegin :: Transport t => a t -> IO (ThriftType, Int)readListEnd :: Transport t => a t -> IO ()readSetBegin :: Transport t => a t -> IO (ThriftType, Int)readSetEnd :: Transport t => a t -> IO ()readBool :: Transport t => a t -> IO BoolreadByte :: Transport t => a t -> IO IntreadI16 :: Transport t => a t -> IO IntreadI32 :: Transport t => a t -> IO IntreadI64 :: Transport t => a t -> IO Int64readDouble :: Transport t => a t -> IO DoublereadString :: Transport t => a t -> IO StringreadBinary :: Transport t => a t -> IO Stringskip :: (Protocol p, Transport t) => p t -> ThriftType -> IO ()skip p T_STOP = return ()skip p T_VOID = return ()skip p T_BOOL = readBool p >> return ()skip p T_BYTE = readByte p >> return ()skip p T_I16 = readI16 p >> return ()skip p T_I32 = readI32 p >> return ()skip p T_I64 = readI64 p >> return ()skip p T_DOUBLE = readDouble p >> return ()skip p T_STRING = readString p >> return ()skip p T_STRUCT = do readStructBegin pskipFields preadStructEnd pskip p T_MAP = do (k, v, s) <- readMapBegin preplicateM_ s (skip p k >> skip p v)readMapEnd pskip p T_SET = do (t, n) <- readSetBegin preplicateM_ n (skip p t)readSetEnd pskip p T_LIST = do (t, n) <- readListBegin preplicateM_ n (skip p t)readListEnd pskipFields :: (Protocol p, Transport t) => p t -> IO ()skipFields p = do(_, t, _) <- readFieldBegin punless (t == T_STOP) (skip p t >> readFieldEnd p >> skipFields p)data ProtocolExnType= PE_UNKNOWN| PE_INVALID_DATA| PE_NEGATIVE_SIZE| PE_SIZE_LIMIT| PE_BAD_VERSIONderiving ( Eq, Show, Typeable )data ProtocolExn = ProtocolExn ProtocolExnType Stringderiving ( Show, Typeable )instance Exception ProtocolExn