2016-02-16 13 views
9

मृत सरल बहाव संघ उदाहरण। पर्यावरण: नवीनतम बचत, सर्वर के रूप में सीपीपी, ग्राहक mytest.thrift के रूप में जावा:थ्रिफ्ट जावा क्लाइंट यूनियन को ठीक से संभाल नहीं सकता

namespace java com.wilbeibi.thrift 

union Value { 
    1: i16  i16_v, 
    2: string str_v, 
} 

struct Box { 
    1: Value value; 
} 

service MyTest { 
    Box echoUnion(1: i32 number); 
} 

C++ server code:

#include "MyTest.h" 
#include <thrift/protocol/TBinaryProtocol.h> 
#include <thrift/server/TSimpleServer.h> 
#include <thrift/transport/TServerSocket.h> 
#include <thrift/transport/TBufferTransports.h> 

using namespace ::apache::thrift; 
using namespace ::apache::thrift::protocol; 
using namespace ::apache::thrift::transport; 
using namespace ::apache::thrift::server; 

using boost::shared_ptr; 

class MyTestHandler : virtual public MyTestIf { 
public: 
    MyTestHandler() { 
    // Your initialization goes here 
    } 

    void echoUnion(Box& _return, const int32_t number) { 
    // Your implementation goes here 
    printf("Into echoUnion\n"); 
    if (number % 2 == 0) { 
    Value v; 
    v.__set_i16_v(100); 
    v.__isset.i16_v = true; 
    _return.__set_value(v); 
    printf("Even number set int32\n"); 
    } else { 
    Value v; 
    v.__set_str_v("String value"); 
    v.__isset.str_v = true; 
    _return.__set_value(v); 
    printf("Odd number set string\n"); 
    } 
    printf("echoUnion\n"); 
    } 

}; 

int main(int argc, char **argv) { 
    int port = 9090; 
    shared_ptr<MyTestHandler> handler(new MyTestHandler()); 
    shared_ptr<TProcessor> processor(new MyTestProcessor(handler)); 
    shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); 
    shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); 
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); 

    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); 
    printf("Server is running on %d\n", port); 
    server.serve(); 
    return 0; 
} 

java client code:

// some imports here 
public class Client { 
    public void startClient() { 
     TTransport transport; 
      try { 
       transport = new TSocket("localhost", 9090); 
       TProtocol protocol = new TBinaryProtocol(transport); 
       MyTest.Client client = new MyTest.Client(protocol); 
       transport.open(); 
       Box box = client.echoUnion(1);    
       System.out.println(box.toString()); 

       Box box2 = client.echoUnion(2); 
       System.out.println(box2.toString()); 

       transport.close(); 
      } catch (TTransportException e) { 
       e.printStackTrace(); 
      } catch (TException e) { 
       e.printStackTrace(); 
      } 
     } 
    public static void main(String[] args) { 
      Client client = new Client(); 
      client.startClient(); 
     } 
}  

किसी तरह, जावा ग्राहक बाहर स्ट्रिंग ठीक से मुद्रित नहीं कर सकते। यहाँ सार पर (मैं भी एक अजगर ग्राहक लिखा था, लेकिन है कि काम लगता है)

पूर्ण कोड: thrift file, c++ and java code

उत्तर

3

वास्तव में आप THRIFT-1833 बग जो संकलक का कारण संघ प्रकार के लिए अमान्य सी ++ कोड के उत्पादन के लिए देख रहे हैं।

आपके मामले में सर्वर यूनियन प्रकार के दोनों फ़ील्ड लिखता है जबकि क्लाइंट हमेशा पहले ही पढ़ता है - i16_v (शेष बाइट अभी भी बफर में रहते हैं)। तो दूसरा पढ़ने कभी समाप्त नहीं होता क्योंकि यह बफर में कुछ अप्रत्याशित डेटा पाता है।

आप union के बजाय struct का उपयोग कर सकते हैं और मैन्युअल रूप से सिंगल-फील्ड तर्क बनाए रख सकते हैं। या आप बग ठीक होने तक या तो योगदान/प्रतीक्षा कर सकते हैं।

अंतिम विकल्प इस तरह गलत तरीके से उत्पन्न सी ++ स्रोत कोड पर एक पैच लागू होता है:

--- mytest_types.cpp 2016-02-26 20:02:57.210652969 +0300 
+++ mytest_types.cpp.old 2016-02-26 20:02:39.650652742 +0300 
@@ -80,13 +80,17 @@ 
apache::thrift::protocol::TOutputRecursionTracker tracker(*oprot); 
xfer += oprot->writeStructBegin("Value"); 

- xfer += oprot->writeFieldBegin("i16_v", ::apache::thrift::protocol::T_I16, 1); 
- xfer += oprot->writeI16(this->i16_v); 
- xfer += oprot->writeFieldEnd(); 
+ if (this->__isset.i16_v) { 
+ xfer += oprot->writeFieldBegin("i16_v", ::apache::thrift::protocol::T_I16, 1); 
+ xfer += oprot->writeI16(this->i16_v); 
+ xfer += oprot->writeFieldEnd(); 
+ } 

- xfer += oprot->writeFieldBegin("str_v", ::apache::thrift::protocol::T_STRING, 2); 
- xfer += oprot->writeString(this->str_v); 
- xfer += oprot->writeFieldEnd(); 
+ if (this->__isset.str_v) { 
+ xfer += oprot->writeFieldBegin("str_v", ::apache::thrift::protocol::T_STRING, 2); 
+ xfer += oprot->writeString(this->str_v); 
+ xfer += oprot->writeFieldEnd(); 
+ } 
संबंधित मुद्दे