Exemple #1
0
def main():

    # start = [Process, Consumer, Producer]
    config = {
        "process_port": "10001",
        "consumer_port": "10002",
        "server": "*",
        "start": "Process"
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v

    cs = config["start"]
    if cs == "Process":
        consumer = capnp.TwoPartyClient(
            "localhost:" + config["consumer_port"]).bootstrap().cast_as(
                fbp_capnp.FBP.Input)
        server = capnp.TwoPartyServer(config["server"] + ":" +
                                      config["process_port"],
                                      bootstrap=Process(consumer))
        server.run_forever()
    elif cs == "Consumer":
        server = capnp.TwoPartyServer(config["server"] + ":" +
                                      config["consumer_port"],
                                      bootstrap=Consumer())
        server.run_forever()
    elif cs == "Producer":
        produce(config)
Exemple #2
0
def main():
    args = parse_args()
    address = args.address
    model_path = args.modelpath
    # print(model_path)
    if args.framework == "spark":
        server = capnp.TwoPartyServer(address,
                                      bootstrap=PySparkFeatureImpl(model_path))
    elif args.framework == "sklearn":
        server = capnp.TwoPartyServer(address,
                                      bootstrap=ScikitFeatureImpl(model_path))
    else:
        print("%s is unsupported framework" % args.system)
        return
    server.run_forever()
Exemple #3
0
def main():
    args = parse_args()
    address = args.address
    # model_path = args.modelpath
    # print(model_path)
    server = capnp.TwoPartyServer(address, bootstrap=CaffeFeatureImpl())
    server.run_forever()
def main():
    address = parse_args().address

    server = capnp.TwoPartyServer(address, bootstrap=CalculatorImpl())
    while True:
        server.poll_once()
        time.sleep(0.001)
Exemple #5
0
def no_async_main(server="*",
                  port=6003,
                  id=None,
                  name="registry name",
                  description=None):
    config = {
        "port": str(port),
        "server": server,
        "id": id,
        "name": name,
        "description": description
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v

    server = capnp.TwoPartyServer(config["server"] + ":" + config["port"],
                                  bootstrap=Registry(
                                      id=config["id"],
                                      name=config["name"],
                                      description=config["description"]))
    server.run_forever()
Exemple #6
0
def main(server="*",
         port=13001,
         path_to_monica_parameters="../monica-parameters",
         id=None,
         name="MONICA Parameters Fertilizer Service",
         description=None):

    config = {
        "port": str(port),
        "server": server,
        "id": id,
        "name": name,
        "description": description,
        "path_to_min_ferts_dir":
        path_to_monica_parameters + "/mineral-fertilisers",
        "path_to_org_ferts_dir":
        path_to_monica_parameters + "/organic-fertilisers"
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print(config)

    server = capnp.TwoPartyServer(config["server"] + ":" + config["port"],
                                  bootstrap=Service(
                                      config["path_to_min_ferts_dir"],
                                      config["path_to_org_ferts_dir"],
                                      id=config["id"],
                                      name=config["name"],
                                      description=config["description"]))
    server.run_forever()
    async def handle_connection(self, reader, writer):
        # Start TwoPartyServer using TwoWayPipe (only requires bootstrap)
        
        #with capnp.TwoPartyServer(bootstrap=self._service) as ss:
        #    self.server = ss
        self.server = capnp.TwoPartyServer(bootstrap=self._service)
        #self._disconnect_prom = self.server.on_disconnect().then(lambda: print("disconnected"))
        self.reader = reader
        self.writer = writer
        self.retry = True

        # Assemble reader and writer tasks, run in the background
        coroutines = [self.socket_reader(), self.socket_writer()]
        tasks = asyncio.gather(*coroutines, return_exceptions=True)

        while True:
            self.server.poll_once()
            # Check to see if reader has been sent an eof (disconnect)
            if self.reader.at_eof():
                self.retry = False
                break
            await asyncio.sleep(0.01)

        # Make wait for reader/writer to finish (prevent possible resource leaks)
        await tasks
Exemple #8
0
def main():
    #address = parse_args().address

    runtime_available = False
    while not runtime_available:
        try:
            # runtime = capnp.TwoPartyClient("localhost:9000").bootstrap().cast_as(
            runtime = capnp.TwoPartyClient(
                "10.10.24.186:9000").bootstrap().cast_as(
                    cluster_admin_service_capnp.Cluster.Runtime)
            runtime_available = True
        except:
            # time.sleep(1)
            pass

    monicaFactory = SlurmMonicaInstanceFactory({
        "port":
        10000,
        "path_to_monica_binaries":
        "C:/Users/berg.ZALF-AD/GitHub/monica/_cmake_vs2019_win64/Debug/"
    })
    registered_factory = False
    while not registered_factory:
        try:
            unreg = runtime.registerModelInstanceFactory(
                "monica_v2.1", monicaFactory).wait().unregister
            registered_factory = True
        except capnp.KjException as e:
            print(e)
            time.sleep(1)

    #server = capnp.TwoPartyServer("*:8000", bootstrap=DataServiceImpl("/home/berg/archive/data/"))
    server = capnp.TwoPartyServer("*:10000", bootstrap=monicaFactory)
    server.run_forever()
Exemple #9
0
def x():
    s = capnp.TwoPartyServer("*:11002",
                             bootstrap=csv_based.TimeSeries.from_csv_file(
                                 "data/climate/climate-iso.csv",
                                 header_map={},
                                 pandas_csv_config={}))
    s.run_forever()
    s._decref()
def main(host, port):
    sock = socket.socket()
    sock.bind((host, int(port)))
    sock.listen()

    print("Starting server...")
    while True:
        conn, _ = sock.accept()
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        conn.setblocking(False)
        server = capnp.TwoPartyServer(conn, bootstrap=Server())
        server.on_disconnect().wait()
        conn.close()
Exemple #11
0
def test_simple_rpc_with_options():
    read, write = socket.socketpair()

    _ = capnp.TwoPartyServer(write, bootstrap=Server())
    # This traversal limit is too low to receive the response in, so we expect
    # an exception during the call.
    client = capnp.TwoPartyClient(read, traversal_limit_in_words=1)

    with pytest.raises(capnp.KjException):
        cap = client.bootstrap().cast_as(test_capability_capnp.TestInterface)

        remote = cap.foo(i=5)
        _ = remote.wait()
Exemple #12
0
def test_simple_rpc_bootstrap():
    read, write = socket.socketpair()

    _ = capnp.TwoPartyServer(write, bootstrap=Server(100))
    client = capnp.TwoPartyClient(read)

    cap = client.bootstrap()
    cap = cap.cast_as(test_capability_capnp.TestInterface)

    remote = cap.foo(i=5)
    response = remote.wait()

    assert response.x == "125"
Exemple #13
0
def test_simple_rpc_restore_func():
    read, write = socket.socketpair(socket.AF_UNIX)

    server = capnp.TwoPartyServer(write, restore_func)
    client = capnp.TwoPartyClient(read)

    ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
    cap = client.restore(ref)
    cap = cap.cast_as(test_capability_capnp.TestInterface)

    remote = cap.foo(i=5)
    response = remote.wait()

    assert response.x == '125'
Exemple #14
0
def main(server="*", port=11002):

    config = {"port": str(port), "server": server}
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print(config)

    server = capnp.TwoPartyServer(config["server"] + ":" + config["port"],
                                  bootstrap=A_Impl())
    server.run_forever()
def main(serve_bootstrap=True, host="*", port=10000, reg_sturdy_ref=None):

    config = {
        "port": port,
        "host": host,
        "reg_sturdy_ref": reg_sturdy_ref,
        "serve_bootstrap": str(serve_bootstrap),
        "reg_category": "climate",
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print(config)

    conMan = common.ConnectionManager()

    service = Factory()
    admin = serv.Admin(service)
    service.admin = admin
    restorer = common.Restorer()

    if config["reg_sturdy_ref"]:
        registrator = conMan.try_connect(config["reg_sturdy_ref"],
                                         cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = registrator.register(
                ref=service, categoryId=config["reg_category"]).wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    addr = config["host"] + (
        (":" + str(config["port"])) if config["port"] else "")
    if config["serve_bootstrap"].lower() == "true":
        server = capnp.TwoPartyServer(addr, bootstrap=restorer)
        restorer.port = port if port else server.port
        admin_sr = restorer.save(admin)
        service_sr = restorer.save(service)
        print("admin_sr:", admin_sr)
        print("service_sr:", service_sr)
        print("restorer_sr:", restorer.sturdy_ref())
    else:
        capnp.wait_forever()
    server.run_forever()
Exemple #16
0
def main(bind, profile):
    """
    Runs the server bound to the\
    given address/port ADDRESS may be '*' to bind to all local addresses.\
    :PORT may be omitted to choose a port automatically.
    """

    JWT_SIGNING_KEY = """-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAES5X8XrfKdx9gYayFITc89wad4usrk0n2
7MjiGYvqalizeSWTHEpnd7oea9IQ8T5oJjMVH5cc0H5tFSKilFFeh//wngxIyny6
6+Vq5t5B0V0Ehy01+2ceEon2Y0XDkIKv
-----END PUBLIC KEY-----"""

    server = capnp.TwoPartyServer(bind, bootstrap=ServerImpl(JWT_SIGNING_KEY))
    print("server start on {}".format(bind))
    server.run_forever()
def test_simple_rpc_with_options():
    read, write = socket.socketpair(socket.AF_UNIX)

    restorer = SimpleRestorer()
    server = capnp.TwoPartyServer(write, restorer)
    # This traversal limit is too low to receive the response in, so we expect
    # an exception during the call.
    client = capnp.TwoPartyClient(read, traversal_limit_in_words=1)

    ref = test_capability_capnp.TestSturdyRefObjectId.new_message(
        tag='testInterface')
    cap = client.restore(ref)
    cap = cap.cast_as(test_capability_capnp.TestInterface)

    remote = cap.foo(i=5)
    with pytest.raises(capnp.KjException):
        response = remote.wait()
def main():
    config = {"port": "6666"}
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v

    print("config:", config)

    #address = parse_args().address

    #server = capnp.TwoPartyServer("*:8000", bootstrap=DataServiceImpl("/home/berg/archive/data/"))
    # UserMasterImpl(AdminMasterImpl()))
    server = capnp.TwoPartyServer("*:" + config["port"],
                                  bootstrap=AdminMasterImpl())
    server.run_forever()
def test_calculator_gc():
    def new_evaluate_impl(old_evaluate_impl):
        def call(*args, **kwargs):
            gc.collect()
            return old_evaluate_impl(*args, **kwargs)

        return call

    read, write = socket.socketpair(socket.AF_UNIX)

    # inject a gc.collect to the beginning of every evaluate_impl call
    evaluate_impl_orig = calculator_server.evaluate_impl
    calculator_server.evaluate_impl = new_evaluate_impl(evaluate_impl_orig)

    server = capnp.TwoPartyServer(write, calculator_server.restore)
    calculator_client.main(read)

    calculator_server.evaluate_impl = evaluate_impl_orig
def main(host, port, server_cert, server_key):
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain(server_cert, server_key)
    sock = socket.socket()
    sock.bind((host, int(port)))
    sock.listen()

    print("Starting server...")
    while True:
        conn, _ = sock.accept()
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        try:
            ssl_conn = context.wrap_socket(conn, server_side=True)
            ssl_conn.setblocking(False)
            server = capnp.TwoPartyServer(ssl_conn, bootstrap=Server())
            server.on_disconnect().wait()
            ssl_conn.close()
        except ssl.SSLError as e:
            print(e)
    async def myserver(self, reader, writer):
        # Start TwoPartyServer using TwoWayPipe (only requires bootstrap)
        self.server = capnp.TwoPartyServer(bootstrap=CalculatorImpl())
        self.reader = reader
        self.writer = writer
        self.retry = True

        # Assemble reader and writer tasks, run in the background
        coroutines = [self.myreader(), self.mywriter()]
        tasks = asyncio.gather(*coroutines, return_exceptions=True)

        while True:
            self.server.poll_once()
            # Check to see if reader has been sent an eof (disconnect)
            if self.reader.at_eof():
                self.retry = False
                break
            await asyncio.sleep(0.01)

        # Make wait for reader/writer to finish (prevent possible resource leaks)
        await tasks
Exemple #22
0
def test_ez_rpc():
    read, write = socket.socketpair(socket.AF_UNIX)

    server = capnp.TwoPartyServer(write, text_restore_func)
    client = capnp.TwoPartyClient(read)

    cap = client.ez_restore('testInterface')
    cap = cap.cast_as(test_capability_capnp.TestInterface)

    remote = cap.foo(i=5)
    response = remote.wait()

    assert response.x == '125'

    cap = client.restore(test_capability_capnp.TestSturdyRefObjectId.new_message())
    cap = cap.cast_as(test_capability_capnp.TestInterface)

    remote = cap.foo(i=5)

    with pytest.raises(capnp.KjException):
        response = remote.wait()
Exemple #23
0
def main():
    config = {"admin_master_address": "localhost:8000"}
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v

    print("config:", config)

    #address = parse_args().address

    master_available = False
    while not master_available:
        try:
            admin_master = capnp.TwoPartyClient(
                config["admin_master_address"]).bootstrap().cast_as(
                    cluster_admin_service_capnp.Cluster.AdminMaster)
            master_available = True
        except:
            # time.sleep(1)
            pass

    #runtime = SlurmRuntime(cores=4, admin_master=admin_master)
    #registered_ = False
    # while not registered_factory:
    #    try:
    #        runtime.registerModelInstanceFactory("monica_v2.1", monicaFactory).wait()
    #        registered_factory = True
    #    except:
    #        time.sleep(1)
    #        pass

    #server = capnp.TwoPartyServer("*:8000", bootstrap=DataServiceImpl("/home/berg/archive/data/"))
    server = capnp.TwoPartyServer("*:9000",
                                  bootstrap=SlurmRuntime(
                                      cores=4, admin_master=admin_master))
    server.run_forever()
given address/port ADDRESS may be '*' to bind to all local addresses.\
:PORT may be omitted to choose a port automatically. ''')

    parser.add_argument("address", type=str, help="ADDRESS[:PORT]")
    parser.add_argument("numtrees", type=int, help="number of trees")
    # parser.add_argument("framework", type=str, help="spark|sklearn")
    # parser.add_argument("modelpath", help="full path to pickled model file")

    return parser.parse_args()


if __name__ == '__main__':

    args = parse_args()
    address = args.address
    numtrees = args.numtrees

    conf = SparkConf() \
            .setAppName("crankshaw-pyspark") \
            .set("spark.executor.memory", "10g") \
            .set("spark.kryoserializer.buffer.mb", "128") \
            .set("master", "local")

    sc = SparkContext(conf=conf, batchSize=1)
    sql = SQLContext(sc)
    rf = train_random_forest(sc, sql, numtrees)

    server = capnp.TwoPartyServer(address,
                                  bootstrap=PySparkMLFeatureImpl(sc, sql, rf))
    server.run_forever()
    'c': {
        'k': c2k,
        'f': c2f
    },
}


def restore(ref):
    assert ref.as_text() == 'tempConv'
    return TempConv()


class TempConv(tempconv_capnp.TempConv.Server):
    def convert(self, temp, target_unit, **kwargs):
        temp_dict = temp.to_dict()
        
        result = tempconv_capnp.Temperature.new_message()
        result.unit = target_unit
        result.value = CONVERTER[temp_dict['unit']][str(target_unit)](temp_dict['value'])

        return result


if __name__ == '__main__':
    s = socket()
    s.bind(('127.0.0.1', 12345))
    s.listen(1)
    conn, addr = s.accept()
    
    server = capnp.TwoPartyServer(conn, bootstrap=TempConv())
    server.on_disconnect().wait()
Exemple #26
0
def main():
    address = parse_args().address

    server = capnp.TwoPartyServer(address, bootstrap=CalculatorImpl())
    server.run_forever()
def test_calculator():
    read, write = socket.socketpair(socket.AF_UNIX)

    server = capnp.TwoPartyServer(write,
                                  bootstrap=calculator_server.CalculatorImpl())
    calculator_client.main(read)
Exemple #28
0
 def run_server():
     capnp.create_event_loop()
     server = capnp.TwoPartyServer(b, bootstrap=func())
     server.on_disconnect().wait()
Exemple #29
0
        if(
            client in [user.client for user in self.clients.values()] or
            not self.validate_nickname(name)
        ):
            return False
        return True

    def validate_nickname(self, name):
        if not name or name in [user.name for user in self.clients.values()]:
            return False
        return True

    def login(self, client_handle, name, login):
        client = Client(login.client, name, client_handle)
        self.clients[login.client] = client

        return ChatServer(client=client, server=self), LoginHandle(login=login)

    def logout(self, client):
        client_handle = self.clients[client]
        for room in client_handle.joined_rooms:
            room.users.remove(client_handle)
        del self.clients[client]


if __name__ == '__main__':
    print('Listening on %s' % SERVER_ADDRESS)
    server = CapnChat()
    capnp_server = capnp.TwoPartyServer(SERVER_ADDRESS, bootstrap=Login)
    capnp_server.run_forever()
Exemple #30
0
 def run_server():
     _ = capnp.TwoPartyServer(write, bootstrap=Server())
     capnp.wait_forever()