[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 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 |
|
---|
| 29 | using std::string;
|
---|
| 30 | using std::fstream;
|
---|
| 31 | using namespace flair::core;
|
---|
| 32 |
|
---|
[15] | 33 | IODevice_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;
|
---|
[157] | 41 | isReady=false;
|
---|
[2] | 42 | }
|
---|
| 43 |
|
---|
| 44 | IODevice_impl::~IODevice_impl() {}
|
---|
| 45 |
|
---|
| 46 | void IODevice_impl::OutputToShMem(bool enabled) {
|
---|
[15] | 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 | }
|
---|
[2] | 54 |
|
---|
[15] | 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;
|
---|
[2] | 60 |
|
---|
[15] | 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;
|
---|
[2] | 67 | }
|
---|
[15] | 68 | }
|
---|
[2] | 69 | }
|
---|
| 70 |
|
---|
| 71 | void IODevice_impl::PrintLogsDescriptors(void) {
|
---|
[15] | 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();
|
---|
[2] | 81 | }
|
---|
| 82 |
|
---|
| 83 | void IODevice_impl::WriteToShMem(void) {
|
---|
[15] | 84 | if (outputtoshm) {
|
---|
| 85 | size_t size = LogSize();
|
---|
[2] | 86 |
|
---|
[15] | 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 | }
|
---|
[2] | 93 |
|
---|
[15] | 94 | AppendLog(&buf);
|
---|
[2] | 95 |
|
---|
[15] | 96 | shmem->Write(buf_orig, size);
|
---|
| 97 | framework_impl->ReleaseBuffer(buf_orig);
|
---|
| 98 | }
|
---|
[2] | 99 | }
|
---|
| 100 |
|
---|
[15] | 101 | void 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 | }
|
---|
[2] | 107 | }
|
---|
| 108 |
|
---|
[122] | 109 | bool IODevice_impl::IsSetToBeLogged(void) const{
|
---|
| 110 | return tobelogged;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void IODevice_impl::SetToBeLogged(void) {
|
---|
[15] | 114 | if (!tobelogged) {
|
---|
| 115 | tobelogged = true;
|
---|
| 116 | } else {
|
---|
| 117 | self->Warn("already added to log\n");
|
---|
| 118 | }
|
---|
[2] | 119 | }
|
---|
| 120 |
|
---|
[15] | 121 | void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
|
---|
[122] | 122 | //Printf("WriteLogsDescriptors %s\n",self->ObjectName().c_str());
|
---|
| 123 | // own descriptor
|
---|
[15] | 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);
|
---|
[122] | 133 | //Printf("WriteLogsDescriptors %s ok\n",self->ObjectName().c_str());
|
---|
[2] | 134 | }
|
---|
| 135 |
|
---|
| 136 | void IODevice_impl::ResumeThread(void) {
|
---|
[15] | 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();
|
---|
[2] | 143 | }
|
---|
| 144 |
|
---|
[15] | 145 | void 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 | }
|
---|
[2] | 151 | }
|
---|
| 152 |
|
---|
| 153 | size_t IODevice_impl::LogSize() const {
|
---|
[15] | 154 | size_t value = 0;
|
---|
[2] | 155 |
|
---|
[15] | 156 | for (size_t i = 0; i < datasToLog.size(); i++) {
|
---|
| 157 | value += datasToLog.at(i)->GetDataType().GetSize();
|
---|
| 158 | }
|
---|
[2] | 159 |
|
---|
[15] | 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 | }
|
---|
[2] | 168 |
|
---|
[15] | 169 | return value;
|
---|
[2] | 170 | }
|
---|
| 171 |
|
---|
| 172 | void IODevice_impl::WriteLog(Time time) {
|
---|
[15] | 173 | if (tobelogged == false)
|
---|
| 174 | return;
|
---|
[2] | 175 |
|
---|
[15] | 176 | size_t size = LogSize();
|
---|
[2] | 177 |
|
---|
[15] | 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 | }
|
---|
[2] | 185 |
|
---|
[15] | 186 | FrameworkManager_impl::log_header_t header;
|
---|
| 187 | header.device = self;
|
---|
| 188 | header.size = size;
|
---|
| 189 | header.time = time;
|
---|
[2] | 190 |
|
---|
[15] | 191 | memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
|
---|
| 192 | buf += sizeof(FrameworkManager_impl::log_header_t);
|
---|
| 193 | AppendLog(&buf);
|
---|
[2] | 194 |
|
---|
[15] | 195 | framework_impl->WriteLog(buf_orig,
|
---|
| 196 | sizeof(FrameworkManager_impl::log_header_t) + size);
|
---|
| 197 | framework_impl->ReleaseBuffer(buf_orig);
|
---|
[2] | 198 | }
|
---|
| 199 |
|
---|
[15] | 200 | void IODevice_impl::AppendLog(char **ptr) {
|
---|
[122] | 201 | //Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
|
---|
[2] | 202 |
|
---|
[15] | 203 | // copy state to buf
|
---|
| 204 | for (size_t i = 0; i < datasToLog.size(); i++) {
|
---|
[122] | 205 | // printf("copy %s\n",datasToLog.at(i)->ObjectName().c_str());
|
---|
[252] | 206 | datasToLog.at(i)->RawRead(*ptr);
|
---|
[15] | 207 | (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
|
---|
| 208 | }
|
---|
[2] | 209 |
|
---|
[15] | 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());
|
---|
[2] | 221 | }
|
---|
| 222 |
|
---|
[15] | 223 | int IODevice_impl::SetToWake(const Thread *thread) {
|
---|
| 224 | int status = 0;
|
---|
[2] | 225 |
|
---|
[15] | 226 | wake_mutex->GetMutex();
|
---|
[330] | 227 | if(thread==NULL) {
|
---|
| 228 | thread_to_wake=NULL;
|
---|
[15] | 229 | } else {
|
---|
[330] | 230 | if (thread_to_wake == NULL) {
|
---|
| 231 | thread_to_wake = (Thread *)thread;
|
---|
| 232 | } else {
|
---|
| 233 | status = -1;
|
---|
| 234 | }
|
---|
[15] | 235 | }
|
---|
| 236 | wake_mutex->ReleaseMutex();
|
---|
[2] | 237 |
|
---|
[15] | 238 | return status;
|
---|
[2] | 239 | }
|
---|
[157] | 240 |
|
---|
| 241 | void IODevice_impl::SetIsReady(bool status) {
|
---|
| 242 | isReady=status;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | bool IODevice_impl::IsReady(void) const {
|
---|
| 246 | return isReady;
|
---|
| 247 | } |
---|