Skip to content

Commit

Permalink
Cleaning up cosmetic defects gofmt can fix.
Browse files Browse the repository at this point in the history
See earlier commit for rationale.
  • Loading branch information
matttproud committed Aug 12, 2012
1 parent 117c4fc commit d674426
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 216 deletions.
112 changes: 56 additions & 56 deletions tutorial/go/src/CalculatorHandler.go
Expand Up @@ -20,81 +20,81 @@ package main
*/

import (
"fmt"
"os"
"strconv"
"thriftlib/tutorial"
"thriftlib/shared"
"fmt"
"os"
"strconv"
"thriftlib/shared"
"thriftlib/tutorial"
)

type CalculatorHandler struct {
log map[int]*shared.SharedStruct
log map[int]*shared.SharedStruct
}

func NewCalculatorHandler() *CalculatorHandler {
return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
}

func (p *CalculatorHandler) Ping() (err os.Error) {
fmt.Print("ping()\n")
return nil
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
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
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
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
fmt.Print("zip()\n")
return nil
}
115 changes: 56 additions & 59 deletions tutorial/go/src/GoClient.go
@@ -1,6 +1,5 @@
package main


/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -20,73 +19,71 @@ package main
* under the License.
*/


import (
"fmt"
"net"
"os"
"thrift"
"thriftlib/tutorial"
"fmt"
"net"
"os"
"thrift"
"thriftlib/tutorial"
)

func Perform(client *tutorial.CalculatorClient) (err os.Error) {
client.Ping()
fmt.Print("ping()\n")
client.Ping()
fmt.Print("ping()\n")

sum, _ := client.Add(1, 1)
fmt.Print("1+1=", sum, "\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 := 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")
}
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
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("tcp", "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()
addr, err := net.ResolveTCPAddr("tcp", "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()
}
89 changes: 43 additions & 46 deletions tutorial/go/src/GoServer.go
@@ -1,6 +1,5 @@
package main


/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -20,66 +19,64 @@ package main
* under the License.
*/


import (
"fmt"
"net"
"thrift"
"thriftlib/tutorial"
"fmt"
"net"
"thrift"
"thriftlib/tutorial"
)


type GoServer struct {
handler tutorial.ICalculator
processor *tutorial.CalculatorProcessor
handler tutorial.ICalculator
processor *tutorial.CalculatorProcessor
}

func NewGoServer() *GoServer {
handler := NewCalculatorHandler()
processor := tutorial.NewCalculatorProcessor(handler)
return &GoServer{handler: handler, processor: processor}
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("tcp", "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));
addr, err := net.ResolveTCPAddr("tcp", "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
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("tcp", "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")
addr, _ := net.ResolveTCPAddr("tcp", "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
server := NewGoServer()
ch := make(chan int)
go Simple(server.processor, transportFactory, protocolFactory, ch)
//go Secure(server.processor)
_ = <-ch
}

0 comments on commit d674426

Please sign in to comment.