2 * Copyright 2011 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.google.ipc.invalidation.external.client.contrib;
18 import com.google.ipc.invalidation.external.client.types.ObjectId;
19 import com.google.ipc.invalidation.ticl.ProtoWrapperConverter;
20 import com.google.ipc.invalidation.ticl.TiclExponentialBackoffDelayGenerator;
21 import com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState;
22 import com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.RetryRegistrationState;
23 import com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.RegistrationCommand;
24 import com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.StartCommand;
25 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP;
26 import com.google.ipc.invalidation.util.Bytes;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
32 import java.util.Map.Entry;
35 * Static helper class supporting construction of valid {code AndroidListenerProtocol} messages.
38 class AndroidListenerProtos {
40 /** Creates a retry register command for the given object and client. */
41 static RegistrationCommand newDelayedRegisterCommand(Bytes clientId, ObjectId objectId) {
42 final boolean isRegister = true;
43 return newDelayedRegistrationCommand(clientId, objectId, isRegister);
46 /** Creates a retry unregister command for the given object and client. */
47 static RegistrationCommand newDelayedUnregisterCommand(Bytes clientId, ObjectId objectId) {
48 final boolean isRegister = false;
49 return newDelayedRegistrationCommand(clientId, objectId, isRegister);
52 /** Creates proto for {@link AndroidListener} state. */
53 static AndroidListenerState newAndroidListenerState(Bytes clientId, int requestCodeSeqNum,
54 Map<ObjectId, TiclExponentialBackoffDelayGenerator> delayGenerators,
55 Collection<ObjectId> desiredRegistrations) {
56 ArrayList<RetryRegistrationState> retryRegistrationState =
57 new ArrayList<RetryRegistrationState>(delayGenerators.size());
58 for (Entry<ObjectId, TiclExponentialBackoffDelayGenerator> entry : delayGenerators.entrySet()) {
59 retryRegistrationState.add(
60 newRetryRegistrationState(entry.getKey(), entry.getValue()));
62 return AndroidListenerState.create(
63 ProtoWrapperConverter.convertToObjectIdProtoCollection(desiredRegistrations),
64 retryRegistrationState, clientId, requestCodeSeqNum);
67 /** Creates proto for retry registration state. */
68 static RetryRegistrationState newRetryRegistrationState(ObjectId objectId,
69 TiclExponentialBackoffDelayGenerator delayGenerator) {
70 return RetryRegistrationState.create(ProtoWrapperConverter.convertToObjectIdProto(objectId),
71 delayGenerator.marshal());
74 /** Returns {@code true} iff the given proto is valid. */
75 static boolean isValidAndroidListenerState(AndroidListenerState state) {
76 return state.hasClientId() && state.hasRequestCodeSeqNum();
79 /** Returns {@code true} iff the given proto is valid. */
80 static boolean isValidRegistrationCommand(RegistrationCommand command) {
81 return command.hasIsRegister() && command.hasClientId() && command.hasIsDelayed();
84 /** Returns {@code true} iff the given proto is valid. */
85 static boolean isValidStartCommand(StartCommand command) {
86 return command.hasClientType() && command.hasClientName();
89 /** Creates start command proto. */
90 static StartCommand newStartCommand(int clientType, Bytes clientName,
91 boolean allowSuppression) {
92 return StartCommand.create(clientType, clientName, allowSuppression);
95 static RegistrationCommand newRegistrationCommand(Bytes clientId,
96 Iterable<ObjectId> objectIds, boolean isRegister) {
97 return RegistrationCommand.create(isRegister,
98 ProtoWrapperConverter.convertToObjectIdProtoCollection(objectIds), clientId,
99 /* isDelayed */ false);
102 private static RegistrationCommand newDelayedRegistrationCommand(Bytes clientId,
103 ObjectId objectId, boolean isRegister) {
104 List<ObjectIdP> objectIds = new ArrayList<ObjectIdP>(1);
105 objectIds.add(ProtoWrapperConverter.convertToObjectIdProto(objectId));
106 return RegistrationCommand.create(isRegister, objectIds, clientId, /* isDelayed */ true);
109 // Prevent instantiation.
110 private AndroidListenerProtos() {