Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 1 | package main; |
| 2 | |
| 3 | |
| 4 | /* |
| 5 | * Licensed to the Apache Software Foundation (ASF) under one |
| 6 | * or more contributor license agreements. See the NOTICE file |
| 7 | * distributed with this work for additional information |
| 8 | * regarding copyright ownership. The ASF licenses this file |
| 9 | * to you under the Apache License, Version 2.0 (the |
| 10 | * "License"); you may not use this file except in compliance |
| 11 | * with the License. You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, |
| 16 | * software distributed under the License is distributed on an |
| 17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | * KIND, either express or implied. See the License for the |
| 19 | * specific language governing permissions and limitations |
| 20 | * under the License. |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "net" |
| 27 | "thrift" |
| 28 | "thriftlib/tutorial" |
| 29 | ) |
| 30 | |
| 31 | |
| 32 | type GoServer struct { |
| 33 | handler tutorial.ICalculator |
| 34 | processor *tutorial.CalculatorProcessor |
| 35 | } |
| 36 | |
| 37 | func NewGoServer() *GoServer { |
| 38 | handler := NewCalculatorHandler() |
| 39 | processor := tutorial.NewCalculatorProcessor(handler) |
| 40 | return &GoServer{handler:handler, processor:processor} |
| 41 | } |
| 42 | |
| 43 | func Simple(processor *tutorial.CalculatorProcessor, transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, ch chan int) { |
| 44 | addr, err := net.ResolveTCPAddr("localhost:9090") |
| 45 | if err != nil { |
| 46 | fmt.Print("Error resolving address: ", err.String(), "\n") |
| 47 | return |
| 48 | } |
| 49 | serverTransport, err := thrift.NewTServerSocketAddr(addr) |
| 50 | if err != nil { |
| 51 | fmt.Print("Error creating server socket: ", err.String(), "\n") |
| 52 | return |
| 53 | } |
| 54 | server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) |
| 55 | // Use this for a multithreaded server |
| 56 | // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)); |
| 57 | |
| 58 | fmt.Print("Starting the simple server... on ", addr, "\n") |
| 59 | for { |
| 60 | err = server.Serve() |
| 61 | if err != nil { |
| 62 | fmt.Print("Error during simple server: ", err.String(), "\n") |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | fmt.Print("Done with the simple server\n") |
| 67 | ch <- 1 |
| 68 | } |
| 69 | |
| 70 | func Secure(processor *tutorial.CalculatorProcessor) { |
| 71 | addr, _ := net.ResolveTCPAddr("localhost:9091") |
| 72 | serverTransport, _ := thrift.NewTNonblockingServerSocketAddr(addr) |
| 73 | server := thrift.NewTSimpleServer2(processor, serverTransport) |
| 74 | fmt.Print("Starting the secure server... on ", addr, "\n") |
| 75 | server.Serve() |
| 76 | fmt.Print("Done with the secure server\n") |
| 77 | } |
| 78 | |
| 79 | func RunServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) { |
| 80 | server := NewGoServer() |
| 81 | ch := make(chan int) |
| 82 | go Simple(server.processor, transportFactory, protocolFactory, ch) |
| 83 | //go Secure(server.processor) |
| 84 | _ = <- ch |
| 85 | } |