Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | 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_base64_transport).
21
 
22
-behaviour(thrift_transport).
23
 
24
%% API
25
-export([new/1, new_transport_factory/1]).
26
 
27
%% thrift_transport callbacks
28
-export([write/2, read/2, flush/1, close/1]).
29
 
30
%% State
31
-record(b64_transport, {wrapped}).
32
 
33
new(Wrapped) ->
34
    State = #b64_transport{wrapped = Wrapped},
35
    thrift_transport:new(?MODULE, State).
36
 
37
 
38
write(#b64_transport{wrapped = Wrapped}, Data) ->
39
    thrift_transport:write(Wrapped, base64:encode(iolist_to_binary(Data))).
40
 
41
 
42
%% base64 doesn't support reading quite yet since it would involve
43
%% nasty buffering and such
44
read(#b64_transport{wrapped = Wrapped}, Data) ->
45
    {error, no_reads_allowed}.
46
 
47
 
48
flush(#b64_transport{wrapped = Wrapped}) ->
49
    thrift_transport:write(Wrapped, <<"\n">>),
50
    thrift_transport:flush(Wrapped).
51
 
52
 
53
close(Me = #b64_transport{wrapped = Wrapped}) ->
54
    flush(Me),
55
    thrift_transport:close(Wrapped).
56
 
57
 
58
%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
new_transport_factory(WrapFactory) ->
60
    F = fun() ->
61
                {ok, Wrapped} = WrapFactory(),
62
                new(Wrapped)
63
        end,
64
    {ok, F}.