Thrift: Added support for double type across all languages
Summary: Just for completeness cause I'm crazy. Let's never use these!
Notes: Also made thrift grammar support # style comments, so you can do this at the top of your files
#!/usr/local/bin/thrift --cpp
/**
* This is a thrift def file youc an invoke directly and gen code!
*/
blah
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664789 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cc b/lib/cpp/src/protocol/TBinaryProtocol.cc
index 51c9306..9fb2ec3 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cc
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cc
@@ -116,7 +116,24 @@
out->write((uint8_t*)&net, 8);
return 8;
}
+
+uint32_t TBinaryProtocol::writeDouble(shared_ptr<TTransport> out,
+ const double dub) const {
+ uint8_t b[8];
+ uint8_t* d = (uint8_t*)&dub;
+ b[0] = d[7];
+ b[1] = d[6];
+ b[2] = d[5];
+ b[3] = d[4];
+ b[4] = d[3];
+ b[5] = d[2];
+ b[6] = d[1];
+ b[7] = d[0];
+ out->write((uint8_t*)b, 8);
+ return 8;
+}
+
uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out,
const string& str) const {
uint32_t result = writeI32(out, str.size());
@@ -276,6 +293,23 @@
return 8;
}
+uint32_t TBinaryProtocol::readDouble(shared_ptr<TTransport> in,
+ double& dub) const {
+ uint8_t b[8];
+ uint8_t d[8];
+ in->readAll(b, 8);
+ d[0] = b[7];
+ d[1] = b[6];
+ d[2] = b[5];
+ d[3] = b[4];
+ d[4] = b[3];
+ d[5] = b[2];
+ d[6] = b[1];
+ d[7] = b[0];
+ dub = *(double*)d;
+ return 8;
+}
+
uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
string& str) const {
uint32_t result;
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 5bca6dd..7f36a57 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -80,6 +80,10 @@
uint32_t writeI64(shared_ptr<TTransport> out,
const int64_t i64) const;
+ uint32_t writeDouble(shared_ptr<TTransport> out,
+ const double dub) const;
+
+
uint32_t writeString(shared_ptr<TTransport> out,
const std::string& str) const;
@@ -141,6 +145,9 @@
uint32_t readI64(shared_ptr<TTransport> in,
int64_t& i64) const;
+ uint32_t readDouble(shared_ptr<TTransport> in,
+ double& dub) const;
+
uint32_t readString(shared_ptr<TTransport> in,
std::string& str) const;
};
diff --git a/lib/cpp/src/protocol/TProtocol.h b/lib/cpp/src/protocol/TProtocol.h
index 65d6f3c..89c7f48 100644
--- a/lib/cpp/src/protocol/TProtocol.h
+++ b/lib/cpp/src/protocol/TProtocol.h
@@ -16,7 +16,7 @@
using namespace facebook::thrift::transport;
-#define ntohll(x) (((uint64_t)(ntohl((int)((x << 32) >> 32))) << 32) | (uint32_t)ntohl(((int)(x >> 32))))
+#define ntohll(x) (((uint64_t)(ntohl((int)((x & 0x00000000FFFFFFFF)))) << 32) | (uint32_t)ntohl(((int)(x >> 32 & 0x00000000FFFFFFFF))))
#define htonll(x) ntohll(x)
@@ -38,6 +38,7 @@
T_I32 = 8,
T_U64 = 9,
T_I64 = 10,
+ T_DOUBLE = 4,
T_STRING = 11,
T_UTF7 = 11,
T_STRUCT = 12,
@@ -133,6 +134,9 @@
virtual uint32_t writeI64(shared_ptr<TTransport> out,
const int64_t i64) const = 0;
+ virtual uint32_t writeDouble(shared_ptr<TTransport> out,
+ const double dub) const = 0;
+
virtual uint32_t writeString(shared_ptr<TTransport> out,
const std::string& str) const = 0;
@@ -193,6 +197,9 @@
virtual uint32_t readI64(shared_ptr<TTransport> in,
int64_t& i64) const = 0;
+ virtual uint32_t readDouble(shared_ptr<TTransport> in,
+ double& dub) const = 0;
+
virtual uint32_t readString(shared_ptr<TTransport> in,
std::string& str) const = 0;
@@ -226,6 +233,11 @@
int64_t i64;
return readI64(in, i64);
}
+ case T_DOUBLE:
+ {
+ double dub;
+ return readDouble(in, dub);
+ }
case T_STRING:
{
std::string str;
diff --git a/lib/java/src/protocol/TBinaryProtocol.java b/lib/java/src/protocol/TBinaryProtocol.java
index d90dece..4d6c345 100644
--- a/lib/java/src/protocol/TBinaryProtocol.java
+++ b/lib/java/src/protocol/TBinaryProtocol.java
@@ -95,6 +95,10 @@
out.write(i64out, 0, 8);
}
+ public void writeDouble(TTransport out, double dub) throws TException {
+ writeI64(out, Double.doubleToLongBits(dub));
+ }
+
public void writeString(TTransport out, String str) throws TException {
byte[] dat = str.getBytes();
writeI32(out, dat.length);
@@ -203,6 +207,10 @@
((long)(i64rd[7] & 0xff));
}
+ public double readDouble(TTransport in) throws TException {
+ return Double.longBitsToDouble(readI64(in));
+ }
+
public String readString(TTransport in) throws TException {
int size = readI32(in);
byte[] buf = new byte[size];
diff --git a/lib/java/src/protocol/TProtocol.java b/lib/java/src/protocol/TProtocol.java
index 69fe885..0831d12 100644
--- a/lib/java/src/protocol/TProtocol.java
+++ b/lib/java/src/protocol/TProtocol.java
@@ -61,6 +61,10 @@
public void writeI64 (TTransport out,
long i64) throws TException;
+ public void writeDouble (TTransport out,
+ double dub) throws TException;
+
+
public void writeString (TTransport out,
String str) throws TException;
@@ -102,6 +106,8 @@
public long readI64 (TTransport in) throws TException;
+ public double readDouble (TTransport in) throws TException;
+
public String readString (TTransport in) throws TException;
}
diff --git a/lib/java/src/protocol/TProtocolUtil.java b/lib/java/src/protocol/TProtocolUtil.java
index 5223639..1c88f8a 100644
--- a/lib/java/src/protocol/TProtocolUtil.java
+++ b/lib/java/src/protocol/TProtocolUtil.java
@@ -34,6 +34,10 @@
{
prot.readI64(in);
}
+ case TType.DOUBLE:
+ {
+ prot.readDouble(in);
+ }
case TType.STRING:
{
prot.readString(in);
diff --git a/lib/java/src/protocol/TType.java b/lib/java/src/protocol/TType.java
index 37b53bd..bed9a21 100644
--- a/lib/java/src/protocol/TType.java
+++ b/lib/java/src/protocol/TType.java
@@ -10,6 +10,7 @@
public static final byte VOID = 1;
public static final byte BOOL = 2;
public static final byte BYTE = 3;
+ public static final byte DOUBLE = 4;
public static final byte I16 = 6;
public static final byte I32 = 8;
public static final byte I64 = 10;
diff --git a/lib/php/src/protocol/TBinaryProtocol.php b/lib/php/src/protocol/TBinaryProtocol.php
index 4bb0297..2b1384f 100644
--- a/lib/php/src/protocol/TBinaryProtocol.php
+++ b/lib/php/src/protocol/TBinaryProtocol.php
@@ -136,6 +136,12 @@
return 8;
}
+ public function writeDouble($out, $value) {
+ $data = pack('d', $value);
+ $out->write(strrev($data), 8);
+ return 8;
+ }
+
public function writeString($out, $value) {
$len = strlen($value);
$result = $this->writeI32($out, $len);
@@ -302,6 +308,13 @@
return 8;
}
+ public function readDouble($in, &$value) {
+ $data = strrev($in->readAll(8));
+ $arr = unpack('d', $data);
+ $value = $arr[1];
+ return 8;
+ }
+
public function readString($in, &$value) {
$result = $this->readI32($in, $len);
$value = $in->readAll($len);
diff --git a/lib/php/src/protocol/TProtocol.php b/lib/php/src/protocol/TProtocol.php
index 855d588..6101f57 100644
--- a/lib/php/src/protocol/TProtocol.php
+++ b/lib/php/src/protocol/TProtocol.php
@@ -89,6 +89,8 @@
public abstract function writeI64($out, $i64);
+ public abstract function writeDouble($out, $dub);
+
public abstract function writeString($out, $str);
@@ -139,6 +141,8 @@
public abstract function readI64($in, &$i64);
+ public abstract function readDouble($in, &$dub);
+
public abstract function readString($in, &$str);
/**
@@ -160,6 +164,8 @@
return $this->readI32($in, $i32);
case TType::I64:
return $this->readI64($in, $i64);
+ case TType::DOUBLE:
+ return $this->readDouble($in, $dub);
case TType::STRING:
return $this->readString($in, $str);
case TType::STRUCT:
diff --git a/lib/php/src/protocol/TType.php b/lib/php/src/protocol/TType.php
index 6ef0935..aa930ce 100644
--- a/lib/php/src/protocol/TType.php
+++ b/lib/php/src/protocol/TType.php
@@ -15,7 +15,8 @@
const VOID = 1;
const BOOL = 2;
const BYTE = 3;
- const I08 = 4;
+ const I08 = 3;
+ const DOUBLE = 4;
const I16 = 6;
const I32 = 8;
const I64 = 10;
diff --git a/lib/py/src/protocol/TBinaryProtocol.py b/lib/py/src/protocol/TBinaryProtocol.py
index 860f461..25f3218 100644
--- a/lib/py/src/protocol/TBinaryProtocol.py
+++ b/lib/py/src/protocol/TBinaryProtocol.py
@@ -73,6 +73,10 @@
buff = pack("!q", i64)
otrans.write(buff)
+ def writeDouble(self, otrans, dub):
+ buff = pack("!d", dub)
+ otrans.write(buff)
+
def writeString(self, otrans, str):
self.writeI32(otrans, len(str))
otrans.write(str)
@@ -153,6 +157,11 @@
val, = unpack('!q', buff)
return val
+ def readDouble(self, itrans):
+ buff = itrans.readAll(8)
+ val, = unpack('!d', buff)
+ return val
+
def readString(self, itrans):
len = self.readI32(itrans)
str = itrans.readAll(len)
diff --git a/lib/py/src/protocol/TProtocol.py b/lib/py/src/protocol/TProtocol.py
index 3105e25..0b480d3 100644
--- a/lib/py/src/protocol/TProtocol.py
+++ b/lib/py/src/protocol/TProtocol.py
@@ -3,7 +3,8 @@
VOID = 1
BOOL = 2
BYTE = 3
- I08 = 4
+ I08 = 3
+ DOUBLE = 4
I16 = 6
I32 = 8
I64 = 10
@@ -78,6 +79,9 @@
def writeI64(self, otrans, i64):
pass
+ def writeDouble(self, otrans, dub):
+ pass
+
def writeString(self, otrans, str):
pass
@@ -132,6 +136,9 @@
def readI64(self, itrans):
pass
+ def readDouble(self, itrans):
+ pass
+
def readString(self, itrans):
pass
@@ -148,6 +155,8 @@
self.readI32(itrans)
elif type == TType.I64:
self.readI64(itrans)
+ elif type == TType.DOUBLE:
+ self.readDouble(itrans)
elif type == TType.STRING:
self.readString(itrans)
elif type == TType.STRUCT:
diff --git a/lib/py/src/server/TServer.py b/lib/py/src/server/TServer.py
index 69be260..a5d5621 100644
--- a/lib/py/src/server/TServer.py
+++ b/lib/py/src/server/TServer.py
@@ -1,3 +1,6 @@
+import sys
+import traceback
+
from thrift.Thrift import TProcessor
from thrift.transport import TTransport
@@ -27,6 +30,6 @@
while True:
self.processor.process(client, client)
except Exception, x:
- print x
+ print '%s, %s, %s' % (type(x), x, traceback.format_exc())
print 'Client died.'
client.close()
diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py
index 4ef35d9..61f1cff 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -36,7 +36,7 @@
def read(self, sz):
buff = self.handle.recv(sz)
if len(buff) == 0:
- raise Exception('TScket read 0 bytes')
+ raise Exception('TSocket read 0 bytes')
return buff
def write(self, buff):