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

Last change on this file since 4 was 2, checked in by Sanahuja Guillaume, 8 years ago

flaircore

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