~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to strigidaemon/lib/socketclient.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2011-09-24 17:12:15 UTC
  • mfrom: (1.2.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110924171215-zmbi1f77jntvz65h
Tags: upstream-0.7.6
ImportĀ upstreamĀ versionĀ 0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Strigi Desktop Search
 
2
 *
 
3
 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public License
 
16
 * along with this library; see the file COPYING.LIB.  If not, write to
 
17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
#include <strigi/socketclient.h>
 
21
#include <sys/types.h>
 
22
#include <sys/socket.h>
 
23
#include <sys/un.h>
 
24
#include <errno.h>
 
25
#include <assert.h>
 
26
#include <sstream>
 
27
#include <unistd.h>
 
28
#include <stdlib.h>
 
29
#include <stdio.h>
 
30
/* GCC 4.3.2 Fix */
 
31
#include <cstring>
 
32
 
 
33
 
 
34
using namespace std;
 
35
 
 
36
#if defined(__APPLE__)
 
37
#define SOCKET_NOSIGNAL SO_NOSIGPIPE
 
38
#elif defined( MSG_NOSIGNAL )
 
39
#define SOCKET_NOSIGNAL MSG_NOSIGNAL
 
40
#else
 
41
#define SOCKET_NOSIGNAL 0
 
42
#endif
 
43
 
 
44
void
 
45
SocketClient::setSocketName(const string& n) {
 
46
    socketpath = n;
 
47
}
 
48
int
 
49
SocketClient::open() {
 
50
    struct sockaddr_un  serv_addr;
 
51
 
 
52
    // create a socket
 
53
    int sd = socket(AF_UNIX, SOCK_STREAM, 0);
 
54
    if (sd < 0) {
 
55
        error = "Could not create socket: ";
 
56
        error += strerror(errno);
 
57
        return -1;
 
58
    }
 
59
 
 
60
    // set the address
 
61
    memset(&serv_addr, 0, sizeof(serv_addr));
 
62
    serv_addr.sun_family = AF_UNIX;
 
63
    size_t len = socketpath.length();
 
64
    len = (len > sizeof(serv_addr.sun_path)) ?sizeof(serv_addr.sun_path) :len;
 
65
    strncpy(serv_addr.sun_path, socketpath.c_str(), len);
 
66
    serv_addr.sun_path[len] = '\0';
 
67
 
 
68
    // connect to the server
 
69
    int r = connect(sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
 
70
    if (r < 0) {
 
71
        error = "Could not connect to server: ";
 
72
        error += strerror(errno);
 
73
        close(sd);
 
74
        return -1;
 
75
    }
 
76
    return sd;
 
77
}
 
78
bool
 
79
SocketClient::readResponse(int sd) {
 
80
    response.clear();
 
81
    string line;
 
82
    char c;
 
83
    while (true) {
 
84
        // read characters one by one
 
85
        ssize_t r = recv(sd, &c, 1, 0);
 
86
        if (r < 0) {
 
87
            error = "Error reading from socket: ";
 
88
            error += strerror(errno);
 
89
            printf("%s\n", error.c_str());
 
90
            return false;
 
91
        } else if (r == 0 || c == 0) {
 
92
            if (line.size() > 0) {
 
93
                response.push_back(line);
 
94
            }
 
95
            return true;
 
96
        } else if (c == '\n') {
 
97
            if (line.size() == 0) {
 
98
                // finished reading the request
 
99
                return true;
 
100
            }
 
101
            response.push_back(line);
 
102
            line.clear();
 
103
        } else {
 
104
            line += c;
 
105
        }
 
106
    }
 
107
}
 
108
bool
 
109
SocketClient::sendRequest(int sd) {
 
110
    ssize_t r;
 
111
    for (uint i=0; i<request.size(); ++i) {
 
112
        string line = request[i];
 
113
        assert(line.find('\n') == string::npos);
 
114
        line += '\n';
 
115
        size_t p = 0;
 
116
        size_t len = line.length();
 
117
        do {
 
118
            r = send(sd, line.c_str()+p, len-p, SOCKET_NOSIGNAL);
 
119
            if (r < 0) {
 
120
                printf("error writing request\n");
 
121
                return false;
 
122
            }
 
123
            p += r;
 
124
        } while (p < len);
 
125
    }
 
126
    r = send(sd, "\n", 1, SOCKET_NOSIGNAL);
 
127
    return r > 0;
 
128
}
 
129
int
 
130
SocketClient::countHits(const string &query) {
 
131
    response.clear();
 
132
    request.clear();
 
133
    request.push_back("countHits");
 
134
    assert(query.find("\n") == string::npos);
 
135
    request.push_back(query);
 
136
    int sd = open();
 
137
    if (sd < 0) {
 
138
        fprintf(stderr, "   %s\n", error.c_str());
 
139
        return -1;
 
140
    }
 
141
    sendRequest(sd);
 
142
    readResponse(sd);
 
143
    close(sd);
 
144
    if (response.size() == 0) return -1;
 
145
    int count = atoi(response[0].c_str());
 
146
    return count;
 
147
}
 
148
ClientInterface::Hits
 
149
SocketClient::getHits(const string &query, uint32_t max, uint32_t off) {
 
150
    response.clear();
 
151
    request.clear();
 
152
    request.push_back("query");
 
153
    assert(query.find("\n") == string::npos);
 
154
    request.push_back(query);
 
155
    ostringstream oss;
 
156
    oss << max;
 
157
    request.push_back(oss.str());
 
158
    oss.str("");
 
159
    oss << off;
 
160
    request.push_back(oss.str());
 
161
    int sd = open();
 
162
    Hits hits;
 
163
    if (sd < 0) {
 
164
        printf("   %s\n", error.c_str());
 
165
        hits.error = error;
 
166
        return hits;
 
167
    }
 
168
    sendRequest(sd);
 
169
    readResponse(sd);
 
170
    close(sd);
 
171
    uint i = 0;
 
172
    while (i+6 < response.size()) {
 
173
        Strigi::IndexedDocument h;
 
174
        h.uri = response[i++];
 
175
        h.fragment = response[i++];
 
176
        h.mimetype = response[i++];
 
177
        h.score = (float)atof(response[i++].c_str());
 
178
        h.size = atoi(response[i++].c_str());
 
179
        h.mtime = atoi(response[i++].c_str());
 
180
        while (i < response.size()) {
 
181
            const char* s = response[i].c_str();
 
182
            const char* v = strchr(s, ':');
 
183
            if (!v) break;
 
184
            const char* d = strchr(s, '/');
 
185
            if (d && d < v) {
 
186
                break;
 
187
            }
 
188
            string n(s, v-s);
 
189
            h.properties.insert(make_pair<const string,string>(n,v+1));
 
190
            ++i;
 
191
        }
 
192
        hits.hits.push_back(h);
 
193
    }
 
194
    response.clear();
 
195
    return hits;
 
196
}
 
197
map<string, string>
 
198
SocketClient::getStatus() {
 
199
    map<string, string> status;
 
200
    response.clear();
 
201
    request.clear();
 
202
    request.push_back("getStatus");
 
203
    int sd = open();
 
204
    if (sd < 0) {
 
205
        // no connection: return an empty map
 
206
        //printf("   %s\n", error.c_str());
 
207
        //status["error"] = error;
 
208
        return status;
 
209
    }
 
210
    sendRequest(sd);
 
211
    readResponse(sd);
 
212
    close(sd);
 
213
    for (uint i=0; i<response.size(); ++i) {
 
214
        string s = response[i];
 
215
        string::size_type p = s.find(":");
 
216
        if (p == string::npos) {
 
217
            printf("''%s''\n", s.c_str());
 
218
            status.clear();
 
219
            status["error"] = "Communication error.";
 
220
            return status;
 
221
        }
 
222
        status[s.substr(0,p)] = s.substr(p+1);
 
223
    }
 
224
    return status;
 
225
}
 
226
vector<string>
 
227
SocketClient::getBackEnds() {
 
228
    vector<string> v;
 
229
    return v;
 
230
}
 
231
string
 
232
SocketClient::stopDaemon() {
 
233
    request.clear();
 
234
    request.push_back("stopDaemon");
 
235
    int sd = open();
 
236
    if (sd < 0) {
 
237
        return "";
 
238
    }
 
239
    sendRequest(sd);
 
240
    readResponse(sd);
 
241
    close(sd);
 
242
    return "";
 
243
}
 
244
string
 
245
SocketClient::startIndexing() {
 
246
    request.clear();
 
247
    request.push_back("startIndexing");
 
248
    int sd = open();
 
249
    if (sd < 0) {
 
250
        return "";
 
251
    }
 
252
    sendRequest(sd);
 
253
    readResponse(sd);
 
254
    close(sd);
 
255
    return "";
 
256
}
 
257
string
 
258
SocketClient::stopIndexing() {
 
259
    request.clear();
 
260
    request.push_back("stopIndexing");
 
261
    int sd = open();
 
262
    if (sd < 0) {
 
263
        return "";
 
264
    }
 
265
    sendRequest(sd);
 
266
    readResponse(sd);
 
267
    close(sd);
 
268
    return "";
 
269
}
 
270
set<string>
 
271
SocketClient::getIndexedDirectories() {
 
272
    set<string> r;
 
273
    request.clear();
 
274
    response.clear();
 
275
    request.push_back("getIndexedDirectories");
 
276
    int sd = open();
 
277
    if (sd < 0) {
 
278
        return r;
 
279
    }
 
280
    sendRequest(sd);
 
281
    readResponse(sd);
 
282
    close(sd);
 
283
    vector<string>::const_iterator i;
 
284
    for (i = response.begin(); i != response.end(); ++i) {
 
285
        r.insert(*i);
 
286
    }
 
287
    return r;
 
288
}
 
289
string
 
290
SocketClient::setIndexedDirectories(set<string> dirs) {
 
291
    request.clear();
 
292
    request.push_back("setIndexedDirectories");
 
293
    set<string>::const_iterator i;
 
294
    for (i = dirs.begin(); i != dirs.end(); ++i) {
 
295
        request.push_back(*i);
 
296
    }
 
297
    int sd = open();
 
298
    if (sd < 0) {
 
299
        return "";
 
300
    }
 
301
    sendRequest(sd);
 
302
    readResponse(sd);
 
303
    close(sd);
 
304
    return "";
 
305
}
 
306
void
 
307
SocketClient::setFilters(const vector<pair<bool,string> >&rules){
 
308
}
 
309
vector<pair<bool,string> >
 
310
SocketClient::getFilters() {
 
311
    vector<pair<bool,string> > f;
 
312
    return f;
 
313
}
 
314
set<string>
 
315
SocketClient::getIndexedFiles() {
 
316
    set<string> r;
 
317
    request.clear();
 
318
    response.clear();
 
319
    request.push_back("getIndexedFiles");
 
320
    int sd = open();
 
321
    if (sd < 0) {
 
322
        return r;
 
323
    }
 
324
    sendRequest(sd);
 
325
    readResponse(sd);
 
326
    close(sd);
 
327
    vector<string>::const_iterator i;
 
328
    for (i = response.begin(); i != response.end(); ++i) {
 
329
        r.insert(*i);
 
330
    }
 
331
    return r;
 
332
}
 
333
void
 
334
SocketClient::indexFile(const string &path, uint64_t mtime,
 
335
        const vector<char>& content) {
 
336
    printf("so you want me to send a file to strigi?\n");
 
337
    request.clear();
 
338
    response.clear();
 
339
    request.push_back("indexFile");
 
340
    request.push_back(path);
 
341
    ostringstream out;
 
342
    out << mtime;
 
343
    request.push_back(out.str());
 
344
    request.push_back(&content[0]);
 
345
}
 
346
vector<string>
 
347
SocketClient::getFieldNames() {
 
348
    fprintf(stderr, "SocketClient::getFieldNames is not implemented yet\n");
 
349
    return vector<string>();
 
350
}
 
351
vector<pair<string, uint32_t> >
 
352
SocketClient::getHistogram(const string& query, const string& field,
 
353
        const string& labeltype) {
 
354
    fprintf(stderr, "SocketClient::getHistogram is not implemented yet\n");
 
355
    return vector<pair<string, uint32_t> >();
 
356
}
 
357
int
 
358
SocketClient::countKeywords(const string& keywordmatch,
 
359
        const vector<string>& fieldnames) {
 
360
    fprintf(stderr, "SocketClient::countKeywords is not implemented yet\n");
 
361
    return 0;
 
362
}
 
363
vector<string>
 
364
SocketClient::getKeywords(const string& keywordprefix,
 
365
        const vector<string>& fieldnames,
 
366
        uint32_t max, uint32_t offset) {
 
367
    fprintf(stderr, "SocketClient::getKeywords is not implemented yet\n");
 
368
    return vector<string>();
 
369
}