source: flair-src/trunk/lib/FlairCore/src/IODevice_impl.cpp@ 122

Last change on this file since 122 was 122, checked in by Sanahuja Guillaume, 6 years ago

modifs uav vrpn i686

File size: 6.2 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2011/05/01
6// filename: IODevice.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe de base avec entrees sorties
14//
15//
16/*********************************************************************/
17
18#include "IODevice_impl.h"
19#include "IODevice.h"
20#include "io_data.h"
21#include "io_data_impl.h"
22#include "Thread.h"
23#include "Mutex.h"
24#include "FrameworkManager.h"
25#include "FrameworkManager_impl.h"
26#include "SharedMem.h"
27#include <string.h>
28
29using std::string;
30using std::fstream;
31using namespace flair::core;
32
33IODevice_impl::IODevice_impl(const IODevice *self) {
34 this->self = self;
35 thread_to_wake = NULL;
36 wake_mutex = new Mutex(self);
37 framework_impl = ::getFrameworkManagerImpl();
38 framework = getFrameworkManager();
39 tobelogged = false;
40 outputtoshm = false;
41}
42
43IODevice_impl::~IODevice_impl() {}
44
45void IODevice_impl::OutputToShMem(bool enabled) {
46 if (framework->IsLogging()) {
47 self->Err("impossible while logging\n");
48 } else {
49 if (LogSize() == 0) {
50 self->Warn("log size is null, not enabling output to shared memory.");
51 return;
52 }
53
54 if (enabled) {
55 string dev_name =
56 getFrameworkManager()->ObjectName() + "-" + self->ObjectName();
57 shmem = new SharedMem(self, dev_name.c_str(), LogSize());
58 outputtoshm = true;
59
60 Printf("Created %s shared memory for object %s; size: %i\n",
61 dev_name.c_str(), self->ObjectName().c_str(), LogSize());
62 PrintLogsDescriptors();
63 } else {
64 outputtoshm = false;
65 delete shmem;
66 }
67 }
68}
69
70void IODevice_impl::PrintLogsDescriptors(void) {
71 // own logs
72 for (size_t i = 0; i < datasToLog.size(); i++)
73 datasToLog.at(i)->pimpl_->PrintLogDescriptor();
74 // childs logs
75 for (size_t i = 0; i < self->TypeChilds()->size(); i++)
76 ((IODevice *)self->TypeChilds()->at(i))->pimpl_->PrintLogsDescriptors();
77 // manually added logs
78 for (size_t i = 0; i < devicesToLog.size(); i++)
79 devicesToLog.at(i)->pimpl_->PrintLogsDescriptors();
80}
81
82void IODevice_impl::WriteToShMem(void) {
83 if (outputtoshm) {
84 size_t size = LogSize();
85
86 char *buf = framework_impl->GetBuffer(size);
87 char *buf_orig = buf;
88 if (buf == NULL) {
89 self->Err("err GetBuffer\n");
90 return;
91 }
92
93 AppendLog(&buf);
94
95 shmem->Write(buf_orig, size);
96 framework_impl->ReleaseBuffer(buf_orig);
97 }
98}
99
100void IODevice_impl::AddDeviceToLog(const IODevice *device) {
101 if (framework->IsLogging()) {
102 self->Err("impossible while logging\n");
103 } else {
104 devicesToLog.push_back(device);
105 }
106}
107
108bool IODevice_impl::IsSetToBeLogged(void) const{
109 return tobelogged;
110}
111
112void IODevice_impl::SetToBeLogged(void) {
113 if (!tobelogged) {
114 tobelogged = true;
115 } else {
116 self->Warn("already added to log\n");
117 }
118}
119
120void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
121 //Printf("WriteLogsDescriptors %s\n",self->ObjectName().c_str());
122 // own descriptor
123 for (size_t i = 0; i < datasToLog.size(); i++)
124 datasToLog.at(i)->pimpl_->WriteLogDescriptor(desc_file, index);
125 // childs descriptors
126 for (size_t i = 0; i < self->TypeChilds()->size(); i++)
127 ((IODevice *)self->TypeChilds()->at(i))
128 ->pimpl_->WriteLogsDescriptors(desc_file, index);
129 // manually added logs
130 for (size_t i = 0; i < devicesToLog.size(); i++)
131 devicesToLog.at(i)->pimpl_->WriteLogsDescriptors(desc_file, index);
132 //Printf("WriteLogsDescriptors %s ok\n",self->ObjectName().c_str());
133}
134
135void IODevice_impl::ResumeThread(void) {
136 wake_mutex->GetMutex();
137 if (thread_to_wake != NULL) {
138 thread_to_wake->Resume();
139 thread_to_wake = NULL;
140 }
141 wake_mutex->ReleaseMutex();
142}
143
144void IODevice_impl::AddDataToLog(const io_data *data) {
145 if (framework->IsLogging()) {
146 self->Err("impossible while logging\n");
147 } else {
148 datasToLog.push_back(data);
149 }
150}
151
152size_t IODevice_impl::LogSize() const {
153 size_t value = 0;
154
155 for (size_t i = 0; i < datasToLog.size(); i++) {
156 value += datasToLog.at(i)->GetDataType().GetSize();
157 }
158
159 // childs logs
160 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
161 value += ((IODevice *)self->TypeChilds()->at(i))->pimpl_->LogSize();
162 }
163 // manually added logs
164 for (size_t i = 0; i < devicesToLog.size(); i++) {
165 value += devicesToLog.at(i)->pimpl_->LogSize();
166 }
167
168 return value;
169}
170
171void IODevice_impl::WriteLog(Time time) {
172 if (tobelogged == false)
173 return;
174
175 size_t size = LogSize();
176
177 char *buf = framework_impl->GetBuffer(
178 sizeof(FrameworkManager_impl::log_header_t) + size);
179 char *buf_orig = buf;
180 if (buf == NULL) {
181 self->Err("err GetBuffer\n");
182 return;
183 }
184
185 FrameworkManager_impl::log_header_t header;
186 header.device = self;
187 header.size = size;
188 header.time = time;
189
190 memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
191 buf += sizeof(FrameworkManager_impl::log_header_t);
192 AppendLog(&buf);
193
194 framework_impl->WriteLog(buf_orig,
195 sizeof(FrameworkManager_impl::log_header_t) + size);
196 framework_impl->ReleaseBuffer(buf_orig);
197}
198
199void IODevice_impl::AppendLog(char **ptr) {
200 //Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
201
202 // copy state to buf
203 for (size_t i = 0; i < datasToLog.size(); i++) {
204 // printf("copy %s\n",datasToLog.at(i)->ObjectName().c_str());
205 datasToLog.at(i)->CopyDatas(*ptr);
206 (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
207 }
208
209 // copy linked states to buf
210 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
211 ((IODevice *)self->TypeChilds()->at(i))->pimpl_->AppendLog(ptr);
212 }
213 // copy manually added logs to buf
214 for (size_t i = 0; i < devicesToLog.size(); i++) {
215 devicesToLog.at(i)->pimpl_->AppendLog(ptr);
216 // devices.at(i)->DataToLog()->CopyDatas(*ptr);
217 //(*ptr)+=devices.at(i)->DataToLog()->Size();
218 }
219 // Printf("AppendLog %s ok\n",self->ObjectName().c_str());
220}
221
222int IODevice_impl::SetToWake(const Thread *thread) {
223 int status = 0;
224
225 wake_mutex->GetMutex();
226 if (thread_to_wake == NULL) {
227 thread_to_wake = (Thread *)thread;
228 } else {
229 status = -1;
230 }
231 wake_mutex->ReleaseMutex();
232
233 return status;
234}
Note: See TracBrowser for help on using the repository browser.