Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions e2e/infrastructure/NodeHttp.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
74 changes: 74 additions & 0 deletions src/infrastructure/NodeHttp.ts
Original file line number Diff line number Diff line change
@@ -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<NodeInfo> {
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<NodeTime> {
return observableFrom(this.nodeRoutesApi.getNodeTime()).pipe(map((nodeTimeDTO: NodeTimeDTO) => {
return new NodeTime(nodeTimeDTO.communicationTimestamps.sendTimestamp, nodeTimeDTO.communicationTimestamps.receiveTimestamp);
}));
}
}
39 changes: 39 additions & 0 deletions src/infrastructure/NodeRepository.ts
Original file line number Diff line number Diff line change
@@ -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<NodeInfo>;

/**
* Gets the node time at the moment the reply was sent and received.
* @summary Get the node time
*/
getNodeTime(): Observable<NodeTime>;
}
1 change: 1 addition & 0 deletions src/infrastructure/infrastructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export * from './TransactionHttp';
export * from './Listener';
export * from './QueryParams';
export * from './NetworkHttp';
export * from './NodeHttp';
export * from './transaction/NamespaceMosaicIdGenerator';
5 changes: 5 additions & 0 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
60 changes: 60 additions & 0 deletions src/model/node/NodeInfo.ts
Original file line number Diff line number Diff line change
@@ -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 ) {}
}
36 changes: 36 additions & 0 deletions src/model/node/NodeTime.ts
Original file line number Diff line number Diff line change
@@ -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[] ) {}
}
20 changes: 20 additions & 0 deletions src/model/node/RoleType.ts
Original file line number Diff line number Diff line change
@@ -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,
}