Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions lib/DataQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
const { ProgramCall } = require('./ProgramCall');
const { CommandCall } = require('./CommandCall');
// const { xmlToJson } = require('./utils');

// QRCVDTAQ Parameters
const QRCVDTAQParameters = {
DataQueueName: { io: 'in', type: '10A' },
LibraryName: { io: 'in', type: '10A' },
LengthOfData: { io: 'out', type: '5p0' },
Data: { io: 'out', type: '10A' },
WaitTime: { io: 'in', type: '5p0' }
}
Comment on lines +5 to +12
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interested in what others think about pulling Parameter information out into a structure like this. Might work a little better after the this PR drops: #139


class DataQueue {
/**
* @description Creates a new DataQueue object
* @constructor
* @param {object} connection
* @param {string} name
* @param {string} library
*/
constructor(connection, name, library = '') {
this.connection = connection;

if (name.length > 10) {
throw Error("DataQueue name must be 10 or fewer characters long.");
}

if (library.length > 10) {
throw Error("DataQueue library must be 10 of fewer characers long.");
}

this.name = name;
this.library = library ? library : '*CURLIB';
this.qualifiedName = this.name.padEnd(10, ' ') + this.library.padEnd(10, ' ');
this.maxLength = undefined;
}

//////////////////////////////////////////////////////////////////////////////
// Create Data Queue (CLDTAQ) CL Command
//////////////////////////////////////////////////////////////////////////////
/**
* @description Creates a Data Queue on the system of the connection passed in
* the constructor.
* @constructor
* @param {number} maxLength
* @param {function} callback
*/
async create(maxLength, callback) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these methods be defined as async? I'm thinking this could lead to synchronization issues. Async function return a promise and within the function we call connection.run with is asynchronous function using the callback pattern. So we wouldn't wait for connection.run to complete before returning the promise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this goes with Issue #322 . I have always declared them as async since that's what they are. I wasn't aware the spec said I had to return a Promise, just that "Async functions can contain zero or more await expressions." Because of the latter I don't think using callbacks internally is a big deal. Will have to dwell on it, but maybe is a reason people don't mix callbacks/Promises in the same function

this.maxLength = maxLength;
const commandConfig = {
type: 'cl',
command: `CRTDTAQ DTAQ(${this.library ? `${this.library}/` : ''}${this.name}) MAXLEN(${this.maxLength})`
}

this.connection.add(new CommandCall(commandConfig));
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error);
}

// const result = xmlToJson(xmlOutput);
callback(null, xmlOutput);
});
}

//////////////////////////////////////////////////////////////////////////////
// Delete Data Queue (DLTDTAQ) CL Command
//////////////////////////////////////////////////////////////////////////////
async delete(callback) {
const commandConfig = {
type: 'cl',
command: `DLTDTAQ DTAQ(${this.library ? `${this.library}/`: ''}${this.name})`
}

this.connection.add(new CommandCall(commandConfig));
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error);
}

// const result = xmlToJson(xmlOutput);
callback(null, xmlOutput);
});
}

//////////////////////////////////////////////////////////////////////////////
// Receive Data Queue (QRCVDTAQ) API
//////////////////////////////////////////////////////////////////////////////

async receive(wait, length, callback) {
if (typeof wait === 'function') {
callback = wait;
wait = 0;
}

if (length == undefined) {
if (this.maxLength == undefined) {
const description = await this._getDataQueueDescription();
this.maxLength = description[0].data[2].value;
}

length = this.maxLength;
}

const program = new ProgramCall('QRCVDTAQ', { lib: 'QSYS' });

program.addParam (
{ value: this.name, ...QRCVDTAQParameters.DataQueueName }
);
program.addParam (
{ value: this.library, ...QRCVDTAQParameters.LibraryName }
);
program.addParam (
{ value: length, ...QRCVDTAQParameters.LengthOfData }
);
program.addParam (
{ value: '', ...QRCVDTAQParameters.Data }
);
program.addParam (
{ value: wait, ...QRCVDTAQParameters.WaitTime}
);

this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
return callback(null, xmlOutput);
});
}

// alias for receive, as the API is "RCV", but jt400 uses the term "read"
async read(wait, callback) {
return this.receive(wait, callback);
}

//////////////////////////////////////////////////////////////////////////////
// Send Data Queue (QSNDDTAQ) API
//////////////////////////////////////////////////////////////////////////////

async send(data, callback) {
const program = new ProgramCall('QSNDDTAQ', { lib: 'QSYS' });
program.addParam({value: this.name, type: '10A'}); // Data queue name
program.addParam({value: this.library, type: '10A'}); // Library name
program.addParam({value: data.length, type: '5p0'}); // Length of data
program.addParam({value: data, type: `${data.length}A`}); // Data

this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
// const result = xmlToJson(xmlOutput);
return callback(null, xmlOutput);
});
}

// alias for send, as the API is "SND", but jt400 uses the term "write"
async write(data, callback) {
return this.send(data, callback);
}

//////////////////////////////////////////////////////////////////////////////
// Clear Data Queue (QCLRDTAQ) API
//////////////////////////////////////////////////////////////////////////////

async clear() {
const program = new ProgramCall('QCLRDTAQ', { lib: 'QSYS' });
program.addParam(this.name, '10A'); // Data queue name
program.addParam(this.library, '10A'); // Library name

this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}
// const result = xmlToJson(xmlOutput);
return callback(null, xmlOutput);
});
}

//////////////////////////////////////////////////////////////////////////////
// "Private" Functions
//////////////////////////////////////////////////////////////////////////////


async _getDataQueueDescription(callback) {
const program = new ProgramCall('QMHQRDQD', { lib: 'QSYS' });

const receiverVariable = [
[0, '10i0'],
[0, '10i0'],
[0, '10i0']
];

program.addParam ( // Receiver variable
{ value: receiverVariable, type: 'ds', io: 'out', len: 'varLen' }
Comment thread
markirish marked this conversation as resolved.
Outdated
);
program.addParam ( // Length of receiver variable
{ value: 0, type: '10i0', setlen: 'varLen' }
);
program.addParam ( // Format name
{ value: 'RDQD0100', type: '8A' }
);
program.addParam ( // Qualified data queue name
{ value: this.qualifiedName, type: '20A' }
);

this.connection.add(program)
this.connection.run((error, xmlOutput) => {
if (error) {
return callback(error, null);
}

return callback(null, xmlOutput);
});
Comment on lines +180 to +221
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea here is:

If we create a DataQueue using this API, we know what the max length is. But if we are consuming a DataQueue created elsewhere, we don't know the max size. So when we need to get the length the first time we call an API that requires us to specify the length of the receiver data

}
}

module.exports.DataQueue = DataQueue;
4 changes: 3 additions & 1 deletion lib/itoolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

const { ProgramCall } = require('./ProgramCall');
const { CommandCall } = require('./CommandCall');
const { Connection } = require('./Connection');
const { Connection } = require('./Connection');
const { DataQueue } = require('./DataQueue');

const {
iPgm,
Expand All @@ -44,6 +45,7 @@ module.exports = {
CommandCall,
Connection,
xmlToJson,
DataQueue,
// deprecated exports below, replaced by functionality above
iCmd,
iConn,
Expand Down