blob: 62084c072abbe8f974b719274aac032fde321a75 [file] [log] [blame]
Jeff Sharkey31c6e482011-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 Sharkeyba2896e2011-11-30 18:13:54 -080019import com.google.android.collect.Lists;
20
21import java.util.ArrayList;
22
Jeff Sharkey31c6e482011-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
31 private final int mCode;
32 private final String mMessage;
33 private final String mRawEvent;
34
35 private NativeDaemonEvent(int code, String message, String rawEvent) {
36 mCode = code;
37 mMessage = message;
38 mRawEvent = rawEvent;
39 }
40
41 public int getCode() {
42 return mCode;
43 }
44
45 public String getMessage() {
46 return mMessage;
47 }
48
49 @Deprecated
50 public String getRawEvent() {
51 return mRawEvent;
52 }
53
54 @Override
55 public String toString() {
56 return mRawEvent;
57 }
58
59 /**
60 * Test if event represents a partial response which is continued in
61 * additional subsequent events.
62 */
63 public boolean isClassContinue() {
64 return mCode >= 100 && mCode < 200;
65 }
66
67 /**
68 * Test if event represents a command success.
69 */
70 public boolean isClassOk() {
71 return mCode >= 200 && mCode < 300;
72 }
73
74 /**
75 * Test if event represents a remote native daemon error.
76 */
77 public boolean isClassServerError() {
78 return mCode >= 400 && mCode < 500;
79 }
80
81 /**
82 * Test if event represents a command syntax or argument error.
83 */
84 public boolean isClassClientError() {
85 return mCode >= 500 && mCode < 600;
86 }
87
88 /**
89 * Test if event represents an unsolicited event from native daemon.
90 */
91 public boolean isClassUnsolicited() {
92 return mCode >= 600 && mCode < 700;
93 }
94
95 /**
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080096 * Verify this event matches the given code.
97 *
98 * @throws IllegalStateException if {@link #getCode()} doesn't match.
99 */
100 public void checkCode(int code) {
101 if (mCode != code) {
102 throw new IllegalStateException("Expected " + code + " but was: " + this);
103 }
104 }
105
106 /**
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800107 * Parse the given raw event into {@link NativeDaemonEvent} instance.
108 *
109 * @throws IllegalArgumentException when line doesn't match format expected
110 * from native side.
111 */
112 public static NativeDaemonEvent parseRawEvent(String rawEvent) {
113 final int splitIndex = rawEvent.indexOf(' ');
114 if (splitIndex == -1) {
115 throw new IllegalArgumentException("unable to find ' ' separator");
116 }
117
118 final int code;
119 try {
120 code = Integer.parseInt(rawEvent.substring(0, splitIndex));
121 } catch (NumberFormatException e) {
122 throw new IllegalArgumentException("problem parsing code", e);
123 }
124
125 final String message = rawEvent.substring(splitIndex + 1);
126 return new NativeDaemonEvent(code, message, rawEvent);
127 }
Jeff Sharkeyba2896e2011-11-30 18:13:54 -0800128
129 /**
130 * Filter the given {@link NativeDaemonEvent} list, returning
131 * {@link #getMessage()} for any events matching the requested code.
132 */
133 public static String[] filterMessageList(NativeDaemonEvent[] events, int matchCode) {
134 final ArrayList<String> result = Lists.newArrayList();
135 for (NativeDaemonEvent event : events) {
136 if (event.getCode() == matchCode) {
137 result.add(event.getMessage());
138 }
139 }
140 return result.toArray(new String[result.size()]);
141 }
Jeff Sharkey31c6e482011-11-18 17:09:01 -0800142}