Added function name to read/writeMessageBegin args

Added cpp generator for master server message processor


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664752 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/src/cpp_generator.py b/compiler/src/cpp_generator.py
index 92636c5..06c253d 100644
--- a/compiler/src/cpp_generator.py
+++ b/compiler/src/cpp_generator.py
@@ -256,59 +256,6 @@
 CPP_TRANSPORT = CPP_TRANSPORT_NS+"::TTransport"
 CPP_TRANSPORTP = CPP_SP.substitute(klass=CPP_TRANSPORT)
 
-CPP_SERVER_FUNCTION_DECLARATION = Template("""    void process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans);
-""")
-
-CPP_SERVER_FUNCTION_DEFINITION = Template("""
-void ${service}ServerIf::process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) {
-
-    uint32_t xfer = 0;
-
-    ${argsStructDeclaration};
-
-    ${argsStructReader};
-
-    iprot->readMessageEnd(itrans);
-
-    ${returnValueDeclaration};
-
-    ${functionCall};
-
-    ${resultStructDeclaration};
-
-    ${returnToResult};
-
-    oprot->writeMessageBegin(otrans, """+CPP_PROTOCOL_REPLY+""", seqid);
-
-    ${resultStructWriter};
-
-    oprot->writeMessaeEnd(otrans);
-
-    otrans->flush();
-}
-""")
-
-CPP_SERVER_PROCESS_DEFINITION = Template("""
-bool ${service}ServerIf::process("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) {
-
-    uint32_t xfer = 0;
-
-    std::string name;
-
-    """+CPP_MESSAGE_TYPE+""" messageType;
-
-    uint32_t seqid;
-
-    _iprot->readMessageBegin(_itrans, name, messageType, cseqid);
-
-    if(messageType == """+CPP_PROTOCOL_CALL+""") {
-${callProcessSwitch}
-    } else {
-        throw """+CPP_EXCEPTION+"""(\"Unexpected message type\");     
-    }
-}
-""")
-
 CPP_PROTOCOL_TSTOP = CPP_PROTOCOL_NS+"::T_STOP"
 CPP_PROTOCOL_TTYPE = CPP_PROTOCOL_NS+"::TType"
 CPP_PROTOCOL_MESSAGE_TYPE = CPP_PROTOCOL_NS+"::TMessageType"
@@ -339,6 +286,60 @@
     SetType : CPP_PROTOCOL_NS+"::T_SET"
 }
 
