00001 /* 00002 * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz> 00003 * 00004 * This file is part of rob08 00005 * 00006 * rob08 is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * any later version. 00010 * 00011 * rob08 is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with rob08. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "config.h" 00021 #include "coresync.h" 00022 00023 #include "geometry.h" 00024 #include "robIO.h" 00025 #include "vscan.h" 00026 #include "trilobot.h" 00027 00028 #ifndef BUILDING_DOX 00029 # include <QMutex> 00030 # include <sstream> 00031 #endif 00032 00033 using std::string; 00034 using namespace StreamDecorator; 00035 00036 // ///////////////////////////////////////////////////////////////////////////// 00037 // CoreSync implementation 00038 struct CoreSync::Private { 00039 ICore *core; 00040 QMutex mutex; 00041 }; 00042 CoreSync::CoreSync(ICore *core): 00043 d(new Private) 00044 { 00045 d->core = core; 00046 } 00047 CoreSync::~CoreSync() { 00048 delete d; 00049 } 00050 ICore* CoreSync::getCore() { 00051 return d->core; 00052 } 00053 void CoreSync::sync(const std::string &data) { 00054 std::istringstream stream(data); 00055 d->mutex.lock(); 00056 #if DEBUG_MUTEX 00057 std::cerr 00058 << Color(C_LIGHT_RED) << "CoreSync" << Color(C_NO_COLOR) << ": " 00059 << Color(C_YELLOW) << "locked for sync ... " << Color(C_NO_COLOR) 00060 << std::flush; 00061 #endif 00062 d->core->Import(stream); 00063 #if DEBUG_MUTEX 00064 std::cerr 00065 << Color(C_LIGHT_BLUE) << "unlocked" << Color(C_NO_COLOR) 00066 << std::endl; 00067 #endif 00068 d->mutex.unlock(); 00069 } 00070 00071 void CoreSync::lockForVis() { 00072 d->mutex.lock(); 00073 #if DEBUG_MUTEX 00074 std::cerr 00075 << Color(C_LIGHT_RED) << "CoreSync" << Color(C_NO_COLOR) << ": " 00076 << Color(C_LIGHT_CYAN) << "locked by GUI ... " << Color(C_NO_COLOR) 00077 << std::flush; 00078 #endif 00079 } 00080 00081 void CoreSync::unlockAfterVis() { 00082 #if DEBUG_MUTEX 00083 std::cerr 00084 << Color(C_LIGHT_BLUE) << "unlocked" << Color(C_NO_COLOR) 00085 << std::endl; 00086 #endif 00087 d->mutex.unlock(); 00088 } 00089 00090 // ///////////////////////////////////////////////////////////////////////////// 00091 // CoreInfo implementation 00092 void readCoreInfo(LockedCore &core, CoreInfo &info) { 00093 Trilobot &bot = core->GetTrilobot(); 00094 const Position &pos = bot.Position(); 00095 00096 info.x = pos.x; 00097 info.y = pos.y; 00098 info.angle = pos.angle; 00099 info.distance = bot.Drive().Distance(); 00100 info.speed = bot.Speed(); 00101 info.radius = bot.Radius(); 00102 } 00103 00104 // ///////////////////////////////////////////////////////////////////////////// 00105 // SyncReader implementation 00106 struct SyncReader::Private { 00107 IVScan *scan; 00108 CoreSync *core; 00109 bool syncExit; 00110 }; 00111 SyncReader::SyncReader(std::istream &input, CoreSync *cs): 00112 d(new Private) 00113 { 00114 d->scan = VScanFactory::createVScan(input, "-"); 00115 d->core = cs; 00116 d->syncExit = false; 00117 } 00118 SyncReader::~SyncReader() { 00119 delete d->scan; 00120 delete d; 00121 } 00122 00123 void SyncReader::syncExit() { 00124 d->syncExit = true; 00125 } 00126 00127 bool SyncReader::hasError() const { 00128 return d->scan->hasError(); 00129 } 00130 00131 void SyncReader::run() { 00132 string data; 00133 while (d->scan->readNext(data)) { 00134 if (d->syncExit) { 00135 #if DEBUG_THREAD 00136 std::cerr 00137 << Color(C_LIGHT_RED) << "SyncReader" << Color(C_NO_COLOR) << ": " 00138 << Color(C_YELLOW) << "syncExit called, exiting..." << Color(C_NO_COLOR) 00139 << std::endl; 00140 #endif 00141 return; 00142 } 00143 d->core->sync(data); 00144 } 00145 #if DEBUG_THREAD 00146 std::cerr 00147 << Color(C_LIGHT_RED) << "SyncReader" << Color(C_NO_COLOR) << ": " 00148 << Color(C_LIGHT_GREEN) << "end of input, exiting..." << Color(C_NO_COLOR) 00149 << std::endl; 00150 #endif 00151 } 00152
1.5.4