blob: 6f352b1ab487003d0509c39bc97339858ed5b5cb [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
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
20unit Thrift;
21
22interface
23
24uses
25 SysUtils, Thrift.Protocol;
26
27type
28 IProcessor = interface
29 ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}']
30 function Process( iprot :IProtocol; oprot: IProtocol): Boolean;
31 end;
32
33 TApplicationException = class( SysUtils.Exception )
34 public
35 type
36{$SCOPEDENUMS ON}
37 TExceptionType = (
38 Unknown,
39 UnknownMethod,
40 InvalidMessageType,
41 WrongMethodName,
42 BadSequenceID,
43 MissingResult
44 );
45{$SCOPEDENUMS OFF}
46 private
47 FType : TExceptionType;
48 public
49 constructor Create; overload;
50 constructor Create( AType: TExceptionType); overload;
51 constructor Create( AType: TExceptionType; const msg: string); overload;
52
53 class function Read( iprot: IProtocol): TApplicationException;
54 procedure Write( oprot: IProtocol );
55 end;
56
57implementation
58
59{ TApplicationException }
60
61constructor TApplicationException.Create;
62begin
63 inherited Create( '' );
64end;
65
66constructor TApplicationException.Create(AType: TExceptionType;
67 const msg: string);
68begin
69 inherited Create( msg );
70 FType := AType;
71end;
72
73constructor TApplicationException.Create(AType: TExceptionType);
74begin
75 inherited Create('');
76 FType := AType;
77end;
78
79class function TApplicationException.Read(
80 iprot: IProtocol): TApplicationException;
81var
82 field : IField;
83 msg : string;
84 typ : TExceptionType;
85begin
86 msg := '';
87 typ := TExceptionType.Unknown;
88 while ( True ) do
89 begin
90 field := iprot.ReadFieldBegin;
91 if ( field.Type_ = TType.Stop) then
92 begin
93 Break;
94 end;
95
96 case field.Id of
97 1 : begin
98 if ( field.Type_ = TType.String_) then
99 begin
100 msg := iprot.ReadString;
101 end else
102 begin
103 TProtocolUtil.Skip( iprot, field.Type_ );
104 end;
105 end;
106
107 2 : begin
108 if ( field.Type_ = TType.I32) then
109 begin
110 typ := TExceptionType( iprot.ReadI32 );
111 end else
112 begin
113 TProtocolUtil.Skip( iprot, field.Type_ );
114 end;
115 end else
116 begin
117 TProtocolUtil.Skip( iprot, field.Type_);
118 end;
119 end;
120 iprot.ReadFieldEnd;
121 end;
122 iprot.ReadStructEnd;
123 Result := TApplicationException.Create( typ, msg );
124end;
125
126procedure TApplicationException.Write(oprot: IProtocol);
127var
128 struc : IStruct;
129 field : IField;
130
131begin
132 struc := TStructImpl.Create( 'TApplicationException' );
133 field := TFieldImpl.Create;
134
135 oprot.WriteStructBegin( struc );
136 if Message <> '' then
137 begin
138 field.Name := 'message';
139 field.Type_ := TType.String_;
140 field.Id := 1;
141 oprot.WriteFieldBegin( field );
142 oprot.WriteString( Message );
143 oprot.WriteFieldEnd;
144 end;
145
146 field.Name := 'type';
147 field.Type_ := TType.I32;
148 field.Id := 2;
149 oprot.WriteFieldBegin(field);
150 oprot.WriteI32(Integer(FType));
151 oprot.WriteFieldEnd();
152 oprot.WriteFieldStop();
153 oprot.WriteStructEnd();
154end;
155
156end.