Subversion Repositories SmartDukaan

Rev

Rev 3252 | Rev 3915 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 23-Feb-2010

@author: ashish
@author: rajveer
'''
import os
import sys
from getopt import getopt, GetoptError
from shop2020.config.impl.ConfigServerHandler import ConfigServerHandler
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
from shop2020.thriftpy.config import Configuration
from thrift.transport import TSocket, TTransport
from thrift.server import TServer
def main(argv):
        
    try:
        opts, args = getopt(argv,"p:f:e:", ["port=", "file=", "env="])
    except GetoptError:
        usage()
        sys.exit(2)

    try:
        environment = os.environ["ENV_TYPE"]
    except KeyError:
        environment = "DEV"
    
    cfg_file_name = os.path.dirname(os.path.realpath(__file__)) + '/resources/shop2020.cfg'
    
    host = "localhost"
    port = 9999
    for opt,arg in opts:
        if opt in ('-p', "--port"):
            port = arg;
        elif opt in ('-f', "--file"):
            cfg_file_name = arg
        elif opt in ('-e', "--env"):
            environment = arg
    
    start_server(cfg_file_name, host, port, environment)
    
def start_server(filename, host, port, environment):
    handler = ConfigServerHandler(filename, environment)
    processor = Configuration.Processor(handler)
    transport = TSocket.TServerSocket(host, port)
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocolFactory()
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
    print "Starting config server with config params, port "+ str(port)+" file "+ filename
    server.serve()
    print "Server functioning"
    
def usage():
    print 'Usage: ConfigServer.py -p <port_to_run> -f <config_file_path> -e <environment> or ConfigServer.py --port=<port_to_run> --file=<config_file_path> --env=<environment>' 

if __name__ == '__main__':
    main(sys.argv[1:])