00001 00012 /* 00013 00014 Copyright (c) 2007, Tim Burrell 00015 Licensed under the Apache License, Version 2.0 (the "License"); 00016 you may not use this file except in compliance with the License. 00017 You may obtain a copy of the License at 00018 00019 http://www.apache.org/licenses/LICENSE-2.0 00020 00021 Unless required by applicable law or agreed to in writing, software 00022 distributed under the License is distributed on an "AS IS" BASIS, 00023 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00024 See the License for the specific language governing permissions and 00025 limitations under the License. 00026 00027 */ 00028 00029 #include "GizmodLibVisualPlugin.hpp" 00030 #include "../libGizmod/GizmoEventSoundVisualization.hpp" 00031 #include "../libH/Debug.hpp" 00032 #include "../libH/Exception.hpp" 00033 #include "../libH/SocketException.hpp" 00034 #include "../libH/UtilFile.hpp" 00035 #include <fstream> 00036 00037 using namespace std; 00038 using namespace H; 00039 using namespace Gizmod; 00040 00042 // Type Defs 00044 00049 #define DEFAULT_HOST "localhost" 00050 00055 #define DEFAULT_PORT 30303 00056 00061 #define CONFIG_FILE "~/.gizmod/libVisualPlugin.config" 00062 00064 // Construction 00066 00070 GizmodLibVisualPlugin::GizmodLibVisualPlugin() { 00071 mServerHost = DEFAULT_HOST; 00072 mServerPort = DEFAULT_PORT; 00073 //Debug::setDebugEnabled(true); 00074 //Debug::setDebugLog("/tmp/actor_gizmod.log"); 00075 } 00076 00080 GizmodLibVisualPlugin::~GizmodLibVisualPlugin() { 00081 closeSocket(); 00082 } 00083 00085 // Class Body 00087 00091 void GizmodLibVisualPlugin::init() { 00092 cdbg << "Init" << endl; 00093 readConfig(); 00094 00095 // initialize the client 00096 try { 00097 cdbg << "Connecting to [" << mServerHost << "] at Port [" << mServerPort << "]..." << endl; 00098 connectToServer(mServerHost, mServerPort); 00099 cdbg << "Connected to [" << mServerHost << "] at Port [" << mServerPort << "]..." << endl; 00100 GizmoEventSoundVisualization Event(SOUNDVISUALIZATION_CONNECT); 00101 sendEventSoundVisualization(Event); 00102 } catch (SocketException const & e) { 00103 cdbg << e.getExceptionMessage() << endl; 00104 } 00105 } 00106 00110 void GizmodLibVisualPlugin::readConfig() { 00111 string ConfigFile = CONFIG_FILE; 00112 UtilFile::relativeToAbsolute(ConfigFile); 00113 cdbg << "Read Config [" << ConfigFile << "]" << endl; 00114 ifstream ifs(ConfigFile.c_str()); 00115 if (!ifs.is_open()) { 00116 writeConfig(); 00117 return; 00118 } 00119 00120 string Line; 00121 while (getline(ifs, Line)) { 00122 size_t eqPos = Line.find("="); 00123 if (eqPos == string::npos) 00124 continue; 00125 string Var = Line.substr(0, eqPos); 00126 string Val = Line.substr(eqPos + 1); 00127 00128 if (Var == "host") 00129 mServerHost = Val; 00130 else if (Var == "port") 00131 mServerPort = atoi(Val.c_str()); 00132 } 00133 } 00134 00138 void GizmodLibVisualPlugin::shutdown() { 00139 GizmoEventSoundVisualization Event(SOUNDVISUALIZATION_DISCONNECT); 00140 sendEventSoundVisualization(Event); 00141 cdbg << "Shutdown" << endl; 00142 } 00143 00150 void GizmodLibVisualPlugin::render(float VULeft, float VURight, float VUCombined) { 00151 GizmoEventSoundVisualization Event(VULeft, VURight, VUCombined); 00152 sendEventSoundVisualization(Event); 00153 } 00154 00158 void GizmodLibVisualPlugin::writeConfig() { 00159 string ConfigFile = CONFIG_FILE; 00160 UtilFile::relativeToAbsolute(ConfigFile); 00161 cdbg << "Write Config [" << ConfigFile << "]" << endl; 00162 UtilFile::touchRecursive(ConfigFile); 00163 ofstream ofs(ConfigFile.c_str()); 00164 if (!ofs.is_open()) 00165 return; 00166 ofs << "host=" << DEFAULT_HOST << endl; 00167 ofs << "port=" << DEFAULT_PORT << endl; 00168 }