Skip to content

Commit

Permalink
[CE-398] Add socket.io communicate with server
Browse files Browse the repository at this point in the history
In user dashboard can get message through socket.io.
When chain is instantiated, will response message through socket.

Change-Id: Id75e714460f33aa60566be9d828efe70fe86bcf8
Signed-off-by: Haitao Yue <hightall@me.com>
  • Loading branch information
hightall committed Jun 28, 2018
1 parent 64ee19a commit 6b238b4
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 15 deletions.
29 changes: 29 additions & 0 deletions user-dashboard/src/app/assets/src/models/global.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
SPDX-License-Identifier: Apache-2.0
*/
import io from "socket.io-client";
import { message } from "antd";

export default {
namespace: "global",

Expand Down Expand Up @@ -53,5 +56,31 @@ export default {
}
});
},
socketIO({ dispatch }) {
const socket = io("/");
socket.emit("join", { id: window.id });
socket.on("instantiate-done", response => {
const { data: { payload }, meta } = response;
const { deployId, codeVersion, codeName, chainName } = meta;
let messageType = "success";
let resultStatus = "successfully";
let status = "instantiated";
if (!payload.success) {
messageType = "error";
resultStatus = "failed";
status = "error";
}
message[messageType](
`Instantiate smart contract ${codeName} ${codeVersion} on chain ${chainName} ${resultStatus}.`
);
dispatch({
type: "smartContract/updateDeployStatus",
payload: {
deployId,
status,
},
});
});
},
},
};
15 changes: 15 additions & 0 deletions user-dashboard/src/app/assets/src/models/smartContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,20 @@ export default {
newOperations,
};
},
updateDeployStatus(state, action) {
const { deploys } = state;
const { deployId, status } = action.payload;
deploys.forEach((deploy, index) => {
if (deploy._id === deployId) {
deploy.status = status;
deploys[index] = deploy;
return false;
}
});
return {
...state,
deploys,
};
},
},
};
2 changes: 1 addition & 1 deletion user-dashboard/src/app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

module.exports = {
get helper() {
get fabricHelper() {
return this.app.fabricHelper;
},
get getClientForOrg() {
Expand Down
20 changes: 20 additions & 0 deletions user-dashboard/src/app/extend/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
SPDX-License-Identifier: Apache-2.0
*/
'use strict';

module.exports = {
parseMsg(action, payload = {}, metadata = {}) {
const meta = Object.assign({}, {
timestamp: Date.now(),
}, metadata);

return {
meta,
data: {
action,
payload,
},
};
},
};
21 changes: 21 additions & 0 deletions user-dashboard/src/app/io/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const Controller = require('egg').Controller;

class NspController extends Controller {
async join() {
const { ctx, app } = this;
const message = ctx.args[0] || {};

try {
const { id } = message;
ctx.socket.join(id);
} catch (error) {
app.logger.error(error);
}
}
}

module.exports = NspController;
14 changes: 8 additions & 6 deletions user-dashboard/src/app/lib/fabric/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ module.exports = app => {
operate: app.config.operations.InstantiateCode.key,
});
}
return promiseResults[0];
return {
success: promiseResults[0].status === 'SUCCESS',
};
}
deploy.status = 'error';
deploy.save();
Expand Down Expand Up @@ -677,12 +679,12 @@ module.exports = app => {
success: false,
message: responsePayloads[i].toString('utf8'),
};
} else {
return {
success: true,
result: responsePayloads[i].toString('utf8'),
};
}
return {
success: true,
result: responsePayloads[i].toString('utf8'),
};

}
} else {
app.logger.error('response_payloads is null');
Expand Down
3 changes: 2 additions & 1 deletion user-dashboard/src/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const LocalStrategy = require('passport-local').Strategy;
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller, passport } = app;
const { router, controller, passport, io } = app;
passport.use(new LocalStrategy({
passReqToCallback: true,
}, (req, username, password, done) => {
Expand Down Expand Up @@ -37,6 +37,7 @@ module.exports = app => {
router.post('/login', passport.authenticate('local', { successRedirect: process.env.WEBROOT }));
router.get('/logout', controller.home.logout);
require('./router/api')(app);
io.of('/').route('join', io.controller.home.join);

router.prefix(process.env.WEBROOT);
};
21 changes: 15 additions & 6 deletions user-dashboard/src/app/service/smart_contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class SmartContractService extends Service {
};
}
async deploySmartContractCode(id, chainId, operation) {
const { ctx, config } = this;
const { ctx, config, app } = this;

const { functionName, args, deployId } = ctx.request.body;
const chainRootDir = `${config.dataDir}/${ctx.user.id}/chains/${chainId}`;
Expand All @@ -172,11 +172,20 @@ class SmartContractService extends Service {
switch (operation) {
case 'install':
return await ctx.installSmartContract(network, keyValueStorePath, ['peer1', 'peer2'], ctx.user.id, id, chainId, 'org1');
case 'instantiate':
ctx.instantiateSmartContract(network, keyValueStorePath, config.default.channelName, deployId, functionName, args, 'org1');
return {
success: true,
};
case 'instantiate': {
const deploy = await ctx.model.SmartContractDeploy.findOne({ _id: deployId }).populate('chain smartContract smartContractCode');
const result = await ctx.instantiateSmartContract(network, keyValueStorePath, config.default.channelName, deployId, functionName, args, 'org1');
const nsp = app.io.of('/');
const msg = ctx.helper.parseMsg('instantiate-done', result, {
chainName: deploy.chain.name,
codeName: deploy.smartContract.name,
codeVersion: deploy.smartContractCode.version,
chainId,
deployId,
});
nsp.to(ctx.user.id).emit('instantiate-done', msg);
break;
}
default:
return {
success: false,
Expand Down
9 changes: 9 additions & 0 deletions user-dashboard/src/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ module.exports = appInfo => {
],
},
dataDir: '/opt/data',
io: {
init: { },
namespace: {
'/': {
connectionMiddleware: [],
packetMiddleware: [],
},
},
},
};

// use for cookie sign key, should change to your own and keep security
Expand Down
3 changes: 2 additions & 1 deletion user-dashboard/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"adm-zip": "^0.4.11",
"await-stream-ready": "^1.0.1",
"stream-wormhole": "^1.0.3",
"enum": "^2.5.0"
"enum": "^2.5.0",
"socket.io-client": "^2.1.1"
},
"devDependencies": {
"autod": "^3.0.1",
Expand Down

0 comments on commit 6b238b4

Please sign in to comment.