blob: f70a2a9554a727babdfb4def072e27aa7ed77af5 [file] [log] [blame]
Christian Lavoieafc6d8f2011-02-20 02:39:19 +00001package 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
24import (
25 "fmt"
26 "net"
27 "thrift"
28 "thriftlib/tutorial"
29)
30
31
32type GoServer struct {
33 handler tutorial.ICalculator
34 processor *tutorial.CalculatorProcessor
35}
36
37func NewGoServer() *GoServer {
38 handler := NewCalculatorHandler()
39 processor := tutorial.NewCalculatorProcessor(handler)
40 return &GoServer{handler:handler, processor:processor}
41}
42
43func 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
70func 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
79func 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}