2021-01-19 15:47:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as PeerId from 'peer-id';
|
|
|
|
import Multiaddr from 'multiaddr';
|
|
|
|
import { FluenceConnection } from './FluenceConnection';
|
|
|
|
|
|
|
|
import { ParticleProcessor } from './ParticleProcessor';
|
2021-03-03 22:01:05 +03:00
|
|
|
import { PeerIdB58, SecurityTetraplet } from './commonTypes';
|
|
|
|
import { FluenceClient } from 'src';
|
|
|
|
import { RequestFlow } from './RequestFlow';
|
|
|
|
import { AquaCallHandler, errorHandler, fnHandler } from './AquaHandler';
|
|
|
|
import { loadRelayFn, loadVariablesService } from './RequestFlowBuilder';
|
|
|
|
|
|
|
|
const makeDefaultClientHandler = (): AquaCallHandler => {
|
|
|
|
const res = new AquaCallHandler();
|
|
|
|
res.use(errorHandler);
|
|
|
|
res.use(fnHandler('op', 'identity', (args, _) => args));
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class ClientImpl implements FluenceClient {
|
2021-01-19 15:47:49 +03:00
|
|
|
readonly selfPeerIdFull: PeerId;
|
|
|
|
|
2021-02-25 18:36:10 +03:00
|
|
|
get relayPeerId(): PeerIdB58 | undefined {
|
2021-01-19 15:47:49 +03:00
|
|
|
return this.connection?.nodePeerId.toB58String();
|
|
|
|
}
|
|
|
|
|
|
|
|
get selfPeerId(): PeerIdB58 {
|
|
|
|
return this.selfPeerIdFull.toB58String();
|
|
|
|
}
|
|
|
|
|
|
|
|
get isConnected(): boolean {
|
|
|
|
return this.connection?.isConnected();
|
|
|
|
}
|
|
|
|
|
2021-03-03 22:01:05 +03:00
|
|
|
private connection: FluenceConnection;
|
2021-01-19 15:47:49 +03:00
|
|
|
protected processor: ParticleProcessor;
|
|
|
|
|
|
|
|
constructor(selfPeerIdFull: PeerId) {
|
|
|
|
this.selfPeerIdFull = selfPeerIdFull;
|
2021-03-03 22:01:05 +03:00
|
|
|
this.aquaCallHandler = makeDefaultClientHandler();
|
|
|
|
this.processor = new ParticleProcessor(selfPeerIdFull, this.aquaCallHandler);
|
2021-01-19 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 22:01:05 +03:00
|
|
|
aquaCallHandler: AquaCallHandler;
|
|
|
|
|
2021-01-19 15:47:49 +03:00
|
|
|
async disconnect(): Promise<void> {
|
2021-03-03 22:01:05 +03:00
|
|
|
if (this.connection) {
|
|
|
|
await this.connection.disconnect();
|
|
|
|
}
|
2021-01-19 15:47:49 +03:00
|
|
|
await this.processor.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK:: this is only needed to fix tests.
|
|
|
|
// Particle processor should be tested instead
|
|
|
|
async local(): Promise<void> {
|
|
|
|
await this.processor.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establish a connection to the node. If the connection is already established, disconnect and reregister all services in a new connection.
|
|
|
|
*
|
|
|
|
* @param multiaddr
|
|
|
|
*/
|
|
|
|
async connect(multiaddr: string | Multiaddr): Promise<void> {
|
|
|
|
multiaddr = Multiaddr(multiaddr);
|
|
|
|
|
|
|
|
const nodePeerId = multiaddr.getPeerId();
|
|
|
|
if (!nodePeerId) {
|
|
|
|
throw Error("'multiaddr' did not contain a valid peer id");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.connection) {
|
|
|
|
await this.connection.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = PeerId.createFromB58String(nodePeerId);
|
|
|
|
const connection = new FluenceConnection(
|
|
|
|
multiaddr,
|
|
|
|
node,
|
|
|
|
this.selfPeerIdFull,
|
2021-03-03 22:01:05 +03:00
|
|
|
this.processor.executeIncomingParticle.bind(this.processor),
|
2021-01-19 15:47:49 +03:00
|
|
|
);
|
|
|
|
await connection.connect();
|
|
|
|
this.connection = connection;
|
2021-03-03 22:01:05 +03:00
|
|
|
await this.processor.init(connection);
|
2021-01-19 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 22:01:05 +03:00
|
|
|
async initiateFlow(request: RequestFlow): Promise<void> {
|
|
|
|
// setting `relayVariableName` here. If the client is not connected (i.e it is created as local) then there is no relay
|
|
|
|
request.handler.on(loadVariablesService, loadRelayFn, () => {
|
|
|
|
return this.relayPeerId || '';
|
|
|
|
});
|
|
|
|
await request.initState(this.selfPeerIdFull);
|
|
|
|
this.processor.executeLocalParticle(request);
|
2021-01-19 15:47:49 +03:00
|
|
|
}
|
|
|
|
}
|