COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
SharedState.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
14 #ifndef VRB_SHAREDSTATE_H
15 #define VRB_SHAREDSTATE_H
16 
17 #include <vector>
18 #include <string>
19 #include <functional>
20 #include <cassert>
21 
22 #include <net/tokenbuffer.h>
23 #include <util/coExport.h>
24 #include "regClass.h"
25 #include "SharedStateSerializer.h"
26 #include "SessionID.h"
27 
28 
29 
30 namespace vrb
31 {
32 
33 class clientRegVar;
34 
36 {
37  USE_COUPLING_MODE, //0 write/read in/from private or puplic session depending on coupling mode
38  NEVER_SHARE, //1 only write to private session to save the state on the server
39  ALWAYS_SHARE, //2 always write to public session and share the state within the group
40  SHARE_WITH_ALL //3 write to session 0 to share the state within all sessions
41 };
42 
44 {
45 public:
47 
48  virtual ~SharedStateBase();
49 
51  void setUpdateFunction(std::function<void(void)> function);
52 
54  bool valueChangedByOther() const;
55 
56  std::string getName() const;
57 
59  void update(clientRegVar *theChangedRegEntry) override;
60  void setID(SessionID &id);
61  void setMute(bool m);
62  bool getMute();
64  void resubscribe(SessionID& id);
65  //send value to local registry and vrb if syncInterval allows it
66  void frame(double time);
67  void setSyncInterval(float time);
68  float getSyncInerval();
69  //unmute and send value to vrb
70  void becomeMaster();
71 protected:
72  //convert tokenbuffer to datatype of the sharedState
73  virtual void deserializeValue(covise::TokenBuffer &data) = 0;
74  void subscribe(covise::TokenBuffer &&val);
75  void setVar(covise::TokenBuffer &&val);
76  const std::string className = "SharedState";
78  bool doSend = false;
79  bool doReceive = false;
80  bool valueChanged = false;
81  std::function<void(void)> updateCallback;
82 
83  VrbClientRegistry *m_registry = nullptr;
84 
85 private:
86  SessionID sessionID = 0;
87  bool muted = false;
88  bool send = false;
89  float syncInterval = 0.1f;
90  double lastUpdateTime = 0.0;
92 };
93 
94 template <class T>
96 {
97 public:
100  , m_value(value)
101  {
102  assert(m_registry);
104  serializeWithType(data, m_value);
105  subscribe(std::move(data));
106  }
107 
109  {
110  if (m_value != value)
111  {
112  m_value = value;
113  push();
114  }
115  return *this;
116  }
117 
118  operator T() const
119  {
120  return m_value;
121  }
122 
124  {
126  }
127 
129  void push()
130  {
131  valueChanged = false;
133  serializeWithType(data, m_value);
134  setVar(std::move(data));
135  }
136 
137  const T &value() const
138  {
139  return m_value;
140  }
141 
142 private:
144 
145 };
146 }
147 #endif
148 
149 
Definition: regClass.h:254
GLenum mode
Definition: khronos-glext.h:6908
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
Definition: SharedState.h:40
const T & value() const
Definition: SharedState.h:137
void subscribe(covise::TokenBuffer &&val)
Definition: SharedState.cpp:35
void deserializeWithType(covise::TokenBuffer &tb, T &value)
Definition: SharedStateSerializer.h:193
void push()
sends the value change to the vrb
Definition: SharedState.h:129
Definition: SessionID.h:19
Definition: SharedState.h:38
GLuint const GLchar * name
Definition: khronos-glext.h:6722
void serializeWithType(covise::TokenBuffer &tb, const T &value)
Definition: SharedStateSerializer.h:186
Definition: SharedState.h:39
Definition: SharedState.h:43
T m_value
Definition: SharedState.h:143
bool valueChanged
Definition: SharedState.h:80
std::string variableName
Definition: SharedState.h:77
Definition: VrbClientRegistry.h:24
const GLfloat * m
Definition: khronos-glext.h:12117
void deserializeValue(covise::TokenBuffer &data) override
Definition: SharedState.h:123
void setVar(covise::TokenBuffer &&val)
Definition: SharedState.cpp:40
Definition: SharedState.h:37
#define VRBEXPORT
Definition: coExport.h:81
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
GLuint GLfloat * val
Definition: khronos-glext.h:7898
Definition: SharedState.h:95
SharedStateType
Definition: SharedState.h:35
covise::TokenBuffer tb_value
Definition: SharedState.h:91
SharedStateBase(std::string name, SharedStateType mode)
Definition: SharedState.cpp:20
std::function< void(void)> updateCallback
Definition: SharedState.h:81
VrbClientRegistry * m_registry
Definition: SharedState.h:83
Definition: regClass.h:291
SharedState< T > & operator=(T value)
Definition: SharedState.h:108
Definition: tokenbuffer.h:74