[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;
|
---|
[2] | 41 | }
|
---|
| 42 |
|
---|
| 43 | IODevice_impl::~IODevice_impl() {}
|
---|
| 44 |
|
---|
| 45 | void IODevice_impl::OutputToShMem(bool enabled) {
|
---|
[15] | 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 | }
|
---|
[2] | 53 |
|
---|
[15] | 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;
|
---|
[2] | 59 |
|
---|
[15] | 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;
|
---|
[2] | 66 | }
|
---|
[15] | 67 | }
|
---|
[2] | 68 | }
|
---|
| 69 |
|
---|
| 70 | void IODevice_impl::PrintLogsDescriptors(void) {
|
---|
[15] | 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();
|
---|
[2] | 80 | }
|
---|
| 81 |
|
---|
| 82 | void IODevice_impl::WriteToShMem(void) {
|
---|
[15] | 83 | if (outputtoshm) {
|
---|
| 84 | size_t size = LogSize();
|
---|
[2] | 85 |
|
---|
[15] | 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 | }
|
---|
[2] | 92 |
|
---|
[15] | 93 | AppendLog(&buf);
|
---|
[2] | 94 |
|
---|
[15] | 95 | shmem->Write(buf_orig, size);
|
---|
| 96 | framework_impl->ReleaseBuffer(buf_orig);
|
---|
| 97 | }
|
---|
[2] | 98 | }
|
---|
| 99 |
|
---|
[15] | 100 | void 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 | }
|
---|
[2] | 106 | }
|
---|
| 107 |
|
---|
[122] | 108 | bool IODevice_impl::IsSetToBeLogged(void) const{
|
---|
| 109 | return tobelogged;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void IODevice_impl::SetToBeLogged(void) {
|
---|
[15] | 113 | if (!tobelogged) {
|
---|
| 114 | tobelogged = true;
|
---|
| 115 | } else {
|
---|
| 116 | self->Warn("already added to log\n");
|
---|
| 117 | }
|
---|
[2] | 118 | }
|
---|
| 119 |
|
---|
[15] | 120 | void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
|
---|
[122] | 121 | //Printf("WriteLogsDescriptors %s\n",self->ObjectName().c_str());
|
---|
| 122 | // own descriptor
|
---|
[15] | 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);
|
---|
[122] | 132 | //Printf("WriteLogsDescriptors %s ok\n",self->ObjectName().c_str());
|
---|
[2] | 133 | }
|
---|
| 134 |
|
---|
| 135 | void IODevice_impl::ResumeThread(void) {
|
---|
[15] | 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();
|
---|
[2] | 142 | }
|
---|
| 143 |
|
---|
[15] | 144 | void 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 | }
|
---|
[2] | 150 | }
|
---|
| 151 |
|
---|
| 152 | size_t IODevice_impl::LogSize() const {
|
---|
[15] | 153 | size_t value = 0;
|
---|
[2] | 154 |
|
---|
[15] | 155 | for (size_t i = 0; i < datasToLog.size(); i++) {
|
---|
| 156 | value += datasToLog.at(i)->GetDataType().GetSize();
|
---|
| 157 | }
|
---|
[2] | 158 |
|
---|
[15] | 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 | }
|
---|
[2] | 167 |
|
---|
[15] | 168 | return value;
|
---|
[2] | 169 | }
|
---|
| 170 |
|
---|
| 171 | void IODevice_impl::WriteLog(Time time) {
|
---|
[15] | 172 | if (tobelogged == false)
|
---|
| 173 | return;
|
---|
[2] | 174 |
|
---|
[15] | 175 | size_t size = LogSize();
|
---|
[2] | 176 |
|
---|
[15] | 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 | }
|
---|
[2] | 184 |
|
---|
[15] | 185 | FrameworkManager_impl::log_header_t header;
|
---|
| 186 | header.device = self;
|
---|
| 187 | header.size = size;
|
---|
| 188 | header.time = time;
|
---|
[2] | 189 |
|
---|
[15] | 190 | memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
|
---|
| 191 | buf += sizeof(FrameworkManager_impl::log_header_t);
|
---|
| 192 | AppendLog(&buf);
|
---|
[2] | 193 |
|
---|
[15] | 194 | framework_impl->WriteLog(buf_orig,
|
---|
| 195 | sizeof(FrameworkManager_impl::log_header_t) + size);
|
---|
| 196 | framework_impl->ReleaseBuffer(buf_orig);
|
---|
[2] | 197 | }
|
---|
| 198 |
|
---|
[15] | 199 | void IODevice_impl::AppendLog(char **ptr) {
|
---|
[122] | 200 | //Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
|
---|
[2] | 201 |
|
---|
[15] | 202 | // copy state to buf
|
---|
| 203 | for (size_t i = 0; i < datasToLog.size(); i++) {
|
---|
[122] | 204 | // printf("copy %s\n",datasToLog.at(i)->ObjectName().c_str());
|
---|
[15] | 205 | datasToLog.at(i)->CopyDatas(*ptr);
|
---|
| 206 | (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
|
---|
| 207 | }
|
---|
[2] | 208 |
|
---|
[15] | 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());
|
---|
[2] | 220 | }
|
---|
| 221 |
|
---|
[15] | 222 | int IODevice_impl::SetToWake(const Thread *thread) {
|
---|
| 223 | int status = 0;
|
---|
[2] | 224 |
|
---|
[15] | 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();
|
---|
[2] | 232 |
|
---|
[15] | 233 | return status;
|
---|
[2] | 234 | }
|
---|