Skip to content

Commit

Permalink
Add cancel-job
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 25, 2024
1 parent 15f3a96 commit 5064b62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
24 changes: 21 additions & 3 deletions lib/ippprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ Error IppPrinter::identify()

Error IppPrinter::setAttributes(List<std::pair<std::string, std::string>> attrStrs)
{
Error err;
Error error;
IppAttrs attrs;
for(const auto& [name, value] : attrStrs)
{
Expand All @@ -384,8 +384,8 @@ Error IppPrinter::setAttributes(List<std::pair<std::string, std::string>> attrSt
}
IppMsg req = _mkMsg(IppMsg::SetPrinterAttrs, {}, {}, attrs);
IppMsg resp;
err = _doRequest(req, resp);
return err;
error = _doRequest(req, resp);
return error;
}

Error IppPrinter::getJobs(List<IppPrinter::JobInfo>& jobInfos)
Expand All @@ -408,6 +408,24 @@ Error IppPrinter::getJobs(List<IppPrinter::JobInfo>& jobInfos)
return error;
}

Error IppPrinter::cancelJob(int jobId)
{
Error error;
IppAttrs cancelJobOpAttrs = {{"job-id", {IppTag::Integer, jobId}}};
IppMsg req = _mkMsg(IppMsg::CancelJob, cancelJobOpAttrs);
IppMsg resp;
error = _doRequest(req, resp);
if(error)
{
return error;
}
if(resp.getStatus() > 0xff)
{
error = resp.getOpAttrs().get<std::string>("status-message", "unknown");
}
return error;
}

Error IppPrinter::_doRequest(IppMsg::Operation op, IppMsg& resp)
{
IppMsg req = _mkMsg(op);
Expand Down
1 change: 1 addition & 0 deletions lib/ippprinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class IppPrinter
Error identify();
Error setAttributes(List<std::pair<std::string, std::string>> attrStrs);
Error getJobs(List<JobInfo>& jobInfos);
Error cancelJob(int jobId);

private:
Error _doRequest(IppMsg::Operation op, IppMsg& resp);
Expand Down
22 changes: 21 additions & 1 deletion utils/ippclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ int main(int argc, char** argv)

bool antiAlias;

int id;

std::string addr;
std::string attrs;
std::string inFile;
Expand Down Expand Up @@ -226,6 +228,8 @@ int main(int argc, char** argv)

SwitchArg<bool> antiAliasOpt(antiAlias, {"-aa", "--antaialias"}, "Enable antialiasing in rasterization");

SwitchArg<int> idOpt(id, {"--id"}, "Id of print job.");

PosArg addrArg(addr, "printer address");
PosArg attrsArg(attrs, "name=value[,name=value]");
PosArg pdfArg(inFile, "input file");
Expand All @@ -240,6 +244,8 @@ int main(int argc, char** argv)
{&addrArg, &attrsArg}}},
{"get-jobs", {{&helpOpt, &verboseOpt},
{&addrArg}}},
{"cancel-job", {{&helpOpt, &verboseOpt, &idOpt},
{&addrArg}}},
{"print", {{&helpOpt, &verboseOpt, &forceOpt, &oneStageOpt,
&pagesOpt, &copiesOpt, &collatedCopiesOpt, &paperSizeOpt,
&resolutionOpt, &resolutionXOpt, &resolutionYOpt,
Expand Down Expand Up @@ -332,11 +338,25 @@ int main(int argc, char** argv)
error = printer.getJobs(jobInfos);
if(error)
{
std::cerr << "Get-jobs attributes failed: " << error.value() << std::endl;
std::cerr << "Get-jobs failed: " << error.value() << std::endl;
return 1;
}
std::cout << jobInfos;
}
else if(args.subCommand() == "cancel-job")
{
if(!idOpt.isSet())
{
std::cerr << "Please specify a job id with --id." << std::endl;
return 1;
}
error = printer.cancelJob(id);
if(error)
{
std::cerr << "Cancel-job failed: " << error.value() << std::endl;
return 1;
}
}
else if(args.subCommand() == "print")
{
IppPrintJob job = printer.createJob();
Expand Down

0 comments on commit 5064b62

Please sign in to comment.