Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | /** |
| 20 | * Parsed event from native side of {@link NativeDaemonConnector}. |
| 21 | */ |
| 22 | public class NativeDaemonEvent { |
| 23 | |
| 24 | // TODO: keep class ranges in sync with ResponseCode.h |
| 25 | // TODO: swap client and server error ranges to roughly mirror HTTP spec |
| 26 | |
| 27 | private final int mCode; |
| 28 | private final String mMessage; |
| 29 | private final String mRawEvent; |
| 30 | |
| 31 | private NativeDaemonEvent(int code, String message, String rawEvent) { |
| 32 | mCode = code; |
| 33 | mMessage = message; |
| 34 | mRawEvent = rawEvent; |
| 35 | } |
| 36 | |
| 37 | public int getCode() { |
| 38 | return mCode; |
| 39 | } |
| 40 | |
| 41 | public String getMessage() { |
| 42 | return mMessage; |
| 43 | } |
| 44 | |
| 45 | @Deprecated |
| 46 | public String getRawEvent() { |
| 47 | return mRawEvent; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String toString() { |
| 52 | return mRawEvent; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Test if event represents a partial response which is continued in |
| 57 | * additional subsequent events. |
| 58 | */ |
| 59 | public boolean isClassContinue() { |
| 60 | return mCode >= 100 && mCode < 200; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Test if event represents a command success. |
| 65 | */ |
| 66 | public boolean isClassOk() { |
| 67 | return mCode >= 200 && mCode < 300; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Test if event represents a remote native daemon error. |
| 72 | */ |
| 73 | public boolean isClassServerError() { |
| 74 | return mCode >= 400 && mCode < 500; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Test if event represents a command syntax or argument error. |
| 79 | */ |
| 80 | public boolean isClassClientError() { |
| 81 | return mCode >= 500 && mCode < 600; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Test if event represents an unsolicited event from native daemon. |
| 86 | */ |
| 87 | public boolean isClassUnsolicited() { |
| 88 | return mCode >= 600 && mCode < 700; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Parse the given raw event into {@link NativeDaemonEvent} instance. |
| 93 | * |
| 94 | * @throws IllegalArgumentException when line doesn't match format expected |
| 95 | * from native side. |
| 96 | */ |
| 97 | public static NativeDaemonEvent parseRawEvent(String rawEvent) { |
| 98 | final int splitIndex = rawEvent.indexOf(' '); |
| 99 | if (splitIndex == -1) { |
| 100 | throw new IllegalArgumentException("unable to find ' ' separator"); |
| 101 | } |
| 102 | |
| 103 | final int code; |
| 104 | try { |
| 105 | code = Integer.parseInt(rawEvent.substring(0, splitIndex)); |
| 106 | } catch (NumberFormatException e) { |
| 107 | throw new IllegalArgumentException("problem parsing code", e); |
| 108 | } |
| 109 | |
| 110 | final String message = rawEvent.substring(splitIndex + 1); |
| 111 | return new NativeDaemonEvent(code, message, rawEvent); |
| 112 | } |
| 113 | } |