blob: b920215f6a7d85d5781387f53713f8fbcf7f83dc [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <errno.h>
San Mehat3d407292009-05-07 08:49:30 -070017#include <string.h>
San Mehat168415b2009-05-06 11:14:21 -070018
19#define LOG_TAG "FrameworkListener"
20
21#include <cutils/log.h>
22
23#include <sysutils/FrameworkListener.h>
24#include <sysutils/FrameworkCommand.h>
25
26FrameworkListener::FrameworkListener(const char *socketName) :
27 SocketListener(socketName, true) {
28 mCommands = new FrameworkCommandCollection();
29}
30
31bool FrameworkListener::onDataAvailable(int socket) {
32 char buffer[101];
33 int len;
34
35 if ((len = read(socket, buffer, sizeof(buffer) -1)) < 0) {
36 LOGE("read() failed (%s)", strerror(errno));
37 return errno;
38 } else if (!len) {
39 LOGW("Lost connection to client");
40 return false;
41 }
42
43 int start = 0;
44 int i;
45
46 buffer[len] = '\0';
47
48 for (i = 0; i < len; i++) {
49 if (buffer[i] == '\0') {
50 dispatchCommand(buffer + start);
51 start = i + 1;
52 }
53 }
54 return true;
55}
56
57void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
58 mCommands->push_back(cmd);
59}
60
61void FrameworkListener::dispatchCommand(char *cmd) {
62 FrameworkCommandCollection::iterator i;
63
64 for (i = mCommands->begin(); i != mCommands->end(); ++i) {
65 FrameworkCommand *c = *i;
66
67 if (!strncmp(cmd, c->getCommand(), strlen(c->getCommand()))) {
68 if (c->runCommand(cmd)) {
69 LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
70 }
71 return;
72 }
73 }
74
75 LOGE("No cmd handlers defined for '%s'", cmd);
76}
77