-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy paththreading_example_8cc-example.html
More file actions
142 lines (140 loc) · 9.08 KB
/
Copy paththreading_example_8cc-example.html
File metadata and controls
142 lines (140 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.7"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cpp-ipfs-http-client: threading_example.cc</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">cpp-ipfs-http-client
</div>
<div id="projectbrief">IPFS C++ client library</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.7 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">threading_example.cc</div></div>
</div><!--header-->
<div class="contents">
<p>An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.</p>
<p>An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.</p>
<div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#include <ipfs/client.h></span></div>
<div class="line"><span class="comment">// Or when you use IPFS locally:</span></div>
<div class="line"><span class="comment">//#include "ipfs/client.h"</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <iostream></span></div>
<div class="line"><span class="preprocessor">#include <sstream></span></div>
<div class="line"><span class="preprocessor">#include <thread></span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> startThread() {</div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> <span class="comment">// Create IPFS Client object, with 2 minutes time-out.</span></div>
<div class="line"> <a id="_a0" name="_a0"></a><a class="code hl_class" href="classipfs_1_1Client.html">ipfs::Client</a> client(<span class="stringliteral">"localhost"</span>, 5001, <span class="stringliteral">"2m"</span>);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Only start a single thread</span></div>
<div class="line"> std::thread thread([&client]() {</div>
<div class="line"> std::stringstream contents;</div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> <span class="comment">// File should not exists, takes forever (until time-out)</span></div>
<div class="line"> client.<a id="a1" name="a1"></a><a class="code hl_function" href="classipfs_1_1Client.html#a7598eaecec5293f29f84e2fc7ca7bc5b">FilesGet</a>(<span class="stringliteral">"QmZp1rrtGTictR2rpNcx4673R7qU9Jdr9DQ6Z7F6Wgo2bQ"</span>,</div>
<div class="line"> &contents);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Code below will never be reached, since we abort the request</span></div>
<div class="line"> std::cout << <span class="stringliteral">"Output: "</span> << contents.str() << std::endl;</div>
<div class="line"> } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::runtime_error& e) {</div>
<div class="line"> <span class="comment">// Run-time error will be thrown because of the aborting request.</span></div>
<div class="line"> std::cerr << <span class="stringliteral">"Error: "</span> << e.what() << std::endl;</div>
<div class="line"> }</div>
<div class="line"> });</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">if</span> (thread.joinable()) {</div>
<div class="line"> std::cout << <span class="stringliteral">"Directly try to abort the request and stop the thread."</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> <span class="comment">// Try to remove the Abort() and Reset() calls,</span></div>
<div class="line"> <span class="comment">// and see the difference yourself :)</span></div>
<div class="line"> client.<a id="a2" name="a2"></a><a class="code hl_function" href="classipfs_1_1Client.html#aaa862322e6ec703b91ed356d968fbc49">Abort</a>();</div>
<div class="line"> thread.join(); <span class="comment">// Should not be blocking now</span></div>
<div class="line"> client.<a id="a3" name="a3"></a><a class="code hl_function" href="classipfs_1_1Client.html#a60b2274e7ee70ed9392870cd2c5a8559">Reset</a>();</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::exception& e) {</div>
<div class="line"> std::cerr << <span class="stringliteral">"General error: "</span> << e.what() << std::endl;</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line"> std::cout << <span class="stringliteral">"Starting thread.."</span> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Start a request inside a thread</span></div>
<div class="line"> startThread();</div>
<div class="line"> </div>
<div class="line"> std::cout << <span class="stringliteral">"Done!"</span> << std::endl;</div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="ttc" id="aclassipfs_1_1Client_html"><div class="ttname"><a href="classipfs_1_1Client.html">ipfs::Client</a></div><div class="ttdoc">IPFS client.</div><div class="ttdef"><b>Definition</b> client.h:48</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_a60b2274e7ee70ed9392870cd2c5a8559"><div class="ttname"><a href="classipfs_1_1Client.html#a60b2274e7ee70ed9392870cd2c5a8559">ipfs::Client::Reset</a></div><div class="ttdeci">void Reset()</div><div class="ttdoc">Resets the abort call, allowing to execute new API requests again.</div><div class="ttdef"><b>Definition</b> client.cc:483</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_a7598eaecec5293f29f84e2fc7ca7bc5b"><div class="ttname"><a href="classipfs_1_1Client.html#a7598eaecec5293f29f84e2fc7ca7bc5b">ipfs::Client::FilesGet</a></div><div class="ttdeci">void FilesGet(const std::string &path, std::iostream *response)</div><div class="ttdoc">Get a file from IPFS.</div><div class="ttdef"><b>Definition</b> client.cc:206</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_aaa862322e6ec703b91ed356d968fbc49"><div class="ttname"><a href="classipfs_1_1Client.html#aaa862322e6ec703b91ed356d968fbc49">ipfs::Client::Abort</a></div><div class="ttdeci">void Abort()</div><div class="ttdoc">Abort any current running IPFS API request.</div><div class="ttdef"><b>Definition</b> client.cc:476</div></div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.7
</small></address>
</body>
</html>