From 185797aa61efbc171790d2392970fd141e5308b1 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 17 Jun 2019 13:08:58 +0100 Subject: [PATCH 1/2] Added #164 Added NodeHttp --- e2e/infrastructure/NodeHttp.spec.ts | 59 ++++++++++++++++++++++ src/infrastructure/NodeHttp.ts | 74 ++++++++++++++++++++++++++++ src/infrastructure/NodeRepository.ts | 39 +++++++++++++++ src/model/node/NodeInfo.ts | 60 ++++++++++++++++++++++ src/model/node/NodeTime.ts | 36 ++++++++++++++ src/model/node/RoleType.ts | 20 ++++++++ 6 files changed, 288 insertions(+) create mode 100644 e2e/infrastructure/NodeHttp.spec.ts create mode 100644 src/infrastructure/NodeHttp.ts create mode 100644 src/infrastructure/NodeRepository.ts create mode 100644 src/model/node/NodeInfo.ts create mode 100644 src/model/node/NodeTime.ts create mode 100644 src/model/node/RoleType.ts diff --git a/e2e/infrastructure/NodeHttp.spec.ts b/e2e/infrastructure/NodeHttp.spec.ts new file mode 100644 index 0000000000..1e02252c34 --- /dev/null +++ b/e2e/infrastructure/NodeHttp.spec.ts @@ -0,0 +1,59 @@ +/* + * Copyright 2019 NEM + * + * 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 {expect} from 'chai'; +import {NodeHttp} from '../../src/infrastructure/NodeHttp'; +describe('NodeHttp', () => { + let nodeHttp: NodeHttp; + before((done) => { + const path = require('path'); + require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => { + if (err) { + throw err; + } + const json = JSON.parse(data); + nodeHttp = new NodeHttp(json.apiUrl); + done(); + }); + }); + + describe('getNodeInfo', () => { + it('should return node info', (done) => { + nodeHttp.getNodeInfo() + .subscribe((nodeInfo) => { + expect(nodeInfo.friendlyName).not.to.be.undefined; + expect(nodeInfo.host).not.to.be.undefined; + expect(nodeInfo.networkIdentifier).not.to.be.undefined; + expect(nodeInfo.port).not.to.be.undefined; + expect(nodeInfo.publicKey).not.to.be.undefined; + expect(nodeInfo.roles).not.to.be.undefined; + expect(nodeInfo.version).not.to.be.undefined; + done(); + }); + }); + }); + + describe('getNodeTime', () => { + it('should return node time', (done) => { + nodeHttp.getNodeTime() + .subscribe((nodeTime) => { + expect(nodeTime.receiveTimeStamp).not.to.be.undefined; + expect(nodeTime.sendTimeStamp).not.to.be.undefined; + done(); + }); + }); + }); +}); diff --git a/src/infrastructure/NodeHttp.ts b/src/infrastructure/NodeHttp.ts new file mode 100644 index 0000000000..7a29ea3388 --- /dev/null +++ b/src/infrastructure/NodeHttp.ts @@ -0,0 +1,74 @@ +/* + * Copyright 2019 NEM + * + * 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 {from as observableFrom, Observable} from 'rxjs'; +import {map} from 'rxjs/operators'; +import { NodeInfo } from '../model/node/NodeInfo'; +import { NodeTime } from '../model/node/NodeTime'; +import { NodeInfoDTO, NodeRoutesApi, NodeTimeDTO } from './api'; +import {Http} from './Http'; +import {NodeRepository} from './NodeRepository'; + +/** + * Node http repository. + * + * @since 1.0 + */ +export class NodeHttp extends Http implements NodeRepository { + /** + * @internal + * Nem2 Library account routes api + */ + private nodeRoutesApi: NodeRoutesApi; + + /** + * Constructor + * @param url + */ + constructor(url: string) { + super(); + this.nodeRoutesApi = new NodeRoutesApi(url); + + } + + /** + * Supplies additional information about the application running on a node. + * @summary Get the node information + */ + public getNodeInfo(): Observable { + return observableFrom(this.nodeRoutesApi.getNodeInfo()).pipe(map((nodeInfoDTO: NodeInfoDTO) => { + return new NodeInfo( + nodeInfoDTO.publicKey, + nodeInfoDTO.port, + nodeInfoDTO.networkIdentifier, + nodeInfoDTO.version, + nodeInfoDTO.roles as number, + nodeInfoDTO.host, + nodeInfoDTO.friendlyName, + ); + })); + } + + /** + * Gets the node time at the moment the reply was sent and received. + * @summary Get the node time + */ + public getNodeTime(): Observable { + return observableFrom(this.nodeRoutesApi.getNodeTime()).pipe(map((nodeTimeDTO: NodeTimeDTO) => { + return new NodeTime(nodeTimeDTO.communicationTimestamps.sendTimestamp, nodeTimeDTO.communicationTimestamps.receiveTimestamp); + })); + } +} diff --git a/src/infrastructure/NodeRepository.ts b/src/infrastructure/NodeRepository.ts new file mode 100644 index 0000000000..94ab38886c --- /dev/null +++ b/src/infrastructure/NodeRepository.ts @@ -0,0 +1,39 @@ +/* + * Copyright 2019 NEM + * + * 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 {Observable} from 'rxjs'; +import { NodeInfo } from '../model/node/NodeInfo'; +import { NodeTime } from '../model/node/NodeTime'; + +/** + * Node interface repository. + * + * @since 1.0 + */ +export interface NodeRepository { + + /** + * Supplies additional information about the application running on a node. + * @summary Get the node information + */ + getNodeInfo(): Observable; + + /** + * Gets the node time at the moment the reply was sent and received. + * @summary Get the node time + */ + getNodeTime(): Observable; +} diff --git a/src/model/node/NodeInfo.ts b/src/model/node/NodeInfo.ts new file mode 100644 index 0000000000..82947da021 --- /dev/null +++ b/src/model/node/NodeInfo.ts @@ -0,0 +1,60 @@ +/* + * Copyright 2019 NEM + * + * 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 { NetworkType } from '../blockchain/NetworkType'; +import { RoleType } from './RoleType'; +/** + * The node info structure describes basic information of a node. + */ +export class NodeInfo { + + /** + * @param publicKey + * @param port + * @param networkIdentifier + * @param version + * @param roles + * @param host + * @param friendlyName + */ + constructor(/** + * The public key used to identify the node. + */ + public readonly publicKey: string, + /** + * The port used for the communication. + */ + public readonly port: number, + /** + * The network identifier. + */ + public readonly networkIdentifier: NetworkType, + /** + * The version of the application. + */ + public readonly version: number, + /** + * The roles of the application. + */ + public readonly roles: RoleType, + /** + * The IP address of the endpoint. + */ + public readonly host: string, + /** + * The name of the node. + */ + public readonly friendlyName: string ) {} +} diff --git a/src/model/node/NodeTime.ts b/src/model/node/NodeTime.ts new file mode 100644 index 0000000000..208656d64b --- /dev/null +++ b/src/model/node/NodeTime.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2019 NEM + * + * 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 { RoleType } from './RoleType'; +import { NetworkType } from '../blockchain/NetworkType'; +import { UInt64 } from '../UInt64'; +/** + * The node info structure describes basic information of a node. + */ +export class NodeTime { + + /** + * @param sendTimeStamp + * @param receiveTimeStamp + */ + constructor(/** + * The request send timestamp + */ + public readonly sendTimeStamp?: number[], + /** + * The request received timestamp + */ + public readonly receiveTimeStamp?: number[] ) {} +} diff --git a/src/model/node/RoleType.ts b/src/model/node/RoleType.ts new file mode 100644 index 0000000000..a8df636cc0 --- /dev/null +++ b/src/model/node/RoleType.ts @@ -0,0 +1,20 @@ +/* + * Copyright 2019 NEM + * + * 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. + */ + +export enum RoleType { + PeerNode = 1, + ApiNode = 2, +} From b925043ca7d3c712588c62f2c8d9585c41a54ff6 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 17 Jun 2019 13:18:05 +0100 Subject: [PATCH 2/2] Added exports --- src/infrastructure/infrastructure.ts | 1 + src/model/model.ts | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/infrastructure/infrastructure.ts b/src/infrastructure/infrastructure.ts index 93351a18b4..81c18382ac 100644 --- a/src/infrastructure/infrastructure.ts +++ b/src/infrastructure/infrastructure.ts @@ -25,4 +25,5 @@ export * from './TransactionHttp'; export * from './Listener'; export * from './QueryParams'; export * from './NetworkHttp'; +export * from './NodeHttp'; export * from './transaction/NamespaceMosaicIdGenerator'; diff --git a/src/model/model.ts b/src/model/model.ts index 383305be0d..47c8471340 100644 --- a/src/model/model.ts +++ b/src/model/model.ts @@ -69,6 +69,11 @@ export * from './namespace/NamespaceName'; export * from './namespace/NamespaceType'; export * from './namespace/AliasActionType'; +// Node +export * from './node/NodeInfo'; +export * from './node/NodeTime'; +export * from './node/RoleType'; + // Receipt export * from './receipt/ArtifactExpiryReceipt'; export * from './receipt/BalanceChangeReceipt';