Author: jensg
Date: Mon Jul 29 21:26:03 2013
New Revision: 1508204
URL: http://svn.apache.org/r1508204
Log:
Existing Go tutorial skeleton filled withsome useful content
Modified:
thrift/site/ (props changed)
thrift/site/config.yaml
thrift/site/content/tutorial/go.md
thrift/site/publish/about/index.html
thrift/site/publish/tutorial/go/index.html
Propchange: thrift/site/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Jul 29 21:26:03 2013
@@ -0,0 +1 @@
+vendor
Modified: thrift/site/config.yaml
URL:
http://svn.apache.org/viewvc/thrift/site/config.yaml?rev=1508204&r1=1508203&r2=1508204&view=diff
==============================================================================
--- thrift/site/config.yaml (original)
+++ thrift/site/config.yaml Mon Jul 29 21:26:03 2013
@@ -44,7 +44,7 @@ committers: [
[ "molinaro", "Anthony Molinaro", "Erlang, Perl, autotools", "-8" ],
[ "roger", "Roger Meier", "Continuous Integration, C++, C#,
JavaScript", "2" ],
[ "jfarrell", "Jake Farrell", "Build, Client Publishing, Java, PHP,
Ruby", "-5" ],
- [ "jensg", "Jens Geyer", "Delphi, C#", "1" ],
+ [ "jensg", "Jens Geyer", "Delphi, C#, Go", "1" ],
[ "carl", "Carl Yeksigian", "C#", "-5"]
]
Modified: thrift/site/content/tutorial/go.md
URL:
http://svn.apache.org/viewvc/thrift/site/content/tutorial/go.md?rev=1508204&r1=1508203&r2=1508204&view=diff
==============================================================================
--- thrift/site/content/tutorial/go.md (original)
+++ thrift/site/content/tutorial/go.md Mon Jul 29 21:26:03 2013
@@ -6,13 +6,255 @@ library_lang: "go"
### Prerequisites
+ * At least Go 1.1.x is required to run the tutorial code.
+ * The GOPATH may need to be adjusted, alternatively manually put the Go
Thrift library files into a suitable location.
+
### Client
+Implements the client code which consumes the tutorial service.
+
<pre><code class="language-c">
+import (
+ "fmt"
+ "git.apache.org/thrift.git/lib/go/thrift"
+ "tutorial"
+)
+
+func handleClient(client *tutorial.CalculatorClient) (err error) {
+ client.Ping()
+ fmt.Println("ping()")
+
+ sum, _ := client.Add(1, 1)
+ fmt.Print("1+1=", sum, "\n")
+
+ work := tutorial.NewWork()
+ work.Op = tutorial.Operation_DIVIDE
+ work.Num1 = 1
+ work.Num2 = 0
+ quotient, ouch, err := client.Calculate(1, work)
+ if err != nil {
+ fmt.Println("Error during operation:", err)
+ return err
+ } else if ouch != nil {
+ fmt.Println("Invalid operation:", ouch)
+ } else {
+ fmt.Println("Whoa we can divide by 0 with new value:", quotient)
+ }
+
+ work.Op = tutorial.Operation_SUBTRACT
+ work.Num1 = 15
+ work.Num2 = 10
+ diff, ouch, err := client.Calculate(1, work)
+ if err != nil {
+ fmt.Println("Error during operation:", err)
+ return err
+ } else if ouch != nil {
+ fmt.Println("Invalid operation:", ouch)
+ } else {
+ fmt.Print("15-10=", diff, "\n")
+ }
+
+ log, err := client.GetStruct(1)
+ if err != nil {
+ fmt.Println("Unable to get struct:", err)
+ return err
+ } else {
+ fmt.Println("Check log:", log.Value)
+ }
+ return err
+}
+
+func runClient(transportFactory thrift.TTransportFactory, protocolFactory
thrift.TProtocolFactory, addr string) error {
+ var transport thrift.TTransport
+ transport, err := thrift.NewTSocket(addr)
+ if err != nil {
+ fmt.Println("Error opening socket:", err)
+ return err
+ }
+ transport = transportFactory.GetTransport(transport)
+ defer transport.Close()
+ if err := transport.Open(); err != nil {
+ return err
+ }
+ return handleClient(tutorial.NewCalculatorClientFactory(transport,
protocolFactory))
+}
</code></pre>
### Server
+Implements a simple socket server.
+
+<pre><code class="language-c">
+import (
+ "fmt"
+ "git.apache.org/thrift.git/lib/go/thrift"
+ "tutorial"
+)
+
+func runServer(transportFactory thrift.TTransportFactory, protocolFactory
thrift.TProtocolFactory, addr string) error {
+ transport, err := thrift.NewTServerSocket(addr)
+ if err != nil {
+ return err
+ }
+ handler := NewCalculatorHandler()
+ processor := tutorial.NewCalculatorProcessor(handler)
+ server := thrift.NewTSimpleServer4(processor, transport,
transportFactory, protocolFactory)
+
+ fmt.Println("Starting the simple server... on ", transport.Addr())
+ return server.Serve()
+}
+</code></pre>
+
+
+### Handler
+
+The handler implements the service defined in the tutorial IDL file and
exposed by the server.
+
+<pre><code class="language-c">
+import (
+ "fmt"
+ "shared"
+ "strconv"
+ "tutorial"
+)
+
+type CalculatorHandler struct {
+ log map[int]*shared.SharedStruct
+}
+
+func NewCalculatorHandler() *CalculatorHandler {
+ return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
+}
+
+func (p *CalculatorHandler) Ping() (err error) {
+ fmt.Print("ping()\n")
+ return nil
+}
+
+func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err
error) {
+ fmt.Print("add(", num1, ",", num2, ")\n")
+ return num1 + num2, nil
+}
+
+func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val
int32, ouch *tutorial.InvalidOperation, err error) {
+ fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2,
"})\n")
+ switch w.Op {
+ case tutorial.Operation_ADD:
+ val = w.Num1 + w.Num2
+ break
+ case tutorial.Operation_SUBTRACT:
+ val = w.Num1 - w.Num2
+ break
+ case tutorial.Operation_MULTIPLY:
+ val = w.Num1 * w.Num2
+ break
+ case tutorial.Operation_DIVIDE:
+ if w.Num2 == 0 {
+ ouch = tutorial.NewInvalidOperation()
+ ouch.What = int32(w.Op)
+ ouch.Why = "Cannot divide by 0"
+ return
+ }
+ val = w.Num1 / w.Num2
+ break
+ default:
+ ouch = tutorial.NewInvalidOperation()
+ ouch.What = int32(w.Op)
+ ouch.Why = "Unknown operation"
+ return
+ }
+ entry := shared.NewSharedStruct()
+ entry.Key = logid
+ entry.Value = strconv.Itoa(int(val))
+ k := int(logid)
+
+ p.log[k] = entry
+ return val, ouch, err
+}
+
+func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, error)
{
+ fmt.Print("getStruct(", key, ")\n")
+ v, _ := p.log[int(key)]
+ return v, nil
+}
+
+func (p *CalculatorHandler) Zip() (err error) {
+ fmt.Print("zip()\n")
+ return nil
+}
+</code></pre>
+
+### Main code
+
+The main code is shared between server and client. The ```-server``` argument
distinguishes betweeen the two modes.
+
+<pre><code class="language-c">
+import (
+ "flag"
+ "fmt"
+ "git.apache.org/thrift.git/lib/go/thrift"
+ "os"
+)
+
+func Usage() {
+ fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
+ flag.PrintDefaults()
+ fmt.Fprint(os.Stderr, "\n")
+}
+
+func main() {
+ flag.Usage = Usage
+ server := flag.Bool("server", false, "Run server")
+ protocol := flag.String("P", "binary", "Specify the protocol (binary,
compact, simplejson)")
+ framed := flag.Bool("framed", false, "Use framed transport")
+ buffered := flag.Bool("buffered", false, "Use buffered transport")
+ addr := flag.String("addr", "localhost:9090", "Address to listen to")
+
+ flag.Parse()
+
+ var protocolFactory thrift.TProtocolFactory
+ switch *protocol {
+ case "compact":
+ protocolFactory = thrift.NewTCompactProtocolFactory()
+ case "simplejson":
+ protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
+ case "json":
+ protocolFactory = thrift.NewTJSONProtocolFactory()
+ case "binary", "":
+ protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ default:
+ fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol,
"\n")
+ Usage()
+ os.Exit(1)
+ }
+
+ var transportFactory thrift.TTransportFactory
+ if *buffered {
+ transportFactory = thrift.NewTBufferedTransportFactory(8192)
+ } else {
+ transportFactory = thrift.NewTTransportFactory()
+ }
+
+ if *framed {
+ transportFactory =
thrift.NewTFramedTransportFactory(transportFactory)
+ }
+
+ if *server {
+ if err := runServer(transportFactory, protocolFactory, *addr);
err != nil {
+ fmt.Println("error running server:", err)
+ }
+ } else {
+ if err := runClient(transportFactory, protocolFactory, *addr);
err != nil {
+ fmt.Println("error running client:", err)
+ }
+ }
+}
+</code></pre>
## Additional Information
+ * Try using one of the other available protocols, default is binary.
+ * Try using the buffered and/or framed transport options.
+ * Note that both server and client must use the exact same protocol and
transport stack.
+
+
\ No newline at end of file
Modified: thrift/site/publish/about/index.html
URL:
http://svn.apache.org/viewvc/thrift/site/publish/about/index.html?rev=1508204&r1=1508203&r2=1508204&view=diff
==============================================================================
--- thrift/site/publish/about/index.html (original)
+++ thrift/site/publish/about/index.html Mon Jul 29 21:26:03 2013
@@ -212,7 +212,7 @@ Strive for performance first, elegance s
<tr class="">
<td class="username">jensg</td>
<td class="fullname">Jens Geyer</td>
- <td>Delphi, C#</td>
+ <td>Delphi, C#, Go</td>
<td align="right">1</td>
</tr>
Modified: thrift/site/publish/tutorial/go/index.html
URL:
http://svn.apache.org/viewvc/thrift/site/publish/tutorial/go/index.html?rev=1508204&r1=1508203&r2=1508204&view=diff
==============================================================================
--- thrift/site/publish/tutorial/go/index.html (original)
+++ thrift/site/publish/tutorial/go/index.html Mon Jul 29 21:26:03 2013
@@ -87,14 +87,252 @@
<h3>Prerequisites</h3>
+<ul>
+<li>At least Go 1.1.x is required to run the tutorial code. </li>
+<li>The GOPATH may need to be adjusted, alternatively manually put the Go
Thrift library files into a suitable location. </li>
+</ul>
+
<h3>Client</h3>
-<div class="CodeRay"><div class="code"><pre><code
class="language-c"></code></pre></div></div>
+<p>Implements the client code which consumes the tutorial service.</p>
+
+<div class="CodeRay"><div class="code"><pre><code class="language-c">import (
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">fmt</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span
style="color:#D20">git.apache.org/thrift.git/lib/go/thrift</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">tutorial</span><span
style="color:#710">"</span></span>
+)
+
+func handleClient(client *tutorial.CalculatorClient) (err error) {
+ client.Ping()
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">ping()</span><span
style="color:#710">"</span></span>)
+
+ sum, _ := client.Add(<span style="color:#00D">1</span>, <span
style="color:#00D">1</span>)
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">1+1=</span><span
style="color:#710">"</span></span>, sum, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#b0b">\n</span><span
style="color:#710">"</span></span>)
+
+ work := tutorial.NewWork()
+ work.Op = tutorial.Operation_DIVIDE
+ work.Num1 = <span style="color:#00D">1</span>
+ work.Num2 = <span style="color:#00D">0</span>
+ quotient, ouch, err := client.Calculate(<span style="color:#00D">1</span>,
work)
+ <span style="color:#080;font-weight:bold">if</span> err != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Error during
operation:</span><span style="color:#710">"</span></span>, err)
+ <span style="color:#080;font-weight:bold">return</span> err
+ } <span style="color:#080;font-weight:bold">else</span> <span
style="color:#080;font-weight:bold">if</span> ouch != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Invalid
operation:</span><span style="color:#710">"</span></span>, ouch)
+ } <span style="color:#080;font-weight:bold">else</span> {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Whoa we can divide by 0
with new value:</span><span style="color:#710">"</span></span>, quotient)
+ }
+
+ work.Op = tutorial.Operation_SUBTRACT
+ work.Num1 = <span style="color:#00D">15</span>
+ work.Num2 = <span style="color:#00D">10</span>
+ diff, ouch, err := client.Calculate(<span style="color:#00D">1</span>,
work)
+ <span style="color:#080;font-weight:bold">if</span> err != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Error during
operation:</span><span style="color:#710">"</span></span>, err)
+ <span style="color:#080;font-weight:bold">return</span> err
+ } <span style="color:#080;font-weight:bold">else</span> <span
style="color:#080;font-weight:bold">if</span> ouch != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Invalid
operation:</span><span style="color:#710">"</span></span>, ouch)
+ } <span style="color:#080;font-weight:bold">else</span> {
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">15-10=</span><span
style="color:#710">"</span></span>, diff, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#b0b">\n</span><span
style="color:#710">"</span></span>)
+ }
+
+ log, err := client.GetStruct(<span style="color:#00D">1</span>)
+ <span style="color:#080;font-weight:bold">if</span> err != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Unable to get
struct:</span><span style="color:#710">"</span></span>, err)
+ <span style="color:#080;font-weight:bold">return</span> err
+ } <span style="color:#080;font-weight:bold">else</span> {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Check log:</span><span
style="color:#710">"</span></span>, log.Value)
+ }
+ <span style="color:#080;font-weight:bold">return</span> err
+}
+
+func runClient(transportFactory thrift.TTransportFactory, protocolFactory
thrift.TProtocolFactory, addr string) error {
+ var transport thrift.TTransport
+ transport, err := thrift.NewTSocket(addr)
+ <span style="color:#080;font-weight:bold">if</span> err != nil {
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Error opening
socket:</span><span style="color:#710">"</span></span>, err)
+ <span style="color:#080;font-weight:bold">return</span> err
+ }
+ transport = transportFactory.GetTransport(transport)
+ defer transport.Close()
+ <span style="color:#080;font-weight:bold">if</span> err :=
transport.Open(); err != nil {
+ <span style="color:#080;font-weight:bold">return</span> err
+ }
+ <span style="color:#080;font-weight:bold">return</span>
handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
+}</code></pre></div></div>
<h3>Server</h3>
+<p>Implements a simple socket server.</p>
+
+<div class="CodeRay"><div class="code"><pre><code class="language-c">import (
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">fmt</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span
style="color:#D20">git.apache.org/thrift.git/lib/go/thrift</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">tutorial</span><span
style="color:#710">"</span></span>
+)
+
+func runServer(transportFactory thrift.TTransportFactory, protocolFactory
thrift.TProtocolFactory, addr string) error {
+ transport, err := thrift.NewTServerSocket(addr)
+ <span style="color:#080;font-weight:bold">if</span> err != nil {
+ <span style="color:#080;font-weight:bold">return</span> err
+ }
+ handler := NewCalculatorHandler()
+ processor := tutorial.NewCalculatorProcessor(handler)
+ server := thrift.NewTSimpleServer4(processor, transport, transportFactory,
protocolFactory)
+
+ fmt.Println(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Starting the simple
server... on </span><span style="color:#710">"</span></span>, transport.Addr())
+ <span style="color:#080;font-weight:bold">return</span> server.Serve()
+}</code></pre></div></div>
+
+<h3>Handler</h3>
+
+<p>The handler implements the service defined in the tutorial IDL file and
exposed by the server.</p>
+
+<div class="CodeRay"><div class="code"><pre><code class="language-c">import (
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">fmt</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">shared</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">strconv</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">tutorial</span><span
style="color:#710">"</span></span>
+)
+
+type CalculatorHandler <span style="color:#080;font-weight:bold">struct</span>
{
+ log map[<span
style="color:#0a5;font-weight:bold">int</span>]*shared.SharedStruct
+}
+
+func NewCalculatorHandler() *CalculatorHandler {
+ <span style="color:#080;font-weight:bold">return</span>
&CalculatorHandler{<span style="color:#970;font-weight:bold">log:</span>
make(map[<span
style="color:#0a5;font-weight:bold">int</span>]*shared.SharedStruct)}
+}
+
+func (p *CalculatorHandler) Ping() (err error) {
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">ping()</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ <span style="color:#080;font-weight:bold">return</span> nil
+}
+
+func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err
error) {
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">add(</span><span
style="color:#710">"</span></span>, num1, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">,</span><span
style="color:#710">"</span></span>, num2, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">)</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ <span style="color:#080;font-weight:bold">return</span> num1 + num2, nil
+}
+
+func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val
int32, ouch *tutorial.InvalidOperation, err error) {
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">calculate(</span><span
style="color:#710">"</span></span>, logid, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">, {</span><span
style="color:#710">"</span></span>, w.Op, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">,</span><span
style="color:#710">"</span></span>, w.Num1, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">,</span><span
style="color:#710">"</span></span>, w.Num2, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">})</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ <span style="color:#080;font-weight:bold">switch</span> w.Op {
+ <span style="color:#080;font-weight:bold">case</span>
tutorial.Operation_ADD:
+ val = w.Num1 + w.Num2
+ <span style="color:#080;font-weight:bold">break</span>
+ <span style="color:#080;font-weight:bold">case</span>
tutorial.Operation_SUBTRACT:
+ val = w.Num1 - w.Num2
+ <span style="color:#080;font-weight:bold">break</span>
+ <span style="color:#080;font-weight:bold">case</span>
tutorial.Operation_MULTIPLY:
+ val = w.Num1 * w.Num2
+ <span style="color:#080;font-weight:bold">break</span>
+ <span style="color:#080;font-weight:bold">case</span>
tutorial.Operation_DIVIDE:
+ <span style="color:#080;font-weight:bold">if</span> w.Num2 == <span
style="color:#00D">0</span> {
+ ouch = tutorial.NewInvalidOperation()
+ ouch.What = int32(w.Op)
+ ouch.Why = <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Cannot divide by
0</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">return</span>
+ }
+ val = w.Num1 / w.Num2
+ <span style="color:#080;font-weight:bold">break</span>
+ <span style="color:#080;font-weight:bold">default</span>:
+ ouch = tutorial.NewInvalidOperation()
+ ouch.What = int32(w.Op)
+ ouch.Why = <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Unknown
operation</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">return</span>
+ }
+ entry := shared.NewSharedStruct()
+ entry.Key = logid
+ entry.Value = strconv.Itoa(<span
style="color:#0a5;font-weight:bold">int</span>(val))
+ k := <span style="color:#0a5;font-weight:bold">int</span>(logid)
+
+ p.log[k] = entry
+ <span style="color:#080;font-weight:bold">return</span> val, ouch, err
+}
+
+func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, error)
{
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">getStruct(</span><span
style="color:#710">"</span></span>, key, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">)</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ v, _ := p.log[<span style="color:#0a5;font-weight:bold">int</span>(key)]
+ <span style="color:#080;font-weight:bold">return</span> v, nil
+}
+
+func (p *CalculatorHandler) Zip() (err error) {
+ fmt.Print(<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">zip()</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ <span style="color:#080;font-weight:bold">return</span> nil
+}</code></pre></div></div>
+
+<h3>Main code</h3>
+
+<p>The main code is shared between server and client. The <code>-server</code>
argument distinguishes betweeen the two modes.</p>
+
+<div class="CodeRay"><div class="code"><pre><code class="language-c">import (
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">flag</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">fmt</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span
style="color:#D20">git.apache.org/thrift.git/lib/go/thrift</span><span
style="color:#710">"</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">os</span><span
style="color:#710">"</span></span>
+)
+
+func Usage() {
+ fmt.Fprint(os.Stderr, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Usage of </span><span
style="color:#710">"</span></span>, os.Args[<span style="color:#00D">0</span>],
<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">:</span><span
style="color:#b0b">\n</span><span style="color:#710">"</span></span>)
+ flag.PrintDefaults()
+ fmt.Fprint(os.Stderr, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#b0b">\n</span><span
style="color:#710">"</span></span>)
+}
+
+func main() {
+ flag.Usage = Usage
+ server := flag.Bool(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">server</span><span
style="color:#710">"</span></span>, <span style="color:#069">false</span>,
<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Run server</span><span
style="color:#710">"</span></span>)
+ protocol := flag.String(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">P</span><span
style="color:#710">"</span></span>, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">binary</span><span
style="color:#710">"</span></span>, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Specify the protocol
(binary, compact, simplejson)</span><span style="color:#710">"</span></span>)
+ framed := flag.Bool(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">framed</span><span
style="color:#710">"</span></span>, <span style="color:#069">false</span>,
<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Use framed
transport</span><span style="color:#710">"</span></span>)
+ buffered := flag.Bool(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">buffered</span><span
style="color:#710">"</span></span>, <span style="color:#069">false</span>,
<span style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Use buffered
transport</span><span style="color:#710">"</span></span>)
+ addr := flag.String(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">addr</span><span
style="color:#710">"</span></span>, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">localhost:9090</span><span
style="color:#710">"</span></span>, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Address to listen
to</span><span style="color:#710">"</span></span>)
+
+ flag.Parse()
+
+ var protocolFactory thrift.TProtocolFactory
+ <span style="color:#080;font-weight:bold">switch</span> *protocol {
+ <span style="color:#080;font-weight:bold">case</span> <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">compact</span><span
style="color:#710">"</span></span>:
+ protocolFactory = thrift.NewTCompactProtocolFactory()
+ <span style="color:#080;font-weight:bold">case</span> <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">simplejson</span><span
style="color:#710">"</span></span>:
+ protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
+ <span style="color:#080;font-weight:bold">case</span> <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">json</span><span
style="color:#710">"</span></span>:
+ protocolFactory = thrift.NewTJSONProtocolFactory()
+ <span style="color:#080;font-weight:bold">case</span> <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">binary</span><span
style="color:#710">"</span></span>, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#710">"</span></span>:
+ protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ <span style="color:#080;font-weight:bold">default</span>:
+ fmt.Fprint(os.Stderr, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">Invalid protocol
specified</span><span style="color:#710">"</span></span>, protocol, <span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#b0b">\n</span><span
style="color:#710">"</span></span>)
+ Usage()
+ os.Exit(<span style="color:#00D">1</span>)
+ }
+
+ var transportFactory thrift.TTransportFactory
+ <span style="color:#080;font-weight:bold">if</span> *buffered {
+ transportFactory = thrift.NewTBufferedTransportFactory(<span
style="color:#00D">8192</span>)
+ } <span style="color:#080;font-weight:bold">else</span> {
+ transportFactory = thrift.NewTTransportFactory()
+ }
+
+ <span style="color:#080;font-weight:bold">if</span> *framed {
+ transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
+ }
+
+ <span style="color:#080;font-weight:bold">if</span> *server {
+ <span style="color:#080;font-weight:bold">if</span> err :=
runServer(transportFactory, protocolFactory, *addr); err != nil {
+ fmt.Println(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">error running
server:</span><span style="color:#710">"</span></span>, err)
+ }
+ } <span style="color:#080;font-weight:bold">else</span> {
+ <span style="color:#080;font-weight:bold">if</span> err :=
runClient(transportFactory, protocolFactory, *addr); err != nil {
+ fmt.Println(<span
style="background-color:hsla(0,100%,50%,0.05)"><span
style="color:#710">"</span><span style="color:#D20">error running
client:</span><span style="color:#710">"</span></span>, err)
+ }
+ }
+}</code></pre></div></div>
+
<h2>Additional Information</h2>
+<ul>
+<li>Try using one of the other available protocols, default is binary.</li>
+<li>Try using the buffered and/or framed transport options.</li>
+<li>Note that both server and client must use the exact same protocol and
transport stack.</li>
+</ul>
+
</div>
<div class="container">
<hr>