From bf062f053de339b660155f4da04fb5328910cbc9 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 7 Sep 2017 17:46:15 +0200
Subject: [PATCH] [enh] #1084 WS2P: add a BMA URL /network/ws2p/info

---
 app/modules/bma/lib/bma.ts                 |  1 +
 app/modules/bma/lib/controllers/network.ts | 19 ++++++++++++++++++-
 app/modules/bma/lib/dtos.ts                |  7 +++++++
 server.ts                                  |  2 ++
 4 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/app/modules/bma/lib/bma.ts b/app/modules/bma/lib/bma.ts
index 6569d3f60..d3e84395f 100644
--- a/app/modules/bma/lib/bma.ts
+++ b/app/modules/bma/lib/bma.ts
@@ -70,6 +70,7 @@ export const bma = function(server:Server, interfaces:NetworkInterface[], httpLo
     httpMethods.httpGET(  '/network/peering/peers',                 (req:any) => net.peersGet(req),                         BMALimitation.limitAsVeryHighUsage());
     httpMethods.httpPOST( '/network/peering/peers',                 (req:any) => net.peersPost(req),                        BMALimitation.limitAsHighUsage());
     httpMethods.httpGET(  '/network/peers',                         (req:any) => net.peers(),                               BMALimitation.limitAsHighUsage());
+    httpMethods.httpGET(  '/network/ws2p/info',                     (req:any) => net.ws2pInfo(),                            BMALimitation.limitAsHighUsage());
     httpMethods.httpPOST( '/wot/add',                               (req:any) => wot.add(req),                              BMALimitation.limitAsHighUsage());
     httpMethods.httpPOST( '/wot/certify',                           (req:any) => wot.certify(req),                          BMALimitation.limitAsHighUsage());
     httpMethods.httpPOST( '/wot/revoke',                            (req:any) => wot.revoke(req),                           BMALimitation.limitAsHighUsage());
diff --git a/app/modules/bma/lib/controllers/network.ts b/app/modules/bma/lib/controllers/network.ts
index 96069b9b1..da9f7cb09 100644
--- a/app/modules/bma/lib/controllers/network.ts
+++ b/app/modules/bma/lib/controllers/network.ts
@@ -1,6 +1,7 @@
 import {AbstractController} from "./AbstractController";
 import {BMAConstants} from "../constants";
-import {HttpMerkleOfPeers, HttpPeer, HttpPeers} from "../dtos";
+import {HttpMerkleOfPeers, HttpPeer, HttpPeers, HttpWS2PInfo} from "../dtos";
+import {WS2PDependency} from "../../../ws2p/index"
 
 const _                = require('underscore');
 const http2raw         = require('../http2raw');
@@ -64,4 +65,20 @@ export class NetworkBinding extends AbstractController {
       })
     };
   }
+
+  async ws2pInfo(): Promise<HttpWS2PInfo> {
+    const cluster = this.server.ws2pCluster
+    let level1 = 0
+    let level2 = 0
+    if (cluster) {
+      level1 = await cluster.clientsCount()
+      level2 = await cluster.servedCount()
+    }
+    return {
+      peers: {
+        level1,
+        level2
+      }
+    };
+  }
 }
diff --git a/app/modules/bma/lib/dtos.ts b/app/modules/bma/lib/dtos.ts
index 6695086a6..dc13172fc 100644
--- a/app/modules/bma/lib/dtos.ts
+++ b/app/modules/bma/lib/dtos.ts
@@ -395,6 +395,13 @@ export interface HttpPeers {
   peers: DBPeer2[]
 }
 
+export interface HttpWS2PInfo {
+  peers: {
+    level1: number,
+    level2: number
+  }
+}
+
 export const MerkleOfPeers = {
   "depth": Number,
   "nodesCount": Number,
diff --git a/server.ts b/server.ts
index b012cd4ee..259276f1b 100644
--- a/server.ts
+++ b/server.ts
@@ -22,6 +22,7 @@ import {RevocationDTO} from "./app/lib/dto/RevocationDTO"
 import {TransactionDTO} from "./app/lib/dto/TransactionDTO"
 import {PeerDTO} from "./app/lib/dto/PeerDTO"
 import {OtherConstants} from "./app/lib/other_constants"
+import {WS2PCluster} from "./app/modules/ws2p/lib/WS2PCluster"
 
 export interface HookableServer {
   generatorGetJoinData: (...args:any[]) => Promise<any>
@@ -48,6 +49,7 @@ export class Server extends stream.Duplex implements HookableServer {
   private paramsP:Promise<any>|null
   private endpointsDefinitions:(()=>Promise<string>)[] = []
   private wrongEndpointsFilters:((endpoints:string[])=>Promise<string[]>)[] = []
+  ws2pCluster:WS2PCluster|undefined
   conf:ConfDTO
   dal:FileDAL
 
-- 
GitLab