diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/jaegertracing/thrift/tutorial/go | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/thrift/tutorial/go')
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/Makefile.am | 59 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/server.crt | 25 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/server.key | 28 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/src/client.go | 108 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/src/handler.go | 102 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/src/main.go | 82 | ||||
-rw-r--r-- | src/jaegertracing/thrift/tutorial/go/src/server.go | 54 |
7 files changed, 458 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/tutorial/go/Makefile.am b/src/jaegertracing/thrift/tutorial/go/Makefile.am new file mode 100644 index 000000000..bd57d656f --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/Makefile.am @@ -0,0 +1,59 @@ +# +# 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. +# + +gen-go/tutorial/calculator.go gen-go/shared/shared_service.go: $(top_srcdir)/tutorial/tutorial.thrift + $(THRIFT) --gen go$(COMPILER_EXTRAFLAG) -r $< + +all-local: gen-go/tutorial/calculator.go + +check: src/github.com/apache/thrift/lib/go/thrift thirdparty-dep + $(THRIFT) -r --gen go$(COMPILER_EXTRAFLAG) $(top_srcdir)/tutorial/tutorial.thrift + cp -r gen-go/* src/ + GOPATH=`pwd` $(GO) build -o go-tutorial ./src + GOPATH=`pwd` $(GO) build -o calculator-remote src/tutorial/calculator-remote/calculator-remote.go + +src/github.com/apache/thrift/lib/go/thrift: + mkdir -p src/github.com/apache/thrift/lib/go + ln -sf $(realpath $(top_srcdir)/lib/go/thrift) src/github.com/apache/thrift/lib/go/thrift + +thirdparty-dep: + +tutorialserver: all + GOPATH=`pwd` $(GO) run src/*.go -server=true + +tutorialclient: all + GOPATH=`pwd` $(GO) run src/*.go + +tutorialsecureserver: all + GOPATH=`pwd` $(GO) run src/*.go -server=true -secure=true + +tutorialsecureclient: all + GOPATH=`pwd` $(GO) run src/*.go -secure=true + +clean-local: + $(RM) -r gen-* src/shared src/tutorial src/git.apache.org go-tutorial calculator-remote + +EXTRA_DIST = \ + src/client.go \ + src/handler.go \ + src/server.go \ + src/main.go \ + server.crt \ + server.key + diff --git a/src/jaegertracing/thrift/tutorial/go/server.crt b/src/jaegertracing/thrift/tutorial/go/server.crt new file mode 100644 index 000000000..8a5ef3c3a --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/server.crt @@ -0,0 +1,25 @@ +-----BEGIN CERTIFICATE----- +MIIENzCCAx+gAwIBAgIJAOYfYfw7NCOcMA0GCSqGSIb3DQEBBQUAMIGxMQswCQYD +VQQGEwJVUzERMA8GA1UECAwITWFyeWxhbmQxFDASBgNVBAcMC0ZvcmVzdCBIaWxs +MScwJQYDVQQKDB5UaGUgQXBhY2h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tl4/JQ++CW8mKj1meTgo5YFxcZYm +T9vsI3C+Nzn84DINgI9mx6yktIt3QOKZRDpzyPkUzxsyJ8J427DaimDrjTR+fTwD +1Dh09xeeMnSa5zeV1HEDyJTqCXutLetwQ/IyfmMBhIx+nvB5f67pz/m+Dv6V0r3I +p4HCcdnDUDGJbfqtoqsAATQQWO+WWuswB6mOhDbvPTxhRpZq6AkgWqv4S+u3M2GO +r5p9FrBgavAw5bKO54C0oQKpN/5fta5l6Ws0 +-----END CERTIFICATE----- diff --git a/src/jaegertracing/thrift/tutorial/go/server.key b/src/jaegertracing/thrift/tutorial/go/server.key new file mode 100644 index 000000000..263cfce59 --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/server.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCqE9TE9wEXp5LR +tLQVDSGQGV78+7ZtP/I/ZaJ6Q6ZGlfxDFvZjFF73seNhAvlKlYm/jflIHYLnNOCy +SN8I2Xw6L9MbC+jvwkEKfQo4eDoxZnOZjNF5J1/lZtBeOowMkhhzBMH1Rds351/H +jKNg6ZKg2Cldd0j7HbDtEixOLgLbPRpBcaYrLrNMasf3Hal+x8/b8ue28x93HSQB +GmZmMIUwAinEu/fNP4lLGl/0kZb76TnyRpYSPYojtS6CnkH+QLYnsRREXJYwD1Xk +u62LipkXwCkRTnZ5nUsDMX6FPKgjQFQCWDXG/N096+PRUQAChhrXsJ+gF3NqWtDm +trhVQF4nAgMBAAECggEAW/y52YYW6ypROGbZ94DQpFV0kLO7qT8q0Ksxw5sPNaIt +fEPRIymDa8ikyHWJS5Oxmw84wo5jnJV26jaLmwe2Lupq7Xf1lqej8f5LJtuv7cQR +xfzp1vM65KJFFJHp6WqjGqJ6HSSZOpVDsnQYcXQjQCdpyAmaSWd3p+FqYSZ1mQmD +bFNI7jqpczWSZhTdotQ7p7Hn9TVCehflP3yGIB3bQ+wCcCB85dOBz201L+YgaIck +Sz43A4NvWaQIRLRDw7s9GW4jY5T0Jv282WIeAlVpVxLIwu48r4R4yGTIx9Ydowvq +57+Y5iPPjAXxu0V9t00oS3bYxDaKh2DUfc/5zowq8QKBgQDYNVPXmaG0aIH4vjQ9 +7fRdw/UDkYcQbn6CnglQOu77/S8ogQzpKCVJgJgkZNqOVtQMEPzekGEcLTbje1gU +8Bky2k+PL9UwbFy0emnOVh4rqrNXHsRvJcehNT/PRb5hjF3MUMFV/0iD4b+naFaE +jrSWiZ2ZXj2qfwAK52GFbtOuBQKBgQDJYQuGiY0r22E4waJmCSKczoBT3cwlVzWj +V2ljgA9RHLNTVkvNNYQLGu2qngFrtwpeaSnsMDerVG4wKAQWyCnYzxVrlnC4uDrJ +HXuFEltBWi9Ffbgfsnd3749AT0oBP1NT2tMleguyf5DFgjCR3VRJLdrVaaZ8row/ +LqKcFMqnOwKBgB+OIO99l7E584Y3VG6ZdSneOLtNmRXX2pT7tcZE465ZdHGH7Dd3 +SYHhx9K/+Xn+yDH+pLli/xlarAEldmSP6k2WuTfftlC78AfTOfAId5zN7CDR9791 +Fx67I9X/itq33tS8EIuZl57P6uXm/4GXRloWOa8xpvRkVsBApuYPl8t1AoGATQDS +y2sllDObBXzlgGbV2WgNIgSZ311toTv3jJiXQsjauW8yJRHln+l4H9mzaWDgkiFc +ang1kUoDqF5k0eFQPxtQcYdhKwEnWWfwp33RbzfxA32DPnubuzzbZhfrkHaKgnIW +cyor9uFYlm2l7ODZLfJez2RKyTplXnOSsmQw6akCgYAz3dj9Hskyj+HVJ+ht1OcE +c7ai/ESkSA7Vajp0tjJp0EKjW/zq8DvUSXOtcdnJgkKycFluLwbmnaN4txBds1C1 +Qr8Rt2sUCCBNZe1L6DHe3XBdbkJe9sgZVNTjtUSQrzy8UhvsCqG4YWeCu07Szcbc +rdPUV9/uQkdx8VrShxlD8A== +-----END PRIVATE KEY----- diff --git a/src/jaegertracing/thrift/tutorial/go/src/client.go b/src/jaegertracing/thrift/tutorial/go/src/client.go new file mode 100644 index 000000000..e3ebe00df --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/src/client.go @@ -0,0 +1,108 @@ +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 ( + "context" + "crypto/tls" + "fmt" + "tutorial" + + "github.com/apache/thrift/lib/go/thrift" +) + +var defaultCtx = context.Background() + +func handleClient(client *tutorial.CalculatorClient) (err error) { + client.Ping(defaultCtx) + fmt.Println("ping()") + + sum, _ := client.Add(defaultCtx, 1, 1) + fmt.Print("1+1=", sum, "\n") + + work := tutorial.NewWork() + work.Op = tutorial.Operation_DIVIDE + work.Num1 = 1 + work.Num2 = 0 + quotient, err := client.Calculate(defaultCtx, 1, work) + if err != nil { + switch v := err.(type) { + case *tutorial.InvalidOperation: + fmt.Println("Invalid operation:", v) + default: + fmt.Println("Error during operation:", err) + } + return err + } 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, err := client.Calculate(defaultCtx, 1, work) + if err != nil { + switch v := err.(type) { + case *tutorial.InvalidOperation: + fmt.Println("Invalid operation:", v) + default: + fmt.Println("Error during operation:", err) + } + return err + } else { + fmt.Print("15-10=", diff, "\n") + } + + log, err := client.GetStruct(defaultCtx, 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, secure bool) error { + var transport thrift.TTransport + var err error + if secure { + cfg := new(tls.Config) + cfg.InsecureSkipVerify = true + transport, err = thrift.NewTSSLSocket(addr, cfg) + } else { + transport, err = thrift.NewTSocket(addr) + } + if err != nil { + fmt.Println("Error opening socket:", err) + return err + } + transport, err = transportFactory.GetTransport(transport) + if err != nil { + return err + } + defer transport.Close() + if err := transport.Open(); err != nil { + return err + } + iprot := protocolFactory.GetProtocol(transport) + oprot := protocolFactory.GetProtocol(transport) + return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot))) +} diff --git a/src/jaegertracing/thrift/tutorial/go/src/handler.go b/src/jaegertracing/thrift/tutorial/go/src/handler.go new file mode 100644 index 000000000..5c0eed006 --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/src/handler.go @@ -0,0 +1,102 @@ +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 ( + "context" + "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(ctx context.Context) (err error) { + fmt.Print("ping()\n") + return nil +} + +func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) { + fmt.Print("add(", num1, ",", num2, ")\n") + return num1 + num2, nil +} + +func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, 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.WhatOp = int32(w.Op) + ouch.Why = "Cannot divide by 0" + err = ouch + return + } + val = w.Num1 / w.Num2 + break + default: + ouch := tutorial.NewInvalidOperation() + ouch.WhatOp = int32(w.Op) + ouch.Why = "Unknown operation" + err = ouch + 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 + return val, err +} + +func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) { + fmt.Print("getStruct(", key, ")\n") + v, _ := p.log[int(key)] + return v, nil +} + +func (p *CalculatorHandler) Zip(ctx context.Context) (err error) { + fmt.Print("zip()\n") + return nil +} diff --git a/src/jaegertracing/thrift/tutorial/go/src/main.go b/src/jaegertracing/thrift/tutorial/go/src/main.go new file mode 100644 index 000000000..7730d7b32 --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/src/main.go @@ -0,0 +1,82 @@ +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" + "github.com/apache/thrift/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, json, 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") + secure := flag.Bool("secure", false, "Use tls secure transport") + + 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, *secure); err != nil { + fmt.Println("error running server:", err) + } + } else { + if err := runClient(transportFactory, protocolFactory, *addr, *secure); err != nil { + fmt.Println("error running client:", err) + } + } +} diff --git a/src/jaegertracing/thrift/tutorial/go/src/server.go b/src/jaegertracing/thrift/tutorial/go/src/server.go new file mode 100644 index 000000000..95708eb87 --- /dev/null +++ b/src/jaegertracing/thrift/tutorial/go/src/server.go @@ -0,0 +1,54 @@ +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 ( + "crypto/tls" + "fmt" + "github.com/apache/thrift/lib/go/thrift" + "tutorial" +) + +func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error { + var transport thrift.TServerTransport + var err error + if secure { + cfg := new(tls.Config) + if cert, err := tls.LoadX509KeyPair("server.crt", "server.key"); err == nil { + cfg.Certificates = append(cfg.Certificates, cert) + } else { + return err + } + transport, err = thrift.NewTSSLServerSocket(addr, cfg) + } else { + transport, err = thrift.NewTServerSocket(addr) + } + + if err != nil { + return err + } + fmt.Printf("%T\n", transport) + handler := NewCalculatorHandler() + processor := tutorial.NewCalculatorProcessor(handler) + server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory) + + fmt.Println("Starting the simple server... on ", addr) + return server.Serve() +} |