• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Esmertec AG.
3  * Copyright (C) 2007 The Android Open Source Project
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 package com.android.im.imps;
19 
20 import com.android.im.engine.ImErrorInfo;
21 
22 abstract class AsyncTransaction extends ImpsTransaction {
23 
24     private final AsyncCompletion mCompletionCallback;
25     private boolean mCompletionNotified;
26     protected final ImpsTransactionManager mTransManager;
27 
AsyncTransaction(ImpsTransactionManager manager)28     AsyncTransaction(ImpsTransactionManager manager) {
29         this(manager, null);
30     }
31 
AsyncTransaction(ImpsTransactionManager manager, AsyncCompletion completion)32     AsyncTransaction(ImpsTransactionManager manager, AsyncCompletion completion) {
33         mTransManager = manager;
34         mCompletionCallback = completion;
35         manager.beginClientTransaction(this);
36     }
37 
38     /**
39      * Sends a request within this transaction.
40      *
41      * @param request the request to send.
42      */
sendRequest(Primitive request)43     public void sendRequest(Primitive request) {
44         sendPrimitive(request);
45     }
46 
47     /**
48      * Notify that an error occurs in the transaction.
49      *
50      * @param error the error
51      */
notifyError(ImErrorInfo error)52     final void notifyError(ImErrorInfo error) {
53         notifyErrorResponse(new ImpsErrorInfo(error.getCode(), error.getDescription(), null));
54     }
55 
56     /**
57      * Notify that a response from the server has arrived.
58      *
59      * @param response the response.
60      */
notifyResponse(Primitive response)61     final void notifyResponse(Primitive response) {
62         response.setTransaction(this);
63         ImpsErrorInfo error = ImpsUtils.checkResultError(response);
64         if (error != null) {
65             notifyErrorResponse(error);
66         } else {
67             notifySuccessResponse(response);
68         }
69     }
70 
notifyErrorResponse(ImpsErrorInfo error)71     protected void notifyErrorResponse(ImpsErrorInfo error) {
72         onResponseError(error);
73         mTransManager.endClientTransaction(this);
74         notifyAsyncCompletionError(error);
75     }
76 
notifySuccessResponse(Primitive response)77     protected void notifySuccessResponse(Primitive response) {
78         onResponseOk(response);
79         mTransManager.endClientTransaction(this);
80         notifyAsyncCompletionSuccess();
81     }
82 
onResponseError(ImpsErrorInfo error)83     public abstract void onResponseError(ImpsErrorInfo error);
onResponseOk(Primitive response)84     public abstract void onResponseOk(Primitive response);
85 
notifyAsyncCompletionError(ImErrorInfo error)86     protected void notifyAsyncCompletionError(ImErrorInfo error) {
87         if (!mCompletionNotified) {
88             mCompletionNotified = true;
89             if (mCompletionCallback != null)
90                 mCompletionCallback.onError(error);
91         }
92     }
93 
notifyAsyncCompletionSuccess()94     protected void notifyAsyncCompletionSuccess() {
95         if (!mCompletionNotified) {
96             mCompletionNotified = true;
97             if (mCompletionCallback != null)
98                 mCompletionCallback.onComplete();
99         }
100     }
101 }
102