def main():
    print 'Server Stub PID : ',os.getpid()

    parser = OptionParser()
    parser.add_option('-a','--address',dest='address',default='',type='string',help='Set the ip address for the server to bind with.')
    parser.add_option('-p','--port'   ,dest='port',default=0,type='int',help='Set the port of the server stub.')
    parser.add_option('-l','--live_console'   ,dest='live_console',default =False,action='store_true',help='Activate the live console for the server stub.')
    parser.add_option('-c','--config_file',dest='conf_file',default='./conf',help='Configration file to save commonly used settings.')

    (options,args) = parser.parse_args()

    conf_file = options.conf_file
    if os.path.exists(conf_file):
        conf = cStub.load_settings(conf_file)

        if 'ip'   in conf : ip   = conf['ip']
        if 'port' in conf : port = conf['port']
        if 'live_console' in conf : live_console = conf['live_console']
    
    else:
        ip = options.address
        port = options.port
        live_console = options.live_console

        conf = dict()
        conf['ip'] = ip
        conf['port'] = port 
        conf['live_console'] = live_console

    if ip == '':
        ip   = raw_input('Server IP :')

    if port == 0:
        port = int(raw_input('Port  :'))   

    SStub = ServerStub(conf)
    SStub.start_listening()
    SStub_Th = SStub.get_thread()
    SStub_Th.start()    

    while live_console :

        cmd = raw_input('>')
        place = cmd.find(':')
        data = ''

        if place > -1 :
            data = cmd[place+1:]
            cmd  = cmd[:place] 
                    
        SStub.process(json.dumps({'cmd':cmd,'data':data}))
def main():
    print "Client STUB PID : ", os.getpid()

    parser = OptionParser()
    parser.add_option("-a", "--address", dest="address", default="", type="string", help="Server Stub address.")
    parser.add_option("-p", "--port", dest="port", default=0, type="int", help="Server Stub port.")
    parser.add_option(
        "-q", "--application_port", dest="app_port", default=8765, type="int", help="Application Execution Port."
    )
    parser.add_option(
        "-n",
        "--name",
        dest="name",
        default="",
        type="string",
        help="Client name to be recognized at the Server Stub, note that the name must be unique , or the connection will fail.",
    )
    parser.add_option(
        "-l",
        "--live_console",
        dest="live_console",
        default=False,
        action="store_true",
        help="Activate the live console for the server stub.",
    )
    parser.add_option(
        "-c",
        "--config_file",
        dest="conf_file",
        default="./conf",
        help="Configration file to save commonly used settings.",
    )
    parser.add_option(
        "-d",
        "--reciving_dir",
        dest="reciving_dir",
        default="./",
        help="Directory in which you recive the parsed modules.",
    )

    (options, args) = parser.parse_args()

    conf_file = options.conf_file
    if os.path.exists(conf_file):

        conf = cStub.load_settings(conf_file)

        if "ip" in conf:
            ip = conf["ip"]
        if "port" in conf:
            port = conf["port"]
        if "app_port" in conf:
            app_port = conf["app_port"]
        if "name" in conf:
            name = conf["name"]
        if "live_console" in conf:
            live_console = conf["live_console"]
        if "reciving_dir" in conf:
            reciving_dir = conf["reciving_dir"]

    else:

        ip = options.address
        port = options.port
        app_port = options.app_port
        name = options.name
        live_console = options.live_console
        reciving_dir = options.reciving_dir

        if ip == "":
            ip = raw_input("Server IP :")

        if port == 0:
            port = int(raw_input("Port  :"))

        if name == "":
            name = raw_input("Name:")

        conf = dict()
        conf["ip"] = ip
        conf["port"] = port
        conf["app_port"] = app_port
        conf["name"] = name
        conf["live_console"] = live_console
        conf["reciving_dir"] = reciving_dir

    d = os.path.dirname(reciving_dir)
    if not os.path.exists(d):
        os.makedirs(d)

    stub = ClientStub(conf)
    stub.connect()
    stub.start_listening()
    stub_th = stub.get_thread()
    stub_th.start()

    while live_console:
        cmd = raw_input(">")
        place = cmd.find(":")
        data = ""

        if place > -1:
            data = cmd[place + 1 :]
            cmd = cmd[:place]
        stub.process(json.dumps({"cmd": cmd, "data": data}))