blob: 50ba6d19a79efde6c4bda2147341f58572e9c349 [file] [log] [blame]
Jeff Sharkeycd257fb2011-11-18 17:09:01 -08001/*
2 * Copyright (C) 2011 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
17package com.android.server;
18
Jeff Sharkey36ff7052011-11-30 18:13:54 -080019import com.google.android.collect.Lists;
20
21import java.util.ArrayList;
22
Jeff Sharkeycd257fb2011-11-18 17:09:01 -080023/**
24 * Parsed event from native side of {@link NativeDaemonConnector}.
25 */
26public class NativeDaemonEvent {
27
28 // TODO: keep class ranges in sync with ResponseCode.h
29 // TODO: swap client and server error ranges to roughly mirror HTTP spec
30
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -080031 private final int mCmdNumber;
Jeff Sharkeycd257fb2011-11-18 17:09:01 -080032 private final int mCode;
33 private final String mMessage;
34 private final String mRawEvent;
35
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -080036 private NativeDaemonEvent(int cmdNumber, int code, String message, String rawEvent) {
37 mCmdNumber = cmdNumber;
Jeff Sharkeycd257fb2011-11-18 17:09:01 -080038 mCode = code;
39 mMessage = message;
40 mRawEvent = rawEvent;
41 }
42
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -080043 public int getCmdNumber() {
44 return mCmdNumber;
45 }
46
Jeff Sharkeycd257fb2011-11-18 17:09:01 -080047 public int getCode() {
48 return mCode;
49 }
50
51 public String getMessage() {
52 return mMessage;
53 }
54
55 @Deprecated
56 public String getRawEvent() {
57 return mRawEvent;
58 }
59
60 @Override
61 public String toString() {
62 return mRawEvent;
63 }
64
65 /**
66 * Test if event represents a partial response which is continued in
67 * additional subsequent events.
68 */
69 public boolean isClassContinue() {
70 return mCode >= 100 && mCode < 200;
71 }
72
73 /**
74 * Test if event represents a command success.
75 */
76 public boolean isClassOk() {
77 return mCode >= 200 && mCode < 300;
78 }
79
80 /**
81 * Test if event represents a remote native daemon error.
82 */
83 public boolean isClassServerError() {
84 return mCode >= 400 && mCode < 500;
85 }
86
87 /**
88 * Test if event represents a command syntax or argument error.
89 */
90 public boolean isClassClientError() {
91 return mCode >= 500 && mCode < 600;
92 }
93
94 /**
95 * Test if event represents an unsolicited event from native daemon.
96 */
97 public boolean isClassUnsolicited() {
98 return mCode >= 600 && mCode < 700;
99 }
100
101 /**
Jeff Sharkey36ff7052011-11-30 18:13:54 -0800102 * Verify this event matches the given code.
103 *
104 * @throws IllegalStateException if {@link #getCode()} doesn't match.
105 */
106 public void checkCode(int code) {
107 if (mCode != code) {
108 throw new IllegalStateException("Expected " + code + " but was: " + this);
109 }
110 }
111
112 /**
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800113 * Parse the given raw event into {@link NativeDaemonEvent} instance.
114 *
115 * @throws IllegalArgumentException when line doesn't match format expected
116 * from native side.
117 */
118 public static NativeDaemonEvent parseRawEvent(String rawEvent) {
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -0800119 final String[] parsed = rawEvent.split(" ");
120 if (parsed.length < 3) {
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800121 throw new IllegalArgumentException("unable to find ' ' separator");
122 }
123
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -0800124 final int cmdNumber;
125 try {
126 cmdNumber = Integer.parseInt(parsed[0]);
127 } catch (NumberFormatException e) {
128 throw new IllegalArgumentException("problem parsing cmdNumber", e);
129 }
130
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800131 final int code;
132 try {
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -0800133 code = Integer.parseInt(parsed[1]);
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800134 } catch (NumberFormatException e) {
135 throw new IllegalArgumentException("problem parsing code", e);
136 }
137
Robert Greenwalt5c7de0b2012-02-07 11:36:55 -0800138 final String message = rawEvent.substring(parsed[0].length() + parsed[1].length() + 2);
139
140 return new NativeDaemonEvent(cmdNumber, code, message, rawEvent);
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800141 }
Jeff Sharkey36ff7052011-11-30 18:13:54 -0800142
143 /**
144 * Filter the given {@link NativeDaemonEvent} list, returning
145 * {@link #getMessage()} for any events matching the requested code.
146 */
147 public static String[] filterMessageList(NativeDaemonEvent[] events, int matchCode) {
148 final ArrayList<String> result = Lists.newArrayList();
149 for (NativeDaemonEvent event : events) {
150 if (event.getCode() == matchCode) {
151 result.add(event.getMessage());
152 }
153 }
154 return result.toArray(new String[result.size()]);
155 }
Jeff Sharkeycd257fb2011-11-18 17:09:01 -0800156}