replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / androidTest / java / org / iotivity / service / ns / sample / provider / ConsumerSimulator.java
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package org.iotivity.service.ns.sample.provider;
22
23 import android.util.Log;
24
25 import org.iotivity.service.ns.common.Message;
26 import org.iotivity.service.ns.common.SyncInfo;
27 import org.iotivity.service.ns.consumer.ConsumerService;
28 import org.iotivity.service.ns.consumer.Provider;
29
30 import java.util.concurrent.CountDownLatch;
31
32 class ConsumerSimulator implements ConsumerService.OnProviderDiscoveredListener,
33         Provider.OnProviderStateListener, Provider.OnMessageReceivedListener,
34         Provider.OnSyncInfoReceivedListener {
35     private String         TAG = "Consumer Simulator";
36     private Provider       mProvider;
37     private CountDownLatch mLockObject;
38     private Response       mResponse;
39     private String         mExpectCb;
40
41     public void set(CountDownLatch lockObject, Response response, String name) {
42         Log.i(TAG, "Setting lock Simulator: ");
43         mLockObject = lockObject;
44         mResponse = response;
45         mExpectCb = name;
46     }
47
48     @Override
49     public void onProviderDiscovered(Provider provider) {
50         mProvider = provider;
51         try {
52             provider.setListener(this, this, this);
53             if (!provider.isSubscribed()) {
54                 provider.subscribe();
55             }
56         } catch (Exception e) {
57             e.printStackTrace();
58         }
59
60     }
61
62     @Override
63     public void onProviderStateReceived(Provider.ProviderState providerState) {
64     }
65
66     @Override
67     public void onMessageReceived(Message message) {
68         if (mExpectCb == "msg") {
69             mResponse.set(true);
70             mLockObject.countDown();
71         }
72     }
73
74     @Override
75     public void onSyncInfoReceived(SyncInfo syncInfo) {
76         if (mExpectCb == "sync") {
77             mResponse.set(true);
78             mLockObject.countDown();
79         }
80     }
81
82     public void sendSyncInfo(long id, SyncInfo.SyncType type) {
83         try {
84             mProvider.sendSyncInfo(id, type);
85         } catch (Exception e) {
86             e.printStackTrace();
87         }
88     }
89 }