blob: eb96c9acb91c3b7a9b11597461628c87c2747779 [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>
San Mehatfa644ff2009-05-08 11:15:53 -070025#include <sysutils/SocketClient.h>
San Mehat168415b2009-05-06 11:14:21 -070026
27FrameworkListener::FrameworkListener(const char *socketName) :
28 SocketListener(socketName, true) {
29 mCommands = new FrameworkCommandCollection();
30}
31
San Mehatfa644ff2009-05-08 11:15:53 -070032bool FrameworkListener::onDataAvailable(SocketClient *c) {
San Mehatd7680662009-05-12 11:16:59 -070033 char buffer[255];
San Mehat168415b2009-05-06 11:14:21 -070034 int len;
35
San Mehatfa644ff2009-05-08 11:15:53 -070036 if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
San Mehat168415b2009-05-06 11:14:21 -070037 LOGE("read() failed (%s)", strerror(errno));
38 return errno;
39 } else if (!len) {
40 LOGW("Lost connection to client");
41 return false;
42 }
43
San Mehatd7680662009-05-12 11:16:59 -070044 int offset = 0;
San Mehat168415b2009-05-06 11:14:21 -070045 int i;
46
San Mehat168415b2009-05-06 11:14:21 -070047 for (i = 0; i < len; i++) {
San Mehatd7680662009-05-12 11:16:59 -070048 if (buffer[i] == '\n') {
49 buffer[i] = '\0';
50 dispatchCommand(c, buffer + offset);
51 offset = i + 1;
San Mehat168415b2009-05-06 11:14:21 -070052 }
53 }
54 return true;
55}
56
57void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
58 mCommands->push_back(cmd);
59}
60
San Mehatfa644ff2009-05-08 11:15:53 -070061void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
San Mehatd7680662009-05-12 11:16:59 -070062 LOGD("Dispatching '%s'", cmd);
San Mehatfa644ff2009-05-08 11:15:53 -070063 char *cm, *last;
64
65 if (!(cm = strtok_r(cmd, ":", &last))) {
San Mehatd7680662009-05-12 11:16:59 -070066 cli->sendMsg(500, "Malformatted message", false);
San Mehatfa644ff2009-05-08 11:15:53 -070067 return;
68 }
69
San Mehat168415b2009-05-06 11:14:21 -070070 FrameworkCommandCollection::iterator i;
71
72 for (i = mCommands->begin(); i != mCommands->end(); ++i) {
73 FrameworkCommand *c = *i;
74
San Mehatfa644ff2009-05-08 11:15:53 -070075 if (!strcmp(cm, c->getCommand())) {
San Mehatd5305922009-05-12 14:35:15 -070076 cm += strlen(cm) +1;
77 if (c->runCommand(cli, cm)) {
San Mehat168415b2009-05-06 11:14:21 -070078 LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
79 }
80 return;
81 }
82 }
83
San Mehatd7680662009-05-12 11:16:59 -070084 cli->sendMsg(500, "Command not recognized", false);
San Mehatfa644ff2009-05-08 11:15:53 -070085 return;
San Mehat168415b2009-05-06 11:14:21 -070086}