shared-state.html
7.14 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>hueplusplus: Shared state cache</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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">hueplusplus
 <span id="projectnumber">1.0.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- 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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Shared state cache </div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><ul><li class="level2"><a href="#autotoc_md22">What shared state means</a><ul><li class="level3"><a href="#autotoc_md23">Advantages of shared state</a></li>
<li class="level3"><a href="#autotoc_md24">Disadvantages of shared state</a></li>
</ul>
</li>
<li class="level2"><a href="#autotoc_md25">Enabling shared state</a></li>
<li class="level2"><a href="#autotoc_md26">Shared state and refreshing</a><ul><li class="level3"><a href="#autotoc_md27">Example:</a></li>
</ul>
</li>
</ul>
</ul>
</div>
<div class="textblock"><p>There are two ways in which the API state (internally JSON) can be handled:</p><ol type="1">
<li>Every resource instance holds its own cache of the state (default).</li>
<li>All instances share the cache of the entire bridge.</li>
</ol>
<ul>
<li>Different resources are always consistent on the library level. If one part of the code uses the light with id 1 and turns it off, light 1 is also off when using a different variable to access it.</li>
<li>The number of requests can be reduced, because they can be bundled together on a higher cache level.</li>
</ul>
<ul>
<li>Different objects are no longer thread safe, you cannot use <b>any</b> parts of the library from multiple threads (without locking).</li>
<li>Changes are not transparent. For example, a <code>const Light</code> can suddenly change its name, because the name was changed somewhere else in the code.</li>
</ul>
<p>Because of these considerations, shared state is disabled by default.</p>
<p>Shared state can be configured when the bridge is first constructed, either in <a class="el" href="classhueplusplus_1_1_bridge_finder.html#a9ff121c380ceaceaf5da2b7a772e5c4b">getBridge()</a> or in the <a class="el" href="classhueplusplus_1_1_bridge.html#a5fa0378ab3552a82d611fba6701bcad6">constructor</a>. Set <code>sharedState</code> to <code>true</code> to keep all resources connected to the bridge cache. </p><div class="fragment"><div class="line"> <a class="code" href="classhueplusplus_1_1_bridge.html">hueplusplus::Bridge</a> bridge = finder.getBridge(bridges[0], <span class="keyword">true</span>);</div></div><!-- fragment --><div class="fragment"><div class="line"> <a class="code" href="classhueplusplus_1_1_bridge.html">hueplusplus::Bridge</a> bridge(<span class="stringliteral">"192.168.2.102"</span>, 80, <span class="stringliteral">"<username>"</span>, handler, <span class="stringliteral">""</span>, std::chrono::seconds(10), <span class="keyword">true</span>);</div></div><!-- fragment --> <p>When shared cache is used, refreshes use a hierarchichal structure to determine how much should be requested from the bridge. Every level has its own last update time and refresh duration. First, it is checked whether the higher level is up to date and refresh everything if not. Otherwise, only the lowest necessary level is requested from the bridge to be more efficient.</p>
<div class="fragment"><div class="line"> bridge.<a class="code" href="classhueplusplus_1_1_bridge.html#adc8372a1ca66b86098ae5a841106d89b">setRefreshDuration</a>(std::chrono::minutes(1));</div><div class="line"> bridge.<a class="code" href="classhueplusplus_1_1_bridge.html#a7bd607246130312915a3b07116c72301">lights</a>().<a class="code" href="classhueplusplus_1_1_resource_list.html#aa165e446439defcae7d1bf05b4576739">setRefreshDuration</a>(std::chrono::seconds(30));</div><div class="line"> <a class="code" href="classhueplusplus_1_1_light.html">hueplusplus::Light</a> light = bridge.<a class="code" href="classhueplusplus_1_1_bridge.html#a7bd607246130312915a3b07116c72301">lights</a>().<a class="code" href="classhueplusplus_1_1_resource_list.html#a7bcf2ed48e70e9f11a4b6bdc24e0e611">get</a>(1);</div><div class="line"> <span class="comment">// ... wait some time</span></div><div class="line"> <span class="keywordtype">bool</span> on = light.<a class="code" href="classhueplusplus_1_1_light.html#a26172501723aeec5abcf8db5b0dc6986">isOn</a>();</div></div><!-- fragment --><p><a class="el" href="classhueplusplus_1_1_light.html#a26172501723aeec5abcf8db5b0dc6986">isOn()</a> is a non-const method (in this case). That means it will refresh the state if it is outdated. The default refresh time is inherited from <code>bridge.lights()</code>, so it is 30 seconds. After 30 seconds, the state of <code>light</code> <em>and</em> <code>bridge.lights()</code> is outdated. Therefore, the entire list of lights is updated at this point.</p>
<p>After more than one minute, the bridge state is considered outdated. This means that <code>isOn()</code> causes an update of the entire bridge. </p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>