summaryrefslogtreecommitdiffhomepage
path: root/Tobii EyeX/include/eyex-cpp/InteractionAgentBase.inl
blob: c94982200763a47076d12e75748de262e745af98 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*********************************************************************************************************************
 * Copyright 2013-2014 Tobii Technology AB. All rights reserved.
 * InteractionAgentBase.inl
 *********************************************************************************************************************/

#if !defined(__TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONAGENTBASE__INL__)
#define __TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONAGENTBASE__INL__

/*********************************************************************************************************************/

TX_NAMESPACE_BEGIN

/*********************************************************************************************************************/

inline InteractionAgentBase::InteractionAgentBase(bool trackObjects) :
_trackObjects(trackObjects)
{ 
    memset(&_defaultLoggingModel, 0, sizeof(_defaultLoggingModel));
	_defaultLoggingModel.Targets = TX_LOGTARGET_CONSOLE;

#if _DEBUG
	_defaultLoggingModel.Targets = (TX_LOGTARGET)(_defaultLoggingModel.Targets | TX_LOGTARGET_TRACE);
#endif
}

/*********************************************************************************************************************/

inline void InteractionAgentBase::Initialize()
{
    _isRunning = true;
	_spSystem = InitializeEyeX();
	_spContext = Context::Create(_trackObjects);

	_connectionStateChangedHandlerTicket = _spContext->RegisterConnectionStateChangedHandler([this] (TX_CONNECTIONSTATE state) {
		OnConnectionStateChanged(state);
	});

	auto eventHandlerTicket = _spContext->RegisterMessageHandler(TX_MESSAGETYPE_EVENT, nullptr, [this] (const std::unique_ptr<AsyncData>& upAsyncData) {
		auto spEvent = upAsyncData->GetDataAs<InteractionEvent>();
		OnEvent(spEvent);
	});

	auto notificationHandlerTicket = _spContext->RegisterMessageHandler(TX_MESSAGETYPE_NOTIFICATION, nullptr, [this] (const std::unique_ptr<AsyncData>& upAsyncData) {
		auto spNotification = upAsyncData->GetDataAs<Notification>();
		OnNotification(spNotification);
	});

	_messageHandlerTickets.push_back(eventHandlerTicket);
	_messageHandlerTickets.push_back(notificationHandlerTicket);

	OnRegisterQueryHandlers();
	OnInitialize();

	_spContext->EnableConnection();    
}

/*********************************************************************************************************************/

inline void InteractionAgentBase::Uninitialize()
{
    _isRunning = false;

	OnUninitialize();
	_spContext->DisableConnection();

	for(auto messageHandlerTicket : _messageHandlerTickets)
		_spContext->UnregisterMessageHandler(messageHandlerTicket);

	_spContext->Shutdown();
    _spContext->UnregisterConnectionStateChangedHandler(_connectionStateChangedHandlerTicket);

	_spContext.reset();
	_spSystem.reset();
}

/*********************************************************************************************************************/

inline std::shared_ptr<Context> InteractionAgentBase::GetContext() const
{
	return _spContext;
}

/*********************************************************************************************************************/

inline std::shared_ptr<Environment> InteractionAgentBase::InitializeEyeX()
{
	auto pLoggingModel = GetLoggingModel();
	auto pThreadingModel = GetThreadingModel();
	auto pSchedulingModel = GetSchedulingModel();
   
	auto overrideFlags = TX_EYEXCOMPONENTOVERRIDEFLAG_NONE;

	if(pLoggingModel)
		overrideFlags = (TX_EYEXCOMPONENTOVERRIDEFLAGS)(overrideFlags | TX_EYEXCOMPONENTOVERRIDEFLAG_LOGGINGMODEL);
		
	if(pThreadingModel)
		overrideFlags = (TX_EYEXCOMPONENTOVERRIDEFLAGS)(overrideFlags | TX_EYEXCOMPONENTOVERRIDEFLAG_INTERNAL_THREADINGMODEL);

	if(pSchedulingModel)
		overrideFlags = (TX_EYEXCOMPONENTOVERRIDEFLAGS)(overrideFlags | TX_EYEXCOMPONENTOVERRIDEFLAG_INTERNAL_SCHEDULINGMODEL);

	return Environment::Initialize(overrideFlags, pLoggingModel, pThreadingModel, pSchedulingModel, nullptr);
}

/*********************************************************************************************************************/

inline void InteractionAgentBase::OnRegisterQueryHandlers()
{
	auto currentProcessId = GetCurrentProcessId();
	auto currentProcessIdStr = std::to_string(currentProcessId);
	RegisterQueryHandler(currentProcessIdStr);
}

/*********************************************************************************************************************/

inline void InteractionAgentBase::RegisterQueryHandler(const std::string& processId)
{
	auto spOptions = _spContext->CreateBag();
	auto spProcessIdProeprty = spOptions->CreateProperty(TX_LITERAL_TARGETPROCESSID);
	spProcessIdProeprty->SetValue(processId);

	auto fnQueryHandler = [this](const std::unique_ptr<AsyncData>& upAsyncData) 
	{
		auto spQuery = upAsyncData->GetDataAs<Query>();       
		OnQuery(spQuery);       
	};

	auto ticket = _spContext->RegisterMessageHandler(TX_MESSAGETYPE_QUERY, spOptions, fnQueryHandler);
	_messageHandlerTickets.push_back(ticket);
}

/*********************************************************************************************************************/

inline TX_LOGGINGMODEL* InteractionAgentBase::GetLoggingModel()
{
	return &_defaultLoggingModel;	
}

/*********************************************************************************************************************/

inline bool InteractionAgentBase::IsRunning() const
{
    return _isRunning;
}

/*********************************************************************************************************************/

inline AutoRespondingInteractionAgentBase::AutoRespondingInteractionAgentBase(bool trackObjects)
	: InteractionAgentBase(trackObjects)
{ }

/*********************************************************************************************************************/

inline void AutoRespondingInteractionAgentBase::OnQuery(const std::shared_ptr<Query>& spQuery)
{	    
    auto spSnapshot = Snapshot::CreateSnapshotForQuery(spQuery);

    try
    {
	    if(!PrepareSnapshot(spSnapshot))
		    return;
    }
    catch(...)
    {         
        GetContext()->WriteLogMessage(TX_LOGLEVEL_ERROR, "AutoRespondingInteractionAgentBase", "Custom snapshot preparation throw an exception");
		return;
    }

	spSnapshot->CommitAsync([this](const std::unique_ptr<AsyncData>& upAsyncData)
	{
		OnSnapshotResult(upAsyncData);	
	});
}

/*********************************************************************************************************************/

TX_NAMESPACE_END

/*********************************************************************************************************************/

#endif // !defined(__TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONAGENTBASE__INL__)

/*********************************************************************************************************************/