comparison Cassandra/lib/Thrift/BufferedTransport.pm @ 0:a2f0a2c135cf

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 06 Jun 2010 22:00:38 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a2f0a2c135cf
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 require 5.6.0;
21 use strict;
22 use warnings;
23
24 use Thrift;
25 use Thrift::Transport;
26
27 package Thrift::BufferedTransport;
28 use base('Thrift::Transport');
29
30 sub new
31 {
32 my $classname = shift;
33 my $transport = shift;
34 my $rBufSize = shift || 512;
35 my $wBufSize = shift || 512;
36
37 my $self = {
38 transport => $transport,
39 rBufSize => $rBufSize,
40 wBufSize => $wBufSize,
41 wBuf => '',
42 rBuf => '',
43 };
44
45 return bless($self,$classname);
46 }
47
48 sub isOpen
49 {
50 my $self = shift;
51
52 return $self->{transport}->isOpen();
53 }
54
55 sub open
56 {
57 my $self = shift;
58 $self->{transport}->open();
59 }
60
61 sub close()
62 {
63 my $self = shift;
64 $self->{transport}->close();
65 }
66
67 sub readAll
68 {
69 my $self = shift;
70 my $len = shift;
71
72 return $self->{transport}->readAll($len);
73 }
74
75 sub read
76 {
77 my $self = shift;
78 my $len = shift;
79 my $ret;
80
81 # Methinks Perl is already buffering these for us
82 return $self->{transport}->read($len);
83 }
84
85 sub write
86 {
87 my $self = shift;
88 my $buf = shift;
89
90 $self->{wBuf} .= $buf;
91 if (length($self->{wBuf}) >= $self->{wBufSize}) {
92 $self->{transport}->write($self->{wBuf});
93 $self->{wBuf} = '';
94 }
95 }
96
97 sub flush
98 {
99 my $self = shift;
100
101 if (length($self->{wBuf}) > 0) {
102 $self->{transport}->write($self->{wBuf});
103 $self->{wBuf} = '';
104 }
105 $self->{transport}->flush();
106 }
107
108
109 #
110 # BufferedTransport factory creates buffered transport objects from transports
111 #
112 package Thrift::BufferedTransportFactory;
113
114 sub new {
115 my $classname = shift;
116 my $self = {};
117
118 return bless($self,$classname);
119 }
120
121 #
122 # Build a buffered transport from the base transport
123 #
124 # @return Thrift::BufferedTransport transport
125 #
126 sub getTransport
127 {
128 my $self = shift;
129 my $trans = shift;
130
131 my $buffered = Thrift::BufferedTransport->new($trans);
132 return $buffered;
133 }
134
135
136 1;