comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2817:473bf302d629

C++ wrappers around OrthancPluginJob and OrthancPluginPeers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Sep 2018 15:25:50 +0200
parents 579acc5e5412
children a0b729ac0549
comparison
equal deleted inserted replaced
2816:567d1be0849e 2817:473bf302d629
1148 } 1148 }
1149 } 1149 }
1150 1150
1151 1151
1152 bool RestApiPut(Json::Value& result, 1152 bool RestApiPut(Json::Value& result,
1153 OrthancPluginContext* context, 1153 OrthancPluginContext* context,
1154 const std::string& uri, 1154 const std::string& uri,
1155 const Json::Value& body, 1155 const Json::Value& body,
1156 bool applyPlugins) 1156 bool applyPlugins)
1157 { 1157 {
1158 Json::FastWriter writer; 1158 Json::FastWriter writer;
1159 return RestApiPut(result, context, uri, writer.write(body), applyPlugins); 1159 return RestApiPut(result, context, uri, writer.write(body), applyPlugins);
1160 } 1160 }
1161 1161
1236 (context->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 || 1236 (context->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 ||
1237 aa < 0 || 1237 aa < 0 ||
1238 bb < 0 || 1238 bb < 0 ||
1239 cc < 0) 1239 cc < 0)
1240 { 1240 {
1241 throw false; 1241 return false;
1242 } 1242 }
1243 1243
1244 unsigned int a = static_cast<unsigned int>(aa); 1244 unsigned int a = static_cast<unsigned int>(aa);
1245 unsigned int b = static_cast<unsigned int>(bb); 1245 unsigned int b = static_cast<unsigned int>(bb);
1246 unsigned int c = static_cast<unsigned int>(cc); 1246 unsigned int c = static_cast<unsigned int>(cc);
1281 else 1281 else
1282 { 1282 {
1283 return false; 1283 return false;
1284 } 1284 }
1285 } 1285 }
1286
1287
1288
1289
1290 #if HAS_ORTHANC_PLUGIN_PEERS == 1
1291 OrthancPeers::OrthancPeers(OrthancPluginContext* context) :
1292 context_(context),
1293 peers_(NULL),
1294 timeout_(0)
1295 {
1296 if (context_ == NULL)
1297 {
1298 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer);
1299 }
1300
1301 peers_ = OrthancPluginGetPeers(context_);
1302
1303 if (peers_ == NULL)
1304 {
1305 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1306 }
1307
1308 uint32_t count = OrthancPluginGetPeersCount(context_, peers_);
1309
1310 for (uint32_t i = 0; i < count; i++)
1311 {
1312 const char* name = OrthancPluginGetPeerName(context_, peers_, i);
1313 if (name == NULL)
1314 {
1315 OrthancPluginFreePeers(context_, peers_);
1316 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1317 }
1318
1319 index_[name] = i;
1320 }
1321 }
1322
1323
1324 OrthancPeers::~OrthancPeers()
1325 {
1326 if (peers_ != NULL)
1327 {
1328 OrthancPluginFreePeers(context_, peers_);
1329 }
1330 }
1331
1332
1333 bool OrthancPeers::LookupName(size_t& target,
1334 const std::string& name) const
1335 {
1336 Index::const_iterator found = index_.find(name);
1337
1338 if (found == index_.end())
1339 {
1340 return false;
1341 }
1342 else
1343 {
1344 target = found->second;
1345 return true;
1346 }
1347 }
1348
1349
1350 std::string OrthancPeers::GetPeerName(size_t index) const
1351 {
1352 if (index >= index_.size())
1353 {
1354 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1355 }
1356 else
1357 {
1358 const char* s = OrthancPluginGetPeerName(context_, peers_, static_cast<uint32_t>(index));
1359 if (s == NULL)
1360 {
1361 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1362 }
1363 else
1364 {
1365 return s;
1366 }
1367 }
1368 }
1369
1370
1371 std::string OrthancPeers::GetPeerUrl(size_t index) const
1372 {
1373 if (index >= index_.size())
1374 {
1375 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1376 }
1377 else
1378 {
1379 const char* s = OrthancPluginGetPeerUrl(context_, peers_, static_cast<uint32_t>(index));
1380 if (s == NULL)
1381 {
1382 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1383 }
1384 else
1385 {
1386 return s;
1387 }
1388 }
1389 }
1390
1391
1392 std::string OrthancPeers::GetPeerUrl(const std::string& name) const
1393 {
1394 size_t index;
1395 if (LookupName(index, name))
1396 {
1397 return GetPeerUrl(index);
1398 }
1399 else
1400 {
1401 std::string s = "Inexistent peer: " + name;
1402 OrthancPluginLogError(context_, s.c_str());
1403 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource);
1404 }
1405 }
1406
1407
1408 bool OrthancPeers::DoGet(MemoryBuffer& target,
1409 size_t index,
1410 const std::string& uri) const
1411 {
1412 if (index >= index_.size())
1413 {
1414 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1415 }
1416
1417 OrthancPluginMemoryBuffer answer;
1418 uint16_t status;
1419 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1420 (context_, &answer, NULL, &status, peers_,
1421 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(),
1422 0, NULL, NULL, NULL, 0, timeout_);
1423
1424 if (code == OrthancPluginErrorCode_Success)
1425 {
1426 target.Assign(answer);
1427 return (status == 200);
1428 }
1429 else
1430 {
1431 return false;
1432 }
1433 }
1434
1435
1436 bool OrthancPeers::DoGet(MemoryBuffer& target,
1437 const std::string& name,
1438 const std::string& uri) const
1439 {
1440 size_t index;
1441 return (LookupName(index, name) &&
1442 DoGet(target, index, uri));
1443 }
1444
1445
1446 bool OrthancPeers::DoGet(Json::Value& target,
1447 size_t index,
1448 const std::string& uri) const
1449 {
1450 MemoryBuffer buffer(context_);
1451
1452 if (DoGet(buffer, index, uri))
1453 {
1454 buffer.ToJson(target);
1455 return true;
1456 }
1457 else
1458 {
1459 return false;
1460 }
1461 }
1462
1463
1464 bool OrthancPeers::DoGet(Json::Value& target,
1465 const std::string& name,
1466 const std::string& uri) const
1467 {
1468 MemoryBuffer buffer(context_);
1469
1470 if (DoGet(buffer, name, uri))
1471 {
1472 buffer.ToJson(target);
1473 return true;
1474 }
1475 else
1476 {
1477 return false;
1478 }
1479 }
1480
1481
1482 bool OrthancPeers::DoPost(MemoryBuffer& target,
1483 const std::string& name,
1484 const std::string& uri,
1485 const std::string& body) const
1486 {
1487 size_t index;
1488 return (LookupName(index, name) &&
1489 DoPost(target, index, uri, body));
1490 }
1491
1492
1493 bool OrthancPeers::DoPost(Json::Value& target,
1494 size_t index,
1495 const std::string& uri,
1496 const std::string& body) const
1497 {
1498 MemoryBuffer buffer(context_);
1499
1500 if (DoPost(buffer, index, uri, body))
1501 {
1502 buffer.ToJson(target);
1503 return true;
1504 }
1505 else
1506 {
1507 return false;
1508 }
1509 }
1510
1511
1512 bool OrthancPeers::DoPost(Json::Value& target,
1513 const std::string& name,
1514 const std::string& uri,
1515 const std::string& body) const
1516 {
1517 MemoryBuffer buffer(context_);
1518
1519 if (DoPost(buffer, name, uri, body))
1520 {
1521 buffer.ToJson(target);
1522 return true;
1523 }
1524 else
1525 {
1526 return false;
1527 }
1528 }
1529
1530
1531 bool OrthancPeers::DoPost(MemoryBuffer& target,
1532 size_t index,
1533 const std::string& uri,
1534 const std::string& body) const
1535 {
1536 if (index >= index_.size())
1537 {
1538 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1539 }
1540
1541 OrthancPluginMemoryBuffer answer;
1542 uint16_t status;
1543 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1544 (context_, &answer, NULL, &status, peers_,
1545 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(),
1546 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_);
1547
1548 if (code == OrthancPluginErrorCode_Success)
1549 {
1550 target.Assign(answer);
1551 return (status == 200);
1552 }
1553 else
1554 {
1555 return false;
1556 }
1557 }
1558
1559
1560 bool OrthancPeers::DoPut(size_t index,
1561 const std::string& uri,
1562 const std::string& body) const
1563 {
1564 if (index >= index_.size())
1565 {
1566 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1567 }
1568
1569 OrthancPluginMemoryBuffer answer;
1570 uint16_t status;
1571 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1572 (context_, &answer, NULL, &status, peers_,
1573 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(),
1574 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_);
1575
1576 if (code == OrthancPluginErrorCode_Success)
1577 {
1578 OrthancPluginFreeMemoryBuffer(context_, &answer);
1579 return (status == 200);
1580 }
1581 else
1582 {
1583 return false;
1584 }
1585 }
1586
1587
1588 bool OrthancPeers::DoPut(const std::string& name,
1589 const std::string& uri,
1590 const std::string& body) const
1591 {
1592 size_t index;
1593 return (LookupName(index, name) &&
1594 DoPut(index, uri, body));
1595 }
1596
1597
1598 bool OrthancPeers::DoDelete(size_t index,
1599 const std::string& uri) const
1600 {
1601 if (index >= index_.size())
1602 {
1603 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1604 }
1605
1606 OrthancPluginMemoryBuffer answer;
1607 uint16_t status;
1608 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1609 (context_, &answer, NULL, &status, peers_,
1610 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(),
1611 0, NULL, NULL, NULL, 0, timeout_);
1612
1613 if (code == OrthancPluginErrorCode_Success)
1614 {
1615 OrthancPluginFreeMemoryBuffer(context_, &answer);
1616 return (status == 200);
1617 }
1618 else
1619 {
1620 return false;
1621 }
1622 }
1623
1624
1625 bool OrthancPeers::DoDelete(const std::string& name,
1626 const std::string& uri) const
1627 {
1628 size_t index;
1629 return (LookupName(index, name) &&
1630 DoDelete(index, uri));
1631 }
1632 #endif
1633
1634
1635
1636 #if HAS_ORTHANC_PLUGIN_JOB == 1
1637 void OrthancJob::CallbackFinalize(void* job)
1638 {
1639 if (job != NULL)
1640 {
1641 delete reinterpret_cast<OrthancJob*>(job);
1642 }
1643 }
1644
1645
1646 float OrthancJob::CallbackGetProgress(void* job)
1647 {
1648 assert(job != NULL);
1649
1650 try
1651 {
1652 return reinterpret_cast<OrthancJob*>(job)->progress_;
1653 }
1654 catch (...)
1655 {
1656 return 0;
1657 }
1658 }
1659
1660
1661 const char* OrthancJob::CallbackGetContent(void* job)
1662 {
1663 assert(job != NULL);
1664
1665 try
1666 {
1667 return reinterpret_cast<OrthancJob*>(job)->content_.c_str();
1668 }
1669 catch (...)
1670 {
1671 return 0;
1672 }
1673 }
1674
1675
1676 const char* OrthancJob::CallbackGetSerialized(void* job)
1677 {
1678 assert(job != NULL);
1679
1680 try
1681 {
1682 const OrthancJob& tmp = *reinterpret_cast<OrthancJob*>(job);
1683
1684 if (tmp.hasSerialized_)
1685 {
1686 return tmp.serialized_.c_str();
1687 }
1688 else
1689 {
1690 return NULL;
1691 }
1692 }
1693 catch (...)
1694 {
1695 return 0;
1696 }
1697 }
1698
1699
1700 OrthancPluginJobStepStatus OrthancJob::CallbackStep(void* job)
1701 {
1702 assert(job != NULL);
1703
1704 try
1705 {
1706 return reinterpret_cast<OrthancJob*>(job)->Step();
1707 }
1708 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
1709 {
1710 return OrthancPluginJobStepStatus_Failure;
1711 }
1712 catch (...)
1713 {
1714 return OrthancPluginJobStepStatus_Failure;
1715 }
1716 }
1717
1718
1719 OrthancPluginErrorCode OrthancJob::CallbackStop(void* job,
1720 OrthancPluginJobStopReason reason)
1721 {
1722 assert(job != NULL);
1723
1724 try
1725 {
1726 reinterpret_cast<OrthancJob*>(job)->Stop(reason);
1727 return OrthancPluginErrorCode_Success;
1728 }
1729 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
1730 {
1731 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
1732 }
1733 catch (...)
1734 {
1735 return OrthancPluginErrorCode_Plugin;
1736 }
1737 }
1738
1739
1740 OrthancPluginErrorCode OrthancJob::CallbackReset(void* job)
1741 {
1742 assert(job != NULL);
1743
1744 try
1745 {
1746 reinterpret_cast<OrthancJob*>(job)->Reset();
1747 return OrthancPluginErrorCode_Success;
1748 }
1749 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
1750 {
1751 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
1752 }
1753 catch (...)
1754 {
1755 return OrthancPluginErrorCode_Plugin;
1756 }
1757 }
1758
1759
1760 void OrthancJob::ClearContent()
1761 {
1762 Json::Value empty = Json::objectValue;
1763 UpdateContent(empty);
1764 }
1765
1766
1767 void OrthancJob::UpdateContent(const Json::Value& content)
1768 {
1769 if (content.type() != Json::objectValue)
1770 {
1771 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat);
1772 }
1773 else
1774 {
1775 Json::FastWriter writer;
1776 content_ = writer.write(content);
1777 }
1778 }
1779
1780
1781 void OrthancJob::ClearSerialized()
1782 {
1783 hasSerialized_ = false;
1784 serialized_.clear();
1785 }
1786
1787
1788 void OrthancJob::UpdateSerialized(const Json::Value& serialized)
1789 {
1790 if (serialized.type() != Json::objectValue)
1791 {
1792 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat);
1793 }
1794 else
1795 {
1796 Json::FastWriter writer;
1797 serialized_ = writer.write(serialized);
1798 hasSerialized_ = true;
1799 }
1800 }
1801
1802
1803 void OrthancJob::UpdateProgress(float progress)
1804 {
1805 if (progress < 0 ||
1806 progress > 1)
1807 {
1808 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1809 }
1810
1811 progress_ = progress;
1812 }
1813
1814
1815 OrthancJob::OrthancJob(const std::string& jobType) :
1816 jobType_(jobType),
1817 progress_(0)
1818 {
1819 ClearContent();
1820 ClearSerialized();
1821 }
1822
1823
1824 OrthancPluginJob* OrthancJob::Create(OrthancPluginContext* context,
1825 OrthancJob* job)
1826 {
1827 if (job == NULL)
1828 {
1829 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer);
1830 }
1831
1832 OrthancPluginJob* orthanc = OrthancPluginCreateJob(
1833 context, job, CallbackFinalize, job->jobType_.c_str(),
1834 CallbackGetProgress, CallbackGetContent, CallbackGetSerialized,
1835 CallbackStep, CallbackStop, CallbackReset);
1836
1837 if (orthanc == NULL)
1838 {
1839 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1840 }
1841 else
1842 {
1843 return orthanc;
1844 }
1845 }
1846
1847
1848 std::string OrthancJob::Submit(OrthancPluginContext* context,
1849 OrthancJob* job,
1850 int priority)
1851 {
1852 OrthancPluginJob* orthanc = Create(context, job);
1853
1854 char* id = OrthancPluginSubmitJob(context, orthanc, priority);
1855
1856 if (id == NULL)
1857 {
1858 OrthancPluginLogError(context, "Plugin cannot submit job");
1859 OrthancPluginFreeJob(context, orthanc);
1860 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1861 }
1862 else
1863 {
1864 std::string tmp(id);
1865 tmp.assign(id);
1866 OrthancPluginFreeString(context, id);
1867
1868 return tmp;
1869 }
1870 }
1871 #endif
1286 } 1872 }
1287