};
// SingleShot interface (input & output)
+function checkSingleShotNotClosed(singleshot) {
+ if (singleshot._id === undefined) {
+ throw new WebAPIException(
+ WebAPIException.ABORT_ERR,
+ 'SingleShot object was closed and using it is no longer possible.'
+ );
+ }
+}
+
var ValidInputExceptions = ['TypeMismatchError', 'AbortError'];
var SingleShot = function(id) {
var _input, _output;
Object.defineProperties(this, {
input: {
get: function() {
+ checkSingleShotNotClosed(this);
if (!_input) {
var result = native_.callSync('MLSingleShotGetTensorsInfo', {
id: this._id,
return _input;
},
set: function() {
+ checkSingleShotNotClosed(this);
var args = validator_.validateArgs(arguments, [
{
name: 'inTensorsInfo',
},
output: {
get: function() {
+ checkSingleShotNotClosed(this);
if (!_output) {
var result = native_.callSync('MLSingleShotGetTensorsInfo', {
id: this._id,
/* readonly */
}
},
- _id: { value: id, writable: false, enumerable: false }
+ _id: { value: id, writable: true, enumerable: false }
});
};
];
SingleShot.prototype.invoke = function() {
+ checkSingleShotNotClosed(this);
var args = validator_.validateArgs(arguments, [
{
name: 'inTensorsData',
];
SingleShot.prototype.getValue = function() {
+ checkSingleShotNotClosed(this);
var args = validator_.validateArgs(arguments, [
{
name: 'name',
};
SingleShot.prototype.setValue = function() {
+ checkSingleShotNotClosed(this);
var args = validator_.validateArgs(arguments, [
{
name: 'name',
);
}
};
-// SingleShot::setTimeout()
-// SingleShot::close()
+var SetTimeoutValidExceptions = [
+ 'NotSupportedError',
+ 'AbortError'
+];
+SingleShot.prototype.setTimeout = function() {
+ checkSingleShotNotClosed(this);
+ var args = validator_.validateArgs(arguments, [
+ {
+ name: 'timeout',
+ type: types_.UNSIGNED_LONG
+ }
+ ]);
+
+ if (!args.has.timeout) {
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'Invalid parameter: timeout is undefined'
+ );
+ }
+
+ var callArgs = {
+ id: this._id,
+ timeout: args.timeout
+ };
+
+ var result = native_.callSync('MLSingleShotSetTimeout', callArgs);
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObjectAndValidate(
+ result,
+ SetTimeoutValidExceptions,
+ AbortError
+ );
+ }
+};
+
+var CloseValidExceptions = [
+ 'AbortError'
+];
+SingleShot.prototype.close = function() {
+ checkSingleShotNotClosed(this);
+ var callArgs = {
+ id: this._id
+ };
+
+ var result = native_.callSync('MLSingleShotClose', callArgs);
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObjectAndValidate(
+ result,
+ CloseValidExceptions,
+ AbortError
+ );
+ }
+ this._id = undefined;
+};
REGISTER_METHOD(MLSingleShotInvoke);
REGISTER_METHOD(MLSingleShotGetValue);
REGISTER_METHOD(MLSingleShotSetValue);
- // SingleShot::setTimeout()
- // SingleShot::close()
+ REGISTER_METHOD(MLSingleShotSetTimeout);
+ REGISTER_METHOD(MLSingleShotClose);
// Single API end
// TODO move to the up section with field names
namespace {
+const std::string kTimeout = "timeout";
const std::string kModelPath = "modelPath";
const std::string kInTensorsInfo = "inTensorsInfo";
const std::string kOutTensorsInfo = "outTensorsInfo";
ReportSuccess(out);
}
-// SingleShot::setTimeout()
+void MlInstance::MLSingleShotSetTimeout(const picojson::value& args, picojson::object& out) {
+ ScopeLogger("args: %s", args.serialize().c_str());
+ CHECK_ARGS(args, kId, double, out);
+ CHECK_ARGS(args, kTimeout, double, out);
+
+ auto id = static_cast<int>(args.get(kId).get<double>());
+ auto timeout = static_cast<unsigned long>(args.get(kTimeout).get<double>());
+
+ auto ret = single_manager_.SetTimeout(id, timeout);
+ if (!ret) {
+ ReportError(ret, &out);
+ return;
+ }
+
+ ReportSuccess(out);
+}
-// SingleShot::close()
+void MlInstance::MLSingleShotClose(const picojson::value& args, picojson::object& out) {
+ ScopeLogger("args: %s", args.serialize().c_str());
+ CHECK_ARGS(args, kId, double, out);
+
+ auto id = static_cast<int>(args.get(kId).get<double>());
+
+ auto ret = single_manager_.Close(id);
+ if (!ret) {
+ ReportError(ret, &out);
+ return;
+ }
+
+ ReportSuccess(out);
+}
// Single API end
void MLSingleShotInvoke(const picojson::value& args, picojson::object& out);
void MLSingleShotGetValue(const picojson::value& args, picojson::object& out);
void MLSingleShotSetValue(const picojson::value& args, picojson::object& out);
- // SingleShot::setTimeout()
- // SingleShot::close()
+ void MLSingleShotSetTimeout(const picojson::value& args, picojson::object& out);
+ void MLSingleShotClose(const picojson::value& args, picojson::object& out);
// Single API end
// Pipeline API begin
return single->SetValue(name, value);
}
-// SingleShot::setTimeout()
-// SingleShot::close()
+PlatformResult SingleManager::SetTimeout(int id, unsigned long timeout) {
+ ScopeLogger();
+
+ SingleShot* single = GetSingleShot(id);
+ if (!single) {
+ LoggerE("Could not find SingleShot handle");
+ return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
+ }
+
+ return single->SetTimeout(timeout);
+}
+
+PlatformResult SingleManager::Close(int id) {
+ ScopeLogger();
+
+ SingleShot* single = GetSingleShot(id);
+ if (!single) {
+ LoggerE("Could not find SingleShot handle");
+ return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
+ }
+
+ return single->Close();
+}
} // namespace ml
} // namespace extension
PlatformResult Invoke(int id, TensorsData* in_tensors_data, TensorsData** out_tensors_data);
PlatformResult GetValue(int id, const std::string& name, std::string& value);
PlatformResult SetValue(int id, const std::string& name, const std::string& value);
- // SingleShot::setTimeout()
- // SingleShot::close()
+ PlatformResult SetTimeout(int id, unsigned long timeout);
+ PlatformResult Close(int id);
private:
int nextId_;
return PlatformResult{};
}
-// SingleShot::setTimeout()
+PlatformResult SingleShot::SetTimeout(unsigned long timeout) {
+ ScopeLogger();
+ int ret = ml_single_set_timeout(handle_, timeout);
+ if (ML_ERROR_NONE != ret) {
+ LoggerE("ml_single_set_timeout failed: %d (%s)", ret, get_error_message(ret));
+ return util::ToPlatformResult(ret, "Failed to set timeout");
+ }
-// SingleShot::close()
+ return PlatformResult{};
+}
+
+PlatformResult SingleShot::Close() {
+ ScopeLogger();
+ int ret = ml_single_close(handle_);
+ if (ML_ERROR_NONE != ret) {
+ LoggerE("ml_single_close failed: %d (%s)", ret, get_error_message(ret));
+ return util::ToPlatformResult(ret, "Failed to close");
+ }
+
+ return PlatformResult{};
+}
} // namespace extension
} // namespace ml
PlatformResult CleanUpAfterInvoke();
PlatformResult GetValue(const std::string& name, std::string& value);
PlatformResult SetValue(const std::string& name, const std::string& value);
-
- // SingleShot::setTimeout()
-
- // SingleShot::close()
+ PlatformResult SetTimeout(unsigned long timeout);
+ PlatformResult Close();
private:
PlatformResult InvokeInternal(ml_tensors_data_h in_data, ml_tensors_info_h in_info);