/[diana_filewatcher]/branches/smhi_tmp/src/CoFileWatcher.cc
ViewVC logotype

Contents of /branches/smhi_tmp/src/CoFileWatcher.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (show annotations) (download)
Sat Dec 7 13:53:35 2013 UTC (6 years, 2 months ago) by yngve.einarsson@smhi.se
File size: 5510 byte(s)
miString removed
1 /**
2 * coFileWatcher
3 * @author Martin Lilleeng Sćtra <martinls@met.no>
4 *
5 * $Id: CoServer4.cc,v 1.22 2007/09/04 11:00:40 martinls Exp $
6 *
7 * Copyright (C) 2007 met.no
8 *
9 * Contact information:
10 * Norwegian Meteorological Institute
11 * Box 43 Blindern
12 * 0313 OSLO
13 * NORWAY
14 * email: diana@met.no
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31 // TODO: Add support for multiple servers active on different ports (on the same node)
32 // TODO: Add support for multiple clients per server
33
34 // Qt-includes
35 #include <QtNetwork>
36 #include <QTextStream>
37 #include <qapplication.h>
38
39 #include <iostream>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44
45 #include <qUtilities/QLetterCommands.h>
46 #include <puTools/miStringFunctions.h>
47 #include "CoFileWatcher.h"
48
49 CoFileWatcher::CoFileWatcher(quint16 port, bool dm, string dir, bool vm, bool cp, string coserver_path, bool logPropFile,
50 string logPropFilename) :
51 QObject()
52 {
53 id = 0;
54 visualMode = vm;
55 watchMode = dm;
56 //fileWatcher = NULL;
57 threadPool.clear();
58 string dirs(dir);
59 vector<string> dirlist = miutil::split(dirs,",");
60 for (int i = 0; i < dirlist.size(); i++)
61 {
62 CoFileWatcherThread * theThread = new CoFileWatcherThread();
63 if (theThread != NULL)
64 {
65 // Connect the threads with
66 // this object
67 connect( theThread, SIGNAL(directoryChanged(const QString&)), this, SLOT(directoryHasChanged(const QString&)));
68 connect( theThread, SIGNAL(fileChanged(const QString&)), this, SLOT(fileHasChanged(const QString&)));
69 threadPool.push_back(theThread);
70 }
71 }
72
73 #ifdef HAVE_LOG4CXX
74 /// LOGGER
75 miutil::miString logpro;
76 if (logPropFile) {
77 logpro = logPropFilename;
78 }
79 #endif
80
81 #ifdef HAVE_LOG4CXX
82 if ( logpro.exists() ) {
83 log4cxx::PropertyConfigurator::configure(logpro.c_str());
84 } else {
85 log4cxx::BasicConfigurator::configure();
86 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getWarn());
87 }
88
89 logger = log4cxx::Logger::getLogger("coserver4.CoServer4");
90 #endif
91 cout << coserver_path.c_str() << endl;
92
93 coclient = new CoClient("filewatcher", "localhost", coserver_path.c_str());
94 coclient->setBroadcastClient();
95 coclient->connectToServer();
96
97 if (visualMode) {
98 cerr << "Visuable" << endl;
99 console = new CoConsole();
100 console->show();
101 }
102 if (watchMode) {
103 //cout << "Watch: " << dir << endl;
104 #ifdef _DEBUG
105 cout << "Started watch mode: " << dir << endl;
106 #endif
107 // Unfortunately, this don't work with the current NFS implementation in linux.
108 /*
109 QString theDir((const char *)dir.c_str());
110 QStringList paths;
111 paths.append(theDir);
112 fileWatcher = new QFileSystemWatcher(paths);
113 QString thePath;
114 // Some magical Qt tricks...
115 connect( fileWatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(directoryHasChanged(const QString&)));
116 connect( fileWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(fileHasChanged(const QString&)));
117 */
118
119 // add paths to the threads
120 // begin with one thread and one path
121 for (int i = 0; i < dirlist.size(); i++)
122 {
123 // start the listening
124 QString theDir((const char *)dirlist[i].c_str());
125 CoFileWatcherThread * theThread = threadPool[i];
126 theThread->addPath(theDir);
127 theThread->start();
128 }
129
130
131 } // end watch mode
132
133 #ifdef HAVE_LOG4CXX
134 if (isListening()) {
135 LOG4CXX_INFO(logger, "coserver4 listening on port " << port);
136 } else {
137 LOG4CXX_ERROR(logger, "Failed to bind to port");
138 }
139 #endif
140
141 }
142
143 CoFileWatcher::~CoFileWatcher()
144 {
145 for (int i = 0; i < threadPool.size(); i++)
146 {
147 // stop the listening
148 CoFileWatcherThread * theThread = threadPool[i];
149 theThread->stop = true;
150 theThread->wait();
151 delete theThread;
152 }
153 if (visualMode) {
154 console->hide();
155 delete console;
156 }
157 }
158
159 void CoFileWatcher::directoryHasChanged ( const QString & path )
160 {
161 if (visualMode)
162 console->log(path.toStdString());
163 else
164 {
165 cout << string(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toStdString()+"\n").c_str();
166 cout << string(path.toStdString() + "\n").c_str();
167 }
168 miMessage letter;
169 letter.command= qmstrings::directory_changed;
170 letter.commondesc= "directory changed";
171 letter.common = path.toStdString();
172 letter.to = qmstrings::all;
173 coclient->sendMessage(letter);
174 }
175
176 void CoFileWatcher::fileHasChanged ( const QString & path )
177 {
178 if (visualMode)
179 console->log(path.toStdString());
180 else {
181 cout << string(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toStdString()+"\n").c_str();
182 cout << string(path.toStdString() + "\n").c_str();
183 }
184 miMessage letter;
185 letter.command= qmstrings::file_changed;
186 letter.commondesc= "file changed";
187 letter.common = path.toStdString();
188 letter.to = qmstrings::all;
189 coclient->sendMessage(letter);
190 }

Properties

Name Value
svn:executable *

svn@met.no
ViewVC Help
Powered by ViewVC 1.1.5