Skip to content

Commit b299ef4

Browse files
authored
Example code update to ES6
1 parent 803e1d4 commit b299ef4

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

README.md

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MPI.size()
1515
//returns the size of the cluster (the number of nodes)
1616
```
1717
```js
18-
MPI.recv(msgType,callback)
18+
MPI.recv(msgType, callback)
1919
function callback(data){
2020
}
2121
//starts to listen on all messages with msgType type and invokes callback on each message with the content as first parameter
@@ -64,80 +64,80 @@ Available for node.js version 4.x or upper.
6464

6565
Basic example of 3 phase commit algorithm using mpi-node library:
6666
```js
67-
var MPI = require('mpi-node');
67+
const MPI = require('mpi-node');
6868
MPI.init(main);
69-
function main(){
70-
var state='Begin'
71-
var tid = MPI.rank()
72-
var timeoutTime=5000;
73-
var timeout;
74-
var numOfAnswers=0;
75-
if(tid == 0){
69+
function main () {
70+
const tid = MPI.rank();
71+
const timeoutTime = 5000;
72+
let state = 'Begin';
73+
let numOfAnswers = 0;
74+
let timeout;
75+
if (tid === 0) {
7676
sendCommitRequest();
77-
setInterval(sendCommitRequest,1000);
78-
function sendCommitRequest(){
79-
MPI.broadcast({type: 'CanCommit', content: 'transaction'})
80-
state = 'Waiting'
81-
timeout = setTimeout(function(){
82-
MPI.broadcast({type: 'Abort', content: 'transaction'})
83-
state = 'Aborted'
84-
},timeoutTime)
77+
setInterval(sendCommitRequest, 1000);
78+
function sendCommitRequest () {
79+
MPI.broadcast({type: 'CanCommit', content: 'transaction'});
80+
state = 'Waiting';
81+
timeout = setTimeout(() => {
82+
MPI.broadcast({type: 'Abort', content: 'transaction'});
83+
state = 'Aborted';
84+
}, timeoutTime);
8585
}
86-
MPI.recv('Yes',function(message){
86+
MPI.recv('Yes', (message) => {
8787
numOfAnswers++;
88-
if(numOfAnswers==MPI.size()-1){
89-
numOfAnswers=0;
88+
if(numOfAnswers === MPI.size() - 1){
89+
numOfAnswers = 0;
9090
clearTimeout(timeout);
91-
MPI.broadcast({type: 'preCommit', content: 'transaction'})
92-
state = 'Prepared'
93-
timeout = setTimeout(function(){
94-
MPI.broadcast({type: 'Abort', content: 'transaction'})
95-
state = 'Aborted'
96-
},timeoutTime)
91+
MPI.broadcast({type: 'preCommit', content: 'transaction'});
92+
state = 'Prepared';
93+
timeout = setTimeout(() => {
94+
MPI.broadcast({type: 'Abort', content: 'transaction'});
95+
state = 'Aborted';
96+
}, timeoutTime);
9797
}
9898
})
99-
MPI.recv('No',function(message){
100-
MPI.broadcast({type: 'Abort', content: 'transaction'})
101-
state = 'Aborted'
99+
MPI.recv('No', (message) => {
100+
MPI.broadcast({type: 'Abort', content: 'transaction'});
101+
state = 'Aborted';
102102
})
103-
MPI.recv('ACK',function(message){
103+
MPI.recv('ACK', (message) => {
104104
numOfAnswers++;
105-
if(numOfAnswers==MPI.size()-1){
106-
numOfAnswers=0;
105+
if(numOfAnswers === MPI.size() - 1){
106+
numOfAnswers = 0;
107107
clearTimeout(timeout);
108-
MPI.broadcast({type: 'doCommit', content: 'transaction'})
109-
state = 'Commited'
108+
MPI.broadcast({type: 'doCommit', content: 'transaction'});
109+
state = 'Commited';
110110
}
111111
});
112112
}
113-
else{
114-
MPI.recv('CanCommit',function(message){
115-
timeout = setTimeout(abortHandler,timeoutTime);
116-
MPI.send(0,{type: 'Yes', content: 'transaction'})
113+
else {
114+
MPI.recv('CanCommit', (message) => {
115+
timeout = setTimeout(abortHandler, timeoutTime);
116+
MPI.send(0, {type: 'Yes', content: 'transaction'});
117117
state = 'Waiting';
118118
})
119-
MPI.recv('abort',abortHandler);
120-
MPI.recv('preCommit',function(message){
119+
MPI.recv('abort', abortHandler);
120+
MPI.recv('preCommit', (message) => {
121121
clearTimeout(timeout);
122-
timeout = setTimeout(commitHandler,timeoutTime);
123-
MPI.send(0,{type: 'ACK', content: 'transaction'})
124-
state = 'Prepared'
122+
timeout = setTimeout(commitHandler, timeoutTime);
123+
MPI.send(0, {type: 'ACK', content: 'transaction'});
124+
state = 'Prepared';
125125
})
126-
MPI.recv('doCommit',commitHandler)
126+
MPI.recv('doCommit', commitHandler);
127127
}
128-
function commitHandler(){
129-
if(timeout){
128+
function commitHandler () {
129+
if (timeout) {
130130
clearTimeout(timeout);
131131
}
132-
state='Commited';
132+
state = 'Commited';
133133
}
134134
function abortHandler(message){
135-
if(timeout){
135+
if (timeout) {
136136
clearTimeout(timeout);
137-
timeout=null;
137+
timeout = null;
138138
}
139-
state='aborted';
139+
state = 'Aborted';
140140
}
141141
```
142142
143-
[mpi-node-cli]: <https://www.npmjs.com/package/mpi-node-cli>
143+
[mpi-node-cli]: <https://www.npmjs.com/package/mpi-node-cli>

0 commit comments

Comments
 (0)