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

Last change on this file since 15 was 15, checked in by Bayard Gildas, 6 years ago

sources reformatted with flair-format-dir script

File size: 6.0 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::SetToBeLogged(void) {
109 if (!tobelogged) {
110 tobelogged = true;
111 return true;
112 } else {
113 self->Warn("already added to log\n");
114 return false;
115 }
116}
117
118void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
119 // own descriptor
120 for (size_t i = 0; i < datasToLog.size(); i++)
121 datasToLog.at(i)->pimpl_->WriteLogDescriptor(desc_file, index);
122 // childs descriptors
123 for (size_t i = 0; i < self->TypeChilds()->size(); i++)
124 ((IODevice *)self->TypeChilds()->at(i))
125 ->pimpl_->WriteLogsDescriptors(desc_file, index);
126 // manually added logs
127 for (size_t i = 0; i < devicesToLog.size(); i++)
128 devicesToLog.at(i)->pimpl_->WriteLogsDescriptors(desc_file, index);
129}
130
131void IODevice_impl::ResumeThread(void) {
132 wake_mutex->GetMutex();
133 if (thread_to_wake != NULL) {
134 thread_to_wake->Resume();
135 thread_to_wake = NULL;
136 }
137 wake_mutex->ReleaseMutex();
138}
139
140void IODevice_impl::AddDataToLog(const io_data *data) {
141 if (framework->IsLogging()) {
142 self->Err("impossible while logging\n");
143 } else {
144 datasToLog.push_back(data);
145 }
146}
147
148size_t IODevice_impl::LogSize() const {
149 size_t value = 0;
150
151 for (size_t i = 0; i < datasToLog.size(); i++) {
152 value += datasToLog.at(i)->GetDataType().GetSize();
153 }
154
155 // childs logs
156 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
157 value += ((IODevice *)self->TypeChilds()->at(i))->pimpl_->LogSize();
158 }
159 // manually added logs
160 for (size_t i = 0; i < devicesToLog.size(); i++) {
161 value += devicesToLog.at(i)->pimpl_->LogSize();
162 }
163
164 return value;
165}
166
167void IODevice_impl::WriteLog(Time time) {
168 if (tobelogged == false)
169 return;
170
171 size_t size = LogSize();
172
173 char *buf = framework_impl->GetBuffer(
174 sizeof(FrameworkManager_impl::log_header_t) + size);
175 char *buf_orig = buf;
176 if (buf == NULL) {
177 self->Err("err GetBuffer\n");
178 return;
179 }
180
181 FrameworkManager_impl::log_header_t header;
182 header.device = self;
183 header.size = size;
184 header.time = time;
185
186 memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
187 buf += sizeof(FrameworkManager_impl::log_header_t);
188 AppendLog(&buf);
189
190 framework_impl->WriteLog(buf_orig,
191 sizeof(FrameworkManager_impl::log_header_t) + size);
192 framework_impl->ReleaseBuffer(buf_orig);
193}
194
195void IODevice_impl::AppendLog(char **ptr) {
196 // Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
197
198 // copy state to buf
199 for (size_t i = 0; i < datasToLog.size(); i++) {
200 // printf("copy\n");
201 datasToLog.at(i)->CopyDatas(*ptr);
202 (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
203 }
204
205 // copy linked states to buf
206 for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
207 ((IODevice *)self->TypeChilds()->at(i))->pimpl_->AppendLog(ptr);
208 }
209 // copy manually added logs to buf
210 for (size_t i = 0; i < devicesToLog.size(); i++) {
211 devicesToLog.at(i)->pimpl_->AppendLog(ptr);
212 // devices.at(i)->DataToLog()->CopyDatas(*ptr);
213 //(*ptr)+=devices.at(i)->DataToLog()->Size();
214 }
215 // Printf("AppendLog %s ok\n",self->ObjectName().c_str());
216}
217
218int IODevice_impl::SetToWake(const Thread *thread) {
219 int status = 0;
220
221 wake_mutex->GetMutex();
222 if (thread_to_wake == NULL) {
223 thread_to_wake = (Thread *)thread;
224 } else {
225 status = -1;
226 }
227 wake_mutex->ReleaseMutex();
228
229 return status;
230}
Note: See TracBrowser for help on using the repository browser.