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

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

change io_data CopyDate to RawRead

File size: 6.3 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 isReady=false;
42}
43
44IODevice_impl::~IODevice_impl() {}
45
46void IODevice_impl::OutputToShMem(bool enabled) {
47 if (framework->IsLogging()) {
48 self->Err("impossible while logging\n");
49 } else {
50 if (LogSize() == 0) {
51 self->Warn("log size is null, not enabling output to shared memory.");
52 return;
53 }
54
55 if (enabled) {
56 string dev_name =
57 getFrameworkManager()->ObjectName() + "-" + self->ObjectName();
58 shmem = new SharedMem(self, dev_name.c_str(), LogSize());
59 outputtoshm = true;
60
61 Printf("Created %s shared memory for object %s; size: %i\n",
62 dev_name.c_str(), self->ObjectName().c_str(), LogSize());
63 PrintLogsDescriptors();
64 } else {
65 outputtoshm = false;
66 delete shmem;
67 }
68 }
69}
70
71void IODevice_impl::PrintLogsDescriptors(void) {
72 // own logs
73 for (size_t i = 0; i < datasToLog.size(); i++)
74 datasToLog.at(i)->pimpl_->PrintLogDescriptor();
75 // childs logs
76 for (size_t i = 0; i < self->TypeChilds()->size(); i++)
77 ((IODevice *)self->TypeChilds()->at(i))->pimpl_->PrintLogsDescriptors();
78 // manually added logs
79 for (size_t i = 0; i < devicesToLog.size(); i++)
80 devicesToLog.at(i)->pimpl_->PrintLogsDescriptors();
81}
82
83void IODevice_impl::WriteToShMem(void) {
84 if (outputtoshm) {
85 size_t size = LogSize();
86
87 char *buf = framework_impl->GetBuffer(size);
88 char *buf_orig = buf;
89 if (buf == NULL) {
90 self->Err("err GetBuffer\n");
91 return;
92 }
93
94 AppendLog(&buf);
95
96 shmem->Write(buf_orig, size);
97 framework_impl->ReleaseBuffer(buf_orig);
98 }
99}
100
101void IODevice_impl::AddDeviceToLog(const IODevice *device) {
102 if (framework->IsLogging()) {
103 self->Err("impossible while logging\n");
104 } else {
105 devicesToLog.push_back(device);
106 }
107}
108
109bool IODevice_impl::IsSetToBeLogged(void) const{
110 return tobelogged;
111}
112
113void IODevice_impl::SetToBeLogged(void) {
114 if (!tobelogged) {
115 tobelogged = true;
116 } else {
117 self->Warn("already added to log\n");
118 }
119}
120
121void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
122 //Printf("WriteLogsDescriptors %s\n",self->ObjectName().c_str());
123 // own descriptor
124 for (size_t i = 0; i < datasToLog.size(); i++)
125 datasToLog.at(i)->pimpl_->WriteLogDescriptor(desc_file, index);
126 // childs descriptors
127 for (size_t i = 0; i < self->TypeChilds()->size(); i++)
128 ((IODevice *)self->TypeChilds()->at(i))
129 ->pimpl_->WriteLogsDescriptors(desc_file, index);
130 // manually added logs
131 for (size_t i = 0; i < devicesToLog.size(); i++)
132 devicesToLog.at(i)->pimpl_->WriteLogsDescriptors(desc_file, index);
133 //Printf("WriteLogsDescriptors %s ok\n",self->ObjectName().c_str());
134}
135
136void IODevice_impl::ResumeThread(void) {
137 wake_mutex->GetMutex();
138 if (thread_to_wake != NULL) {
139 thread_to_wake->Resume();
140 thread_to_wake = NULL;
141 }
142 wake_mutex->ReleaseMutex();
143}
144
145void IODevice_impl::AddDataToLog(const io_data *data) {
146 if (framework->IsLogging()) {
147 self->Err("impossible while logging\n");
148 } else {
149 datasToLog.push_back(data);
150 }
151}
152
153size_t IODevice_impl::LogSize() const {
154 size_t value = 0;
155
156 for (size_t i = 0; i < datasToLog.size(); i++) {
157 value += datasToLog.at(i)->GetDataType().GetSize();
158 }
159
160 // childs logs
161 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
162 value += ((IODevice *)self->TypeChilds()->at(i))->pimpl_->LogSize();
163 }
164 // manually added logs
165 for (size_t i = 0; i < devicesToLog.size(); i++) {
166 value += devicesToLog.at(i)->pimpl_->LogSize();
167 }
168
169 return value;
170}
171
172void IODevice_impl::WriteLog(Time time) {
173 if (tobelogged == false)
174 return;
175
176 size_t size = LogSize();
177
178 char *buf = framework_impl->GetBuffer(
179 sizeof(FrameworkManager_impl::log_header_t) + size);
180 char *buf_orig = buf;
181 if (buf == NULL) {
182 self->Err("err GetBuffer\n");
183 return;
184 }
185
186 FrameworkManager_impl::log_header_t header;
187 header.device = self;
188 header.size = size;
189 header.time = time;
190
191 memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
192 buf += sizeof(FrameworkManager_impl::log_header_t);
193 AppendLog(&buf);
194
195 framework_impl->WriteLog(buf_orig,
196 sizeof(FrameworkManager_impl::log_header_t) + size);
197 framework_impl->ReleaseBuffer(buf_orig);
198}
199
200void IODevice_impl::AppendLog(char **ptr) {
201 //Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
202
203 // copy state to buf
204 for (size_t i = 0; i < datasToLog.size(); i++) {
205 // printf("copy %s\n",datasToLog.at(i)->ObjectName().c_str());
206 datasToLog.at(i)->RawRead(*ptr);
207 (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
208 }
209
210 // copy linked states to buf
211 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
212 ((IODevice *)self->TypeChilds()->at(i))->pimpl_->AppendLog(ptr);
213 }
214 // copy manually added logs to buf
215 for (size_t i = 0; i < devicesToLog.size(); i++) {
216 devicesToLog.at(i)->pimpl_->AppendLog(ptr);
217 // devices.at(i)->DataToLog()->CopyDatas(*ptr);
218 //(*ptr)+=devices.at(i)->DataToLog()->Size();
219 }
220 // Printf("AppendLog %s ok\n",self->ObjectName().c_str());
221}
222
223int IODevice_impl::SetToWake(const Thread *thread) {
224 int status = 0;
225
226 wake_mutex->GetMutex();
227 if (thread_to_wake == NULL) {
228 thread_to_wake = (Thread *)thread;
229 } else {
230 status = -1;
231 }
232 wake_mutex->ReleaseMutex();
233
234 return status;
235}
236
237void IODevice_impl::SetIsReady(bool status) {
238 isReady=status;
239}
240
241bool IODevice_impl::IsReady(void) const {
242 return isReady;
243}
Note: See TracBrowser for help on using the repository browser.