| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 |  | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 20 | -module(thrift_transport). | 
 | 21 |  | 
| David Reiss | 3b9c342 | 2008-06-11 00:59:19 +0000 | [diff] [blame] | 22 | -export([behaviour_info/1]). | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 23 |  | 
| David Reiss | 3b9c342 | 2008-06-11 00:59:19 +0000 | [diff] [blame] | 24 | -export([new/2, | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 25 |          write/2, | 
| David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 26 |          read/2, | 
| David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 27 |          flush/1, | 
 | 28 |          close/1 | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 29 |         ]). | 
 | 30 |  | 
 | 31 | behaviour_info(callbacks) -> | 
| David Reiss | 3b9c342 | 2008-06-11 00:59:19 +0000 | [diff] [blame] | 32 |     [{read, 2}, | 
 | 33 |      {write, 2}, | 
| David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 34 |      {flush, 1}, | 
 | 35 |      {close, 1} | 
| David Reiss | 3b9c342 | 2008-06-11 00:59:19 +0000 | [diff] [blame] | 36 |     ]. | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 37 |  | 
| David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 38 | -record(transport, {module, data}). | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 39 |  | 
| David Reiss | 9b170eb | 2010-08-30 22:05:44 +0000 | [diff] [blame] | 40 | -ifdef(transport_wrapper_module). | 
 | 41 | -define(debug_wrap(Transport), | 
 | 42 |         case Transport#transport.module of | 
 | 43 |             ?transport_wrapper_module -> | 
 | 44 |                 Transport; | 
 | 45 |             _Else -> | 
 | 46 |                 {ok, Result} = ?transport_wrapper_module:new(Transport), | 
 | 47 |                 Result | 
 | 48 |         end). | 
 | 49 | -else. | 
 | 50 | -define(debug_wrap(Transport), Transport). | 
 | 51 | -endif. | 
 | 52 |  | 
| Bryan Duxbury | d3879f8 | 2010-08-19 05:06:02 +0000 | [diff] [blame] | 53 | new(Module, Data) when is_atom(Module) -> | 
| David Reiss | 9b170eb | 2010-08-30 22:05:44 +0000 | [diff] [blame] | 54 |     Transport0 = #transport{module = Module, data = Data}, | 
 | 55 |     Transport1 = ?debug_wrap(Transport0), | 
 | 56 |     {ok, Transport1}. | 
| Bryan Duxbury | d3879f8 | 2010-08-19 05:06:02 +0000 | [diff] [blame] | 57 |  | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 58 | -spec write(#transport{}, iolist() | binary()) -> {#transport{}, ok | {error, _Reason}}. | 
| David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 59 | write(Transport, Data) -> | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 60 |     Module = Transport#transport.module, | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 61 |     {NewTransData, Result} = Module:write(Transport#transport.data, Data), | 
 | 62 |     {Transport#transport{data = NewTransData}, Result}. | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 63 |  | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 64 | -spec read(#transport{}, non_neg_integer()) -> {#transport{}, {ok, binary()} | {error, _Reason}}. | 
| David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 65 | read(Transport, Len) when is_integer(Len) -> | 
 | 66 |     Module = Transport#transport.module, | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 67 |     {NewTransData, Result} = Module:read(Transport#transport.data, Len), | 
 | 68 |     {Transport#transport{data = NewTransData}, Result}. | 
| David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 69 |  | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 70 | -spec flush(#transport{}) -> {#transport{}, ok | {error, _Reason}}. | 
 | 71 | flush(Transport = #transport{module = Module, data = Data}) -> | 
 | 72 |     {NewTransData, Result} = Module:flush(Data), | 
 | 73 |     {Transport#transport{data = NewTransData}, Result}. | 
| David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 74 |  | 
| David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 75 | -spec close(#transport{}) -> {#transport{}, ok | {error, _Reason}}. | 
 | 76 | close(Transport = #transport{module = Module, data = Data}) -> | 
 | 77 |     {NewTransData, Result} = Module:close(Data), | 
 | 78 |     {Transport#transport{data = NewTransData}, Result}. |