+
+CPP_SERVER_FUNCTION_DECLARATION = Template("""    void process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans);
+""")
+
+CPP_SERVER_FUNCTION_DEFINITION = Template("""
+void ${service}ServerIf::process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) {
+
+    uint32_t xfer = 0;
+
+    ${argsStructDeclaration};
+
+    ${argsStructReader};
+
+    _iprot->readMessageEnd(itrans);
+
+    ${returnValueDeclaration};
+
+    ${functionCall};
+
+    ${resultStructDeclaration};
+
+    ${returnToResult};
+
+    _oprot->writeMessageBegin(otrans, \"${function}\", """+CPP_PROTOCOL_REPLY+""", seqid);
+
+    ${resultStructWriter};
+
+    _oprot->writeMessageEnd(otrans);
+
+    otrans->flush();
+}
+""")
+
+CPP_SERVER_PROCESS_DEFINITION = Template("""
+bool ${service}ServerIf::process("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) {
+
+    uint32_t xfer = 0;
+
+    std::string name;
+
+    """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType;
+
+    uint32_t seqid;
+
+    _iprot->readMessageBegin(itrans, name, messageType, seqid);
+
+    if(messageType == """+CPP_PROTOCOL_CALL+""") {
+${callProcessSwitch}
+    } else {
+        throw """+CPP_EXCEPTION+"""(\"Unexpected message type\");     
+    }
+}
+""")
+
 def toWireType(ttype):
 
     if isinstance(ttype, PrimitiveType):
@@ -384,11 +385,12 @@
 ${returnDeclaration} ${service}Client::${function}(${argsDeclaration}) {
 
     uint32_t xfer = 0;
+    std::string name;
     """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType;
     uint32_t cseqid = 0;
     uint32_t rseqid = 0;
 
-    _oprot->writeMessageBegin(_otrans, """+CPP_PROTOCOL_CALL+""", cseqid);
+    _oprot->writeMessageBegin(_otrans, \"${function}\", """+CPP_PROTOCOL_CALL+""", cseqid);
 
     ${argsStructDeclaration};
 
@@ -398,7 +400,7 @@
 
     _otrans->flush();
 
-    _iprot->readMessageBegin(_itrans, messageType, rseqid);
+    _iprot->readMessageBegin(_itrans, name, messageType, rseqid);
 
     if(messageType != """+CPP_PROTOCOL_REPLY+""" || 
        rseqid != cseqid) {
@@ -478,11 +480,10 @@
     for function in service.functionList:
 	
 	result+= toServerFunctionDefinition(service.name, function, debugp)
-
     
-    callProcessSwitch = "if"+string.join(["(name.compare(\""+function.name+"\") == 0) {"+toServerProcessFunctionCall(function)+";}" for function in service.functionList], "\n    else if")
+    callProcessSwitch = "        if"+string.join(["(name.compare(\""+function.name+"\") == 0) { process_"+function.name+"(seqid, itrans, otrans);}" for function in service.functionList], "\n        else if")+"\n        else {throw "+CPP_EXCEPTION+"(\"Unknown function name \\\"\"+name+\"\\\"\");}"
 
-    result+= CPP_SERVER_PROCESS_DEFINITION(service=service.name, callProcessSwitch=callProcessSwitch)
+    result+= CPP_SERVER_PROCESS_DEFINITION.substitute(service=service.name, callProcessSwitch=callProcessSwitch)
 
     return result
 
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cc b/lib/cpp/src/protocol/TBinaryProtocol.cc
index f1e4ec9..7ad3b2b 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cc
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cc
@@ -5,9 +5,11 @@
 namespace facebook { namespace thrift { namespace protocol { 
 
 uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out,
+					    const std::string name,
 					    const TMessageType messageType,
 					    const uint32_t seqid) const {
   return 
+    writeString(out, name) + 
     writeByte(out, (uint8_t)messageType) +
     writeU32(out, seqid);
 }
@@ -134,11 +136,13 @@
  */
 
 uint32_t TBinaryProtocol::readMessasgeBegin(shared_ptr<TTransport> in,
+					    std::string& name,
 					    TMessageType& messageType,
 					    uint32_t& seqid) const {
 
   uint32_t result = 0;
   uint8_t type;
+  result+= readString(in, name);
   result+=  readByte(in, type);
   messageType = (TMessageType)type;
   result+= readU32(in, seqid);
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 89f4d4a..e70f687 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -25,6 +25,7 @@
    */
 
   virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
+				     const std::string name,
 				     const TMessageType messageType,
 				     const uint32_t seqid) const;
 
@@ -97,8 +98,9 @@
 
 
   uint32_t readMessasgeBegin(shared_ptr<TTransport> in,
-			      TMessageType& messageType,
-			      uint32_t& seqid) const;
+			     std::string& name,
+			     TMessageType& messageType,
+			     uint32_t& seqid) const;
 
   uint32_t readMessageEnd(shared_ptr<TTransport> in) const;
 
diff --git a/lib/cpp/src/protocol/TProtocol.h b/lib/cpp/src/protocol/TProtocol.h
index 329b7bf..7dbb4a0 100644
--- a/lib/cpp/src/protocol/TProtocol.h
+++ b/lib/cpp/src/protocol/TProtocol.h
@@ -79,6 +79,7 @@
    */
 
   virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
+				     const std::string name,
 				     const TMessageType messageType,
 				     const uint32_t seqid) const = 0;
 
@@ -150,8 +151,9 @@
    */
 
   virtual uint32_t readMessageBegin(shared_ptr<TTransport> in,
-				     TMessageType& messageType,
-				     uint32_t& seqid) const = 0;
+				    std::string& name,
+				    TMessageType& messageType,
+				    uint32_t& seqid) const = 0;
   
   virtual uint32_t readMessageEnd(shared_ptr<TTransport> in) const = 0;