Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
--
2
-- Licensed to the Apache Software Foundation (ASF) under one
3
-- or more contributor license agreements. See the NOTICE file
4
-- distributed with this work for additional information
5
-- regarding copyright ownership. The ASF licenses this file
6
-- to you under the Apache License, Version 2.0 (the
7
-- "License"); you may not use this file except in compliance
8
-- with the License. You may obtain a copy of the License at
9
--
10
--   http://www.apache.org/licenses/LICENSE-2.0
11
--
12
-- Unless required by applicable law or agreed to in writing,
13
-- software distributed under the License is distributed on an
14
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
-- KIND, either express or implied. See the License for the
16
-- specific language governing permissions and limitations
17
-- under the License.
18
--
19
 
20
module Thrift
21
    ( module Thrift.Transport
22
    , module Thrift.Protocol
23
    , AppExnType(..)
24
    , AppExn(..)
25
    , readAppExn
26
    , writeAppExn
27
    , ThriftException(..)
28
    ) where
29
 
30
import Control.Monad ( when )
31
import Control.Exception
32
 
33
import Data.Typeable ( Typeable )
34
 
35
import Thrift.Transport
36
import Thrift.Protocol
37
 
38
 
39
data ThriftException = ThriftException
40
  deriving ( Show, Typeable )
41
instance Exception ThriftException
42
 
43
data AppExnType
44
    = AE_UNKNOWN
45
    | AE_UNKNOWN_METHOD
46
    | AE_INVALID_MESSAGE_TYPE
47
    | AE_WRONG_METHOD_NAME
48
    | AE_BAD_SEQUENCE_ID
49
    | AE_MISSING_RESULT
50
      deriving ( Eq, Show, Typeable )
51
 
52
instance Enum AppExnType where
53
    toEnum 0 = AE_UNKNOWN
54
    toEnum 1 = AE_UNKNOWN_METHOD
55
    toEnum 2 = AE_INVALID_MESSAGE_TYPE
56
    toEnum 3 = AE_WRONG_METHOD_NAME
57
    toEnum 4 = AE_BAD_SEQUENCE_ID
58
    toEnum 5 = AE_MISSING_RESULT
59
 
60
    fromEnum AE_UNKNOWN = 0
61
    fromEnum AE_UNKNOWN_METHOD = 1
62
    fromEnum AE_INVALID_MESSAGE_TYPE = 2
63
    fromEnum AE_WRONG_METHOD_NAME = 3
64
    fromEnum AE_BAD_SEQUENCE_ID = 4
65
    fromEnum AE_MISSING_RESULT = 5
66
 
67
data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String }
68
  deriving ( Show, Typeable )
69
instance Exception AppExn
70
 
71
writeAppExn :: (Protocol p, Transport t) => p t -> AppExn -> IO ()
72
writeAppExn pt ae = do
73
    writeStructBegin pt "TApplicationException"
74
 
75
    when (ae_message ae /= "") $ do
76
        writeFieldBegin pt ("message", T_STRING , 1)
77
        writeString pt (ae_message ae)
78
        writeFieldEnd pt
79
 
80
    writeFieldBegin pt ("type", T_I32, 2);
81
    writeI32 pt (fromEnum (ae_type ae))
82
    writeFieldEnd pt
83
    writeFieldStop pt
84
    writeStructEnd pt
85
 
86
readAppExn :: (Protocol p, Transport t) => p t -> IO AppExn
87
readAppExn pt = do
88
    readStructBegin pt
89
    rec <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined})
90
    readStructEnd pt
91
    return rec
92
 
93
readAppExnFields pt rec = do
94
    (n, ft, id) <- readFieldBegin pt
95
    if ft == T_STOP
96
        then return rec
97
        else case id of
98
                 1 -> if ft == T_STRING then
99
                          do s <- readString pt
100
                             readAppExnFields pt rec{ae_message = s}
101
                          else do skip pt ft
102
                                  readAppExnFields pt rec
103
                 2 -> if ft == T_I32 then
104
                          do i <- readI32 pt
105
                             readAppExnFields pt rec{ae_type = (toEnum  i)}
106
                          else do skip pt ft
107
                                  readAppExnFields pt rec
108
                 _ -> do skip pt ft
109
                         readFieldEnd pt
110
                         readAppExnFields pt rec
111