Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
package org.apache.thrift.server;
21
 
22
import org.apache.thrift.TException;
23
import org.apache.thrift.TProcessor;
24
import org.apache.thrift.TProcessorFactory;
25
import org.apache.thrift.protocol.TProtocol;
26
import org.apache.thrift.protocol.TProtocolFactory;
27
import org.apache.thrift.transport.TServerTransport;
28
import org.apache.thrift.transport.TTransport;
29
import org.apache.thrift.transport.TTransportFactory;
30
import org.apache.thrift.transport.TTransportException;
31
 
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
 
35
/**
36
 * Simple singlethreaded server for testing.
37
 *
38
 */
39
public class TSimpleServer extends TServer {
40
 
41
  private static final Logger LOGGER = LoggerFactory.getLogger(TSimpleServer.class.getName());
42
 
43
  private boolean stopped_ = false;
44
 
45
  public TSimpleServer(TProcessor processor,
46
                       TServerTransport serverTransport) {
47
    super(new TProcessorFactory(processor), serverTransport);
48
  }
49
 
50
  public TSimpleServer(TProcessor processor,
51
                       TServerTransport serverTransport,
52
                       TTransportFactory transportFactory,
53
                       TProtocolFactory protocolFactory) {
54
    super(new TProcessorFactory(processor), serverTransport, transportFactory, protocolFactory);
55
  }
56
 
57
  public TSimpleServer(TProcessor processor,
58
                       TServerTransport serverTransport,
59
                       TTransportFactory inputTransportFactory,
60
                       TTransportFactory outputTransportFactory,
61
                       TProtocolFactory inputProtocolFactory,
62
                       TProtocolFactory outputProtocolFactory) {
63
    super(new TProcessorFactory(processor), serverTransport,
64
          inputTransportFactory, outputTransportFactory,
65
          inputProtocolFactory, outputProtocolFactory);
66
  }
67
 
68
  public TSimpleServer(TProcessorFactory processorFactory,
69
          TServerTransport serverTransport) {
70
    super(processorFactory, serverTransport);
71
  }
72
 
73
  public TSimpleServer(TProcessorFactory processorFactory,
74
          TServerTransport serverTransport,
75
          TTransportFactory transportFactory,
76
          TProtocolFactory protocolFactory) {
77
    super(processorFactory, serverTransport, transportFactory, protocolFactory);
78
  }
79
 
80
  public TSimpleServer(TProcessorFactory processorFactory,
81
          TServerTransport serverTransport,
82
          TTransportFactory inputTransportFactory,
83
          TTransportFactory outputTransportFactory,
84
          TProtocolFactory inputProtocolFactory,
85
          TProtocolFactory outputProtocolFactory) {
86
    super(processorFactory, serverTransport,
87
          inputTransportFactory, outputTransportFactory,
88
          inputProtocolFactory, outputProtocolFactory);
89
  }
90
 
91
 
92
  public void serve() {
93
    stopped_ = false;
94
    try {
95
      serverTransport_.listen();
96
    } catch (TTransportException ttx) {
97
      LOGGER.error("Error occurred during listening.", ttx);
98
      return;
99
    }
100
 
101
    while (!stopped_) {
102
      TTransport client = null;
103
      TProcessor processor = null;
104
      TTransport inputTransport = null;
105
      TTransport outputTransport = null;
106
      TProtocol inputProtocol = null;
107
      TProtocol outputProtocol = null;
108
      try {
109
        client = serverTransport_.accept();
110
        if (client != null) {
111
          processor = processorFactory_.getProcessor(client);
112
          inputTransport = inputTransportFactory_.getTransport(client);
113
          outputTransport = outputTransportFactory_.getTransport(client);
114
          inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
115
          outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
116
          while (processor.process(inputProtocol, outputProtocol)) {}
117
        }
118
      } catch (TTransportException ttx) {
119
        // Client died, just move on
120
      } catch (TException tx) {
121
        if (!stopped_) {
122
          LOGGER.error("Thrift error occurred during processing of message.", tx);
123
        }
124
      } catch (Exception x) {
125
        if (!stopped_) {
126
          LOGGER.error("Error occurred during processing of message.", x);
127
        }
128
      }
129
 
130
      if (inputTransport != null) {
131
        inputTransport.close();
132
      }
133
 
134
      if (outputTransport != null) {
135
        outputTransport.close();
136
      }
137
 
138
    }
139
  }
140
 
141
  public void stop() {
142
    stopped_ = true;
143
    serverTransport_.interrupt();
144
  }
145
}