THRIFT-625: Add support for 'Go'; provided by Aalok Shah.



git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1072478 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/tutorial/go/src/CalculatorHandler.go b/tutorial/go/src/CalculatorHandler.go
new file mode 100644
index 0000000..9eb2838
--- /dev/null
+++ b/tutorial/go/src/CalculatorHandler.go
@@ -0,0 +1,101 @@
+package main;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+  "fmt"
+  "os"
+  "strconv"
+  "thriftlib/tutorial"
+  "thriftlib/shared"
+)
+
+type CalculatorHandler struct {
+  log map[int]*shared.SharedStruct
+}
+
+func NewCalculatorHandler() *CalculatorHandler {
+  return &CalculatorHandler{log:make(map[int]*shared.SharedStruct)}
+}
+
+func (p *CalculatorHandler) Ping() (err os.Error) {
+  fmt.Print("ping()\n")
+  return nil
+}
+
+func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err os.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 os.Error) {
+  fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
+  switch(w.Op) {
+  case tutorial.ADD:
+    val = w.Num1 + w.Num2
+    break
+  case tutorial.SUBTRACT:
+    val = w.Num1 - w.Num2
+    break
+  case tutorial.MULTIPLY:
+    val = w.Num1 * w.Num2
+    break
+  case tutorial.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)
+  /*
+  oldvalue, exists := p.log[k]
+  if exists {
+    fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
+  } else {
+    fmt.Print("Adding ", entry, " for key ", k, "\n")
+  }
+  */
+  p.log[k] = entry, true
+  return val, ouch, err
+}
+
+func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, os.Error) {
+  fmt.Print("getStruct(", key, ")\n")
+  v, _ := p.log[int(key)]
+  return v, nil
+}
+
+func (p *CalculatorHandler) Zip() (err os.Error) {
+  fmt.Print("zip()\n")
+  return nil
+}
+
diff --git a/tutorial/go/src/GoClient.go b/tutorial/go/src/GoClient.go
new file mode 100644
index 0000000..9440530
--- /dev/null
+++ b/tutorial/go/src/GoClient.go
@@ -0,0 +1,92 @@
+package main;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import (
+  "fmt"
+  "net"
+  "os"
+  "thrift"
+  "thriftlib/tutorial"
+)
+
+func Perform(client *tutorial.CalculatorClient) (err os.Error) {
+  client.Ping()
+  fmt.Print("ping()\n")
+  
+  sum, _ := client.Add(1, 1)
+  fmt.Print("1+1=", sum, "\n")
+  
+  work := tutorial.NewWork()
+  work.Op = tutorial.DIVIDE
+  work.Num1 = 1
+  work.Num2 = 0
+  quotient, ouch, err := client.Calculate(1, work)
+  if err != nil {
+    fmt.Print("Error during operation: ", err.String(), "\n")
+    return err
+  } else if ouch != nil {
+    fmt.Print("Invalid operation: ", ouch.String(), "\n")
+  } else {
+    fmt.Print("Whoa we can divide by 0 with new value: ", quotient, "\n")
+  }
+  
+  work.Op = tutorial.SUBTRACT
+  work.Num1 = 15
+  work.Num2 = 10
+  diff, ouch, err := client.Calculate(1, work)
+  if err != nil {
+    fmt.Print("Error during operation: ", err.String(), "\n")
+    return err
+  } else if ouch != nil {
+    fmt.Print("Invalid operation: ", ouch.String(), "\n")
+  } else {
+    fmt.Print("15-10=", diff, "\n")
+  }
+  
+  log, err := client.GetStruct(1)
+  if err != nil {
+    fmt.Print("Unable to get struct: ", err.String(), "\n")
+    return err
+  } else {
+    fmt.Print("Check log: ", log.Value, "\n")
+  }
+  return err
+}
+
+
+func RunClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) os.Error {
+  addr, err := net.ResolveTCPAddr("localhost:9090")
+  if err != nil {
+    fmt.Print("Error resolving address: ", err.String(), "\n")
+    return err
+  }
+  transport := thrift.NewTSocketAddr(addr)
+  if err = transport.Open(); err != nil {
+    fmt.Print("Error opening connection for protocol ", addr.Network(), " to ", addr.String(), ": ", err.String(), "\n")
+    return err
+  }
+  useTransport := transportFactory.GetTransport(transport)
+  client := tutorial.NewCalculatorClientFactory(useTransport, protocolFactory)
+  Perform(client)
+  return transport.Close()
+}
diff --git a/tutorial/go/src/GoServer.go b/tutorial/go/src/GoServer.go
new file mode 100644
index 0000000..f70a2a9
--- /dev/null
+++ b/tutorial/go/src/GoServer.go
@@ -0,0 +1,85 @@
+package main;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import (
+  "fmt"
+  "net"
+  "thrift"
+  "thriftlib/tutorial"
+)
+
+
+type GoServer struct {
+  handler tutorial.ICalculator
+  processor *tutorial.CalculatorProcessor
+}
+
+func NewGoServer() *GoServer {
+  handler := NewCalculatorHandler()
+  processor := tutorial.NewCalculatorProcessor(handler)
+  return &GoServer{handler:handler, processor:processor}
+}
+
+func Simple(processor *tutorial.CalculatorProcessor, transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, ch chan int) {
+  addr, err := net.ResolveTCPAddr("localhost:9090")
+  if err != nil {
+    fmt.Print("Error resolving address: ", err.String(), "\n")
+    return
+  }
+  serverTransport, err := thrift.NewTServerSocketAddr(addr)
+  if err != nil {
+    fmt.Print("Error creating server socket: ", err.String(), "\n")
+    return
+  }
+  server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
+  // Use this for a multithreaded server
+  // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
+  
+  fmt.Print("Starting the simple server... on ", addr, "\n")
+  for {
+    err = server.Serve()
+    if err != nil {
+      fmt.Print("Error during simple server: ", err.String(), "\n")
+      return
+    }
+  }
+  fmt.Print("Done with the simple server\n")
+  ch <- 1
+}
+
+func Secure(processor *tutorial.CalculatorProcessor) {
+  addr, _ := net.ResolveTCPAddr("localhost:9091")
+  serverTransport, _ := thrift.NewTNonblockingServerSocketAddr(addr)
+  server := thrift.NewTSimpleServer2(processor, serverTransport)
+  fmt.Print("Starting the secure server... on ", addr, "\n")
+  server.Serve()
+  fmt.Print("Done with the secure server\n")
+}
+
+func RunServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) {
+  server := NewGoServer()
+  ch := make(chan int)
+  go Simple(server.processor, transportFactory, protocolFactory, ch)
+  //go Secure(server.processor)
+  _ = <- ch
+}
diff --git a/tutorial/go/src/Makefile b/tutorial/go/src/Makefile
new file mode 100644
index 0000000..87acbca
--- /dev/null
+++ b/tutorial/go/src/Makefile
@@ -0,0 +1,22 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.inc
+
+all: install
+
+TARG=TutorialServerClient
+
+DIRS=\
+
+GOFILES=\
+  CalculatorHandler.go\
+  GoClient.go\
+  GoServer.go\
+  main.go
+
+-include ../Make.deps
+
+include $(GOROOT)/src/Make.cmd
+
diff --git a/tutorial/go/src/main.go b/tutorial/go/src/main.go
new file mode 100644
index 0000000..30b5d52
--- /dev/null
+++ b/tutorial/go/src/main.go
@@ -0,0 +1,86 @@
+package main;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import (
+  "flag"
+  "fmt"
+  "os"
+  "thrift"
+)
+
+func Usage() {
+  fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], " <--server | --client>:\n")
+  flag.PrintDefaults()
+  fmt.Fprint(os.Stderr, "\n")
+  os.Exit(0)
+}
+
+func main() {
+  flag.Usage = Usage
+  var client bool
+  var server bool
+  var protocol string
+  var framed bool
+  var useHttp bool
+  var help bool
+  
+  flag.BoolVar(&client, "client", false, "Run client")
+  flag.BoolVar(&server, "server", false, "Run server")
+  flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson)")
+  flag.BoolVar(&framed, "framed", false, "Use framed transport")
+  flag.BoolVar(&useHttp, "http", false, "Use http")
+  flag.BoolVar(&help, "help", false, "See usage string")
+  flag.Parse()
+  if help || (client && server) || !(client || server) {
+    fmt.Print("flag.NArg() == ", flag.NArg(), "\n")
+    flag.Usage()
+  }
+  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)
+  }
+  transportFactory := thrift.NewTTransportFactory()
+  if framed {
+    transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
+  }
+  
+  if(client) {
+    RunClient(transportFactory, protocolFactory)
+  } else if(server) {
+    RunServer(transportFactory, protocolFactory)
+  } else {
+    flag.Usage()
+  }
+  os.Exit(0)
+}