file_example.cpp
Go to the documentation of this file.
1 #include <qicore/file.hpp>
2 #include <qi/session.hpp>
3 #include <qi/application.hpp>
4 #include <qi/type/dynamicobjectbuilder.hpp>
5 #include <testsession/testsessionpair.hpp>
6 #include <testsession/testsession.hpp>
7 
8 #include "imagestore.hpp"
9 
10 qiLogCategory("test.qi.fileExample");
11 
12 namespace bob
13 {
14 void printTranferProgress(double progress)
15 {
16  qiLogInfo() << ">>>> File Transfer Progress = " << (progress * 100.0) << "%";
17 }
18 
19 void workOnImageFile(const qi::Path& imageFilePath)
20 {
21  qiLogInfo() << "Working on image file at " << imageFilePath << " ...";
22  // we fake working on it...
23  boost::this_thread::sleep_for(boost::chrono::milliseconds(300));
24  qiLogInfo() << "Working on image file at " << imageFilePath << " - DONE";
25 }
26 
27 void storeImage(alice::ImageStorePtr imageStore, const std::string& name, const qi::Path& imageFilePath)
28 {
29  qiLogInfo() << "Storing image file at " << imageFilePath << " into the ImageStore...";
30 
31  // First open the file with read-only shareable access.
32  qi::FilePtr file = qi::openLocalFile(imageFilePath);
33 
34  // Now we can share reading access to this file.
35  imageStore->storeImage(file, name);
36 
37  qiLogInfo() << "Storing image file at " << imageFilePath << " into the ImageStore - DONE";
38 }
39 
40 void processImage(alice::ImageStorePtr imageStore, const std::string& imageFile, const qi::Path& imageFilePath)
41 {
42  // We acquire read-only access to the file and retrieve it locally.
43  qi::FilePtr file = imageStore->getImage(imageFile);
44  qi::copyToLocal(file, imageFilePath);
45 
46  // We don't need the remote access anymore.
47  file.reset();
48 
49  // Now work on the file located at `fileLocation`.
50  workOnImageFile(imageFilePath);
51 }
52 
54  const std::string& imageFile,
55  const std::string& imageFilePath)
56 {
57  // We acquire read-only access to the file.
58  qi::FilePtr file = imageStore->getImage(imageFile);
59 
60  // We prepare the operation without launching it yet:
61  qi::FileCopyToLocal fileOp{file, imageFilePath};
62 
63  // We want to see the progress so we plug a logging function.
64  fileOp.notifier()->progress.connect(&printTranferProgress);
65 
66  // Launch the copy and wait for it to end before continuing.
67  fileOp.start().wait(); // Don't wait for futures in real code, you should .connect() instead.
68 
69  // We don't need the remote access anymore.
70  file.reset();
71 
72  // Now work on the file located at `fileLocation`.
73  workOnImageFile(imageFilePath);
74 }
75 
76 void doSomeWork(qi::SessionPtr clientSession, const qi::Path& imagePath, const std::string& imageName)
77 {
78  qi::AnyObject aliceServices = clientSession->service("AliceServices").value();
79  alice::ImageStorePtr imageStore = aliceServices.call<alice::ImageStorePtr>("imageStore");
80  assert(imageStore);
81  storeImage(imageStore, imageName, imagePath);
82  processImage(imageStore, imageName, "./tempfile");
83  processImageWithProgress(imageStore, imageName, "./tempfile");
84 }
85 }
86 
87 void setupAliceServer(qi::SessionPtr serverSession)
88 {
89  qi::DynamicObjectBuilder objectBuilder;
90  objectBuilder.advertiseMethod("imageStore", &alice::getImageStore);
91  qi::AnyObject aliceServices = objectBuilder.object();
92  serverSession->registerService("AliceServices", aliceServices);
93 }
94 
95 int main(int argc, char** argv)
96 {
97  ::TestMode::forceTestMode(TestMode::Mode_SD);
98  qi::Application app(argc, argv);
99 
100  if (argc != 3)
101  return EXIT_FAILURE;
102 
103  const auto imageFilePath = qi::Path::fromNative(argv[1]);
104  const auto imageID = qi::Path::fromNative(argv[2]).str();
105  TestSessionPair sessionPair;
106  setupAliceServer(sessionPair.server());
107  bob::doSomeWork(sessionPair.client(), imageFilePath, imageID);
108 
109  return EXIT_SUCCESS;
110 }
alice::getImageStore
ImageStorePtr getImageStore()
Definition: imagestore.cpp:83
bob::processImage
void processImage(alice::ImageStorePtr imageStore, const std::string &imageFile, const qi::Path &imageFilePath)
Definition: file_example.cpp:40
bob::workOnImageFile
void workOnImageFile(const qi::Path &imageFilePath)
Definition: file_example.cpp:19
bob::processImageWithProgress
void processImageWithProgress(alice::ImageStorePtr imageStore, const std::string &imageFile, const std::string &imageFilePath)
Definition: file_example.cpp:53
alice::ImageStorePtr
qi::Object< ImageStore > ImageStorePtr
Definition: imagestore.hpp:26
file.hpp
qi::copyToLocal
FutureSync< void > copyToLocal(FilePtr file, Path localPath)
Definition: fileoperation.cpp:19
setupAliceServer
void setupAliceServer(qi::SessionPtr serverSession)
Definition: file_example.cpp:87
setup.name
name
Definition: setup.py:84
main
int main(int argc, char **argv)
Definition: file_example.cpp:95
bob::storeImage
void storeImage(alice::ImageStorePtr imageStore, const std::string &name, const qi::Path &imageFilePath)
Definition: file_example.cpp:27
qi::openLocalFile
QICORE_API FilePtr openLocalFile(const qi::Path &localPath)
Definition: fileimpl.cpp:186
file_example.app
app
Definition: file_example.py:8
bob::doSomeWork
void doSomeWork(qi::SessionPtr clientSession, const qi::Path &imagePath, const std::string &imageName)
Definition: file_example.cpp:76
bob
Definition: file_example.cpp:12
imagestore.hpp
bob::printTranferProgress
void printTranferProgress(double progress)
Definition: file_example.cpp:14
qiLogCategory
qiLogCategory("test.qi.fileExample")
qi::FilePtr
qi::Object< File > FilePtr
Pointer to a file with shared/remote semantic.
Definition: file.hpp:213


naoqi_libqicore
Author(s): Aldebaran
autogenerated on Wed Sep 14 2022 02:22:41