From 58e33421ad3c2d1ece40cb7fdb8a5b3ddf1be2df Mon Sep 17 00:00:00 2001 From: snoop Date: Mon, 30 Oct 2017 19:22:36 +0900 Subject: [PATCH] Discovery ing all --- .../discovery/react/components/Discovery.tsx | 4 +- .../@overflow/discovery/redux/reducer/ing.ts | 78 ++++++++++++++----- 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/ts/@overflow/discovery/react/components/Discovery.tsx b/src/ts/@overflow/discovery/react/components/Discovery.tsx index e83516a..d22bf78 100644 --- a/src/ts/@overflow/discovery/react/components/Discovery.tsx +++ b/src/ts/@overflow/discovery/react/components/Discovery.tsx @@ -89,11 +89,11 @@ export class Discovery extends React.Component { public handleTest = () => { let min: number = 1; - let max: number = 2; + let max: number = 3; let idx: number = Math.floor(Math.random() * (max - min + 1) + min); - idx = 3; + switch(idx) { case 1: diff --git a/src/ts/@overflow/discovery/redux/reducer/ing.ts b/src/ts/@overflow/discovery/redux/reducer/ing.ts index 577368a..b3a9b9e 100644 --- a/src/ts/@overflow/discovery/redux/reducer/ing.ts +++ b/src/ts/@overflow/discovery/redux/reducer/ing.ts @@ -19,13 +19,26 @@ const reducer: ReducersMapObject = { action: Action): DiscoveryIngState => { let obj: any = action.payload; + let host: Host = JSON.parse(obj); let hostList: Host[] = null; if(state.hostList === null || state.hostList === undefined) { hostList = new Array(); hostList.push(JSON.parse(obj)); } else { + hostList = _.clone(state.hostList); - hostList.push(JSON.parse(obj)); + + let tHost: Host = null; + for(let h of hostList) { + if(h.ip === host.ip) { + tHost = h; + break; + } + } + + if(tHost === null) { + hostList.push(host); + } } return { @@ -49,36 +62,59 @@ const reducer: ReducersMapObject = { } else { hostList = _.clone(state.hostList); - let port: Port = JSON.parse(obj); - // let host: Host = _.clone(port.host); - let tHost: Host = null; + let tPort: Port = null; + let port: Port = JSON.parse(obj); + for(let h of hostList) { if(h.ip === port.host.ip) { tHost = h; - port.host = h; - if(h.ports === null || h.ports === undefined) { - h.ports = new Array(); + + let chkObj: Object = h; + if(chkObj.hasOwnProperty('ports') === false + || h.ports === null) { + let nPort: Port = _.clone(port); + let nHost: Host = _.clone(h); + nPort.host = nHost; + nHost.ports = new Array(); + nHost.ports.push(nPort); + hostList.push(nHost); + break; + } + + tPort = null; + for(let p of h.ports) { + if(p.portNumber === port.portNumber) { + tPort = p; + break; + } + } + if(tPort === null) { + let nHost: Host = _.clone(h); + let nPort: Port = _.clone(port); + nPort.host = nHost; + + let dIdx: number = hostList.indexOf(h, 0); + if(dIdx > -1) { + hostList.slice(dIdx, 1); + } + nHost.ports.push(nPort); + hostList.push(nHost); } - h.ports.push(port); - break; } } - let dIdx: number = hostList.indexOf(tHost, 0); - if(dIdx > -1) { - hostList.slice(dIdx, 1); - hostList.push(tHost); - } else { - let host: Host = _.clone(port.host); - host.ports = new Array(); - port.host = host; - host.ports.push(port); - hostList.push(host); + if(tHost === null) { + let nHost: Host = _.clone(port.host); + let nPort: Port = _.clone(port); + + nPort.host = nHost; + nHost.ports = new Array(); + nHost.ports.push(nPort); + hostList.push(nHost); } - - } + return { ...state, hostList: hostList,