Skip to content

Commit

Permalink
Merge 7c3ad4d into 900cf19
Browse files Browse the repository at this point in the history
  • Loading branch information
jessy1092 committed Jul 6, 2016
2 parents 900cf19 + 7c3ad4d commit 5c4fac8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let libPipe = ffi.Library(`${__dirname}/libpipe.dll`, {
module.exports = {

connect(callback) {
libPipe.connect_pipe.async('PIME', callback);
libPipe.connect_pipe.async('node', callback);
},

read(pipe, callback) {
Expand Down
39 changes: 35 additions & 4 deletions src/nimeSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ class NIMESocket extends EventEmitter {
this.service = service;
}

handleMessage(msg) {

switch (msg) {
case 'ping':
this.write('pong');
this.read();
break;
case 'quit':
this.close();
break;
default:
this.service.handleRequest(msg);
this.read();
break;
}
}

read() {
LOG.info('Wait data');
this.pipe.read(this.ref, (err, data) => {
Expand All @@ -28,12 +45,18 @@ class NIMESocket extends EventEmitter {

case SUCCESS:
this.data += data;
this.msg = JSON.parse(this.data);

LOG.info('Get Data: ' + this.data);
try {
this.msg = JSON.parse(this.data);
} catch (e) {
this.msg = this.data;
}

this.data = "";

this.emit('data', this.msg);
this.service.handleRequest(this.msg);
this.read();
this.handleMessage(this.msg);
break;

case ERROR_MORE_DATA:
Expand All @@ -53,7 +76,15 @@ class NIMESocket extends EventEmitter {
}

write(response) {
LOG.info(`Write Data: ${JSON.stringify(response)}`);
let data = '';

try {
data = JSON.stringify(response);
} catch (e) {
data = response;
}

LOG.info(`Write Data: ${data}`);
this.pipe.write(this.ref, response, (err, len) => {
this.emit('drain', len);
});
Expand Down
55 changes: 22 additions & 33 deletions src/textService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TextService extends EventEmitter {
this.env = {};
this.setting = {};
this.handle = false; // Check already write response or not
this.open = true; // Check Service get started
this.open = false; // Check Service get started
}

setSocket(socket) {
Expand All @@ -47,40 +47,34 @@ class TextService extends EventEmitter {
this.setting = JSON.parse(data);

// Store OS env
this.env['id'] = msg['id'];
this.env['isWindows8Above'] = msg['isWindows8Above'];
this.env['isMetroApp'] = msg['isMetroApp'];
this.env['isUiLess'] = msg['isUiLess'];
this.env['isConsole'] = msg['isConsole'];
}

onActivate() {
// initialize process
this.registerLangProfileActivated();
this.registerLangProfileDeactivated();
this.registerDeactivate();

// key event
this.registerKeyEvent();
this.registerEndEvent();
if (this.env['id'].toLowerCase() === this.setting['guid'].toLowerCase()) {
this.open = true;
this.writeSuccess(msg['seqNum']);
} else {
this.open = false;
this.writeFail(msg['seqNum']);
}
}

registerLangProfileActivated() {
this.on('onLangProfileActivated', (msg) => {
LOG.info('onLangProfileActivated');
if (this.setting['guid'] === msg['guid']) {
this.open = true;
} else {
this.open = false;
}
this.emit('end', msg);
});
}
onActivate(msg) {
if (this.open) {
// initialize process
this.registerDeactivate();

registerLangProfileDeactivated() {
this.on('onLangProfileDeactivated', (msg) => {
LOG.info('onLangProfileDeactivated');
this.emit('end', msg);
});
// key event
this.registerKeyEvent();
this.registerEndEvent();

this.writeSuccess(msg['seqNum']);
} else {
this.writeFail(msg['seqNum']);
}
}

registerDeactivate() {
Expand Down Expand Up @@ -135,25 +129,20 @@ class TextService extends EventEmitter {

handleRequest(msg) {
this.handle = false;
// LOG.info(msg);

let method = msg['method'];

switch (method) {

case 'init':
this.init(msg);
this.writeSuccess(msg['seqNum']);
return;

case 'onActivate':
this.onActivate();
this.writeSuccess(msg['seqNum']);
this.onActivate(msg);
return;

case 'onDeactivate':
case 'onLangProfileActivated':
case 'onLangProfileDeactivated':
this.emit(method, msg);
break;

Expand Down
66 changes: 35 additions & 31 deletions tests/textService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

let textService = require('../src/textService');
let nimeSocket = require('../src/nimeSocket');
let fs = require('fs');
let sinon = require('sinon');

describe('Text Service', () => {
Expand Down Expand Up @@ -38,32 +39,6 @@ describe('Text Service', () => {
});
});

describe('#registerLangProfileActivated', () => {

let onSpy;

beforeEach(() => onSpy = sinon.spy(service, 'on'));

it('should register onLangProfileActivated event', () => {
service.registerLangProfileActivated();

assert.equal('onLangProfileActivated', onSpy.getCall(0).args[0]);
});
});

describe('#registerLangProfileDeactivated', () => {

let onSpy;

beforeEach(() => onSpy = sinon.spy(service, 'on'));

it('should register onLangProfileDeactivated event', () => {
service.registerLangProfileDeactivated();

assert.equal('onLangProfileDeactivated', onSpy.getCall(0).args[0]);
});
});

describe('#registerDeactivate', () => {

let onSpy;
Expand Down Expand Up @@ -91,25 +66,53 @@ describe('Text Service', () => {
});

describe('#handleRequest', () => {
let sandbox;

beforeEach(() => {
service.onActivate();
sandbox = sinon.sandbox.create();
service.setting.guid = '{C5F37DA0-274E-4837-9B7C-9BB79FE85D9D}';
});

context('when LangProfileActivated with guid match', () => {
afterEach(() => {
sandbox.restore();
})

context('when init with guid match', () => {
it('should open the service', () => {
let fakeMsg = {"seqNum": 12841, "method": "onLangProfileActivated", "guid": "{C5F37DA0-274E-4837-9B7C-9BB79FE85D9D}"}
let readFileSync = sandbox.stub(fs, 'readFileSync');
readFileSync.returns('{"guid": "{C5F37DA0-274E-4837-9B7C-9BB79FE85D9D}"}');
sandbox.stub(service, 'write');

let fakeMsg = {
"id": "{c5f37da0-274e-4837-9b7c-9bb79fe85d9d}",
"isConsole": false,
"isMetroApp": false,
"isUiLess": false,
"isWindows8Above": false,
"method":"init",
"seqNum":66
}

service.handleRequest(fakeMsg);

assert.equal(true, service.open);
});
});

context('when LangProfileActivated with guid not match', () => {
context('when init with guid not match', () => {
it('should close the service', () => {
let fakeMsg = {"seqNum": 12841, "method": "onLangProfileActivated", "guid": "{123}"}
let readFileSync = sandbox.stub(fs, 'readFileSync');
readFileSync.returns('{"guid": "{C5F37DA0-274E-4837-9B7C-9BB79FE85D9D}"}');
sandbox.stub(service, 'write');

let fakeMsg = {
"id": "{123}",
"isConsole": false,
"isMetroApp": false,
"isUiLess": false,
"isWindows8Above": false,
"method":"init", "seqNum":66
}

service.handleRequest(fakeMsg);

Expand All @@ -124,6 +127,7 @@ describe('Text Service', () => {
beforeEach(() => {
writeSpy = sinon.spy(service, 'write');
service.handle = false;
service.open = true;
});

it('should write the success response', () => {
Expand Down

0 comments on commit 5c4fac8

Please sign in to comment.