2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package com.android.im.imps;
20 import com.android.im.engine.ImErrorInfo;
22 abstract class AsyncTransaction extends ImpsTransaction {
24 private final AsyncCompletion mCompletionCallback;
25 private boolean mCompletionNotified;
26 protected final ImpsTransactionManager mTransManager;
28 AsyncTransaction(ImpsTransactionManager manager) {
32 AsyncTransaction(ImpsTransactionManager manager, AsyncCompletion completion) {
33 mTransManager = manager;
34 mCompletionCallback = completion;
35 manager.beginClientTransaction(this);
39 * Sends a request within this transaction.
41 * @param request the request to send.
43 public void sendRequest(Primitive request) {
44 sendPrimitive(request);
48 * Notify that an error occurs in the transaction.
50 * @param error the error
52 final void notifyError(ImErrorInfo error) {
53 notifyErrorResponse(new ImpsErrorInfo(error.getCode(), error.getDescription(), null));
57 * Notify that a response from the server has arrived.
59 * @param response the response.
61 final void notifyResponse(Primitive response) {
62 response.setTransaction(this);
63 ImpsErrorInfo error = ImpsUtils.checkResultError(response);
65 notifyErrorResponse(error);
67 notifySuccessResponse(response);
71 protected void notifyErrorResponse(ImpsErrorInfo error) {
72 onResponseError(error);
73 mTransManager.endClientTransaction(this);
74 notifyAsyncCompletionError(error);
77 protected void notifySuccessResponse(Primitive response) {
78 onResponseOk(response);
79 mTransManager.endClientTransaction(this);
80 notifyAsyncCompletionSuccess();
83 public abstract void onResponseError(ImpsErrorInfo error);
84 public abstract void onResponseOk(Primitive response);
86 protected void notifyAsyncCompletionError(ImErrorInfo error) {
87 if (!mCompletionNotified) {
88 mCompletionNotified = true;
89 if (mCompletionCallback != null)
90 mCompletionCallback.onError(error);
94 protected void notifyAsyncCompletionSuccess() {
95 if (!mCompletionNotified) {
96 mCompletionNotified = true;
97 if (mCompletionCallback != null)
98 mCompletionCallback.onComplete();