00001
00002 #include <stdexcept>
00003 #include <dvutil/file.h>
00004 #include <dvutil/tostring.h>
00005 #include <dvnet/socket.h>
00006
00007 using namespace Dv;
00008
00009 int
00010 add(Net::Socket& server, const string& path) {
00011
00012
00013
00014
00015 int n(0);
00016 Util::File f(path);
00017 f.expand();
00018 if (f.type() == Util::File::DIRECTORY) {
00019 try {
00020 Util::Directory d(f);
00021 const vector<string>& files(d.files());
00022 for (unsigned int i=0; i<files.size(); ++i)
00023 n += add(server, d.file(files[i]));
00024 return n;
00025 }
00026 catch (...) {
00027 return n;
00028 }
00029 }
00030 server << "add " << path << endl;
00031 string reply;
00032 getline(server, reply);
00033 return n + (reply == "OK" ? 1 : 0 );
00034 }
00035
00036
00037 int
00038 main( int argc , char* argv[] ) {
00039 static const string usage("usage: ffiles host port command");
00040 static const string error("ERROR ");
00041 try {
00042
00043 if (argc<4)
00044 throw runtime_error(usage);
00045
00046 const string host(argv[1]);
00047 const int port(atoi(argv[2]));
00048 const string cmd(argv[3]);
00049
00050
00051
00052 Net::Socket server(host, port);
00053 if (!server)
00054 throw runtime_error(Util::tostring(port) + "@" + host + ": " +
00055 server.strerror());
00056
00057
00058
00059 if ( cmd=="stop" || cmd == "save" ) {
00060 string reply;
00061 server << cmd << endl;
00062 getline(server, reply);
00063 cout << reply << endl;
00064 }
00065 else if (cmd == "add") {
00066 if (argc<5)
00067 throw runtime_error(usage);
00068 const string path(argv[4]);
00069 cout << add(server, path) << " files added" << endl;
00070 }
00071 else if (cmd == "query") {
00072 if (argc<5)
00073 throw runtime_error(usage);
00074 const string query(argv[4]);
00075 server << cmd << ' ' << query << endl;
00076 string reply;
00077 while (server) {
00078 getline(server,reply);
00079 if (reply.size()==0)
00080 break;
00081 cout << reply << endl;
00082 }
00083 }
00084 else
00085 throw runtime_error(cmd + ": bad command");
00086 }
00087 catch (exception& e) {
00088 cerr << error << e.what() << endl;
00089 return 1;
00090 }
00091 return 0;
00092 }