| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright 2011 Google Inc. |
| 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 package com.google.ipc.invalidation.ticl; |
| 17 |
| 18 import static com.google.ipc.invalidation.external.client.SystemResources.Schedu
ler.NO_DELAY; |
| 19 |
| 20 import com.google.ipc.invalidation.external.client.InvalidationListener; |
| 21 import com.google.ipc.invalidation.external.client.SystemResources; |
| 22 import com.google.ipc.invalidation.external.client.types.AckHandle; |
| 23 import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 24 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ClientConfigP; |
| 25 |
| 26 import java.util.Collection; |
| 27 import java.util.Random; |
| 28 |
| 29 /** |
| 30 * Implementation of the standard Java Ticl. This class extends {@link Invalida
tionClientCore} |
| 31 * with additional thread-safety related constructs. Specifically, it ensures th
at: |
| 32 * <p> |
| 33 * 1. All application calls into the Ticl execute on the internal scheduler. |
| 34 * <p> |
| 35 * 2. The storage layer always executes callbacks on the internal scheduler thre
ad. |
| 36 * <p> |
| 37 * 3. All calls into the listener are made on the listener scheduler thread. |
| 38 */ |
| 39 public class InvalidationClientImpl extends InvalidationClientCore { |
| 40 public InvalidationClientImpl(final SystemResources resources, Random random,
int clientType, |
| 41 final byte[] clientName, ClientConfigP config, String applicationName, |
| 42 InvalidationListener listener) { |
| 43 super( |
| 44 // We will make Storage a SafeStorage after the constructor call. It's n
ot possible to |
| 45 // construct a new resources around the existing components and pass tha
t to super(...) |
| 46 // because then subsequent calls on the first resources object (e.g., st
art) would not |
| 47 // affect the new resources object that the Ticl would be using. |
| 48 resources, |
| 49 |
| 50 // Pass basic parameters through unmodified. |
| 51 random, clientType, clientName, config, applicationName, |
| 52 |
| 53 // Wrap the listener in a CheckingInvalidationListener to enforce approp
riate threading. |
| 54 new CheckingInvalidationListener(listener, |
| 55 resources.getInternalScheduler(), resources.getListenerScheduler(), |
| 56 resources.getLogger()) |
| 57 ); // End super. |
| 58 |
| 59 // Make Storage safe. |
| 60 this.storage = new SafeStorage(resources.getStorage()); |
| 61 this.storage.setSystemResources(resources); |
| 62 |
| 63 // CheckingInvalidationListener needs the statistics object created by our s
uper() call, so |
| 64 // we can't provide it at construction-time (since it hasn't been created ye
t). |
| 65 ((CheckingInvalidationListener) this.listener).setStatistics(statistics); |
| 66 |
| 67 } |
| 68 |
| 69 // Methods below are public methods from InvalidationClient that must first en
queue onto the |
| 70 // internal thread. |
| 71 |
| 72 @Override |
| 73 public void start() { |
| 74 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 75 @Override |
| 76 public void run() { |
| 77 InvalidationClientImpl.super.start(); |
| 78 } |
| 79 }); |
| 80 } |
| 81 |
| 82 @Override |
| 83 public void stop() { |
| 84 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 85 @Override |
| 86 public void run() { |
| 87 InvalidationClientImpl.super.stop(); |
| 88 } |
| 89 }); |
| 90 } |
| 91 |
| 92 @Override |
| 93 public void register(final ObjectId objectId) { |
| 94 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 95 @Override |
| 96 public void run() { |
| 97 InvalidationClientImpl.super.register(objectId); |
| 98 } |
| 99 }); |
| 100 } |
| 101 |
| 102 @Override |
| 103 public void register(final Collection<ObjectId> objectIds) { |
| 104 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 105 @Override |
| 106 public void run() { |
| 107 InvalidationClientImpl.super.register(objectIds); |
| 108 } |
| 109 }); |
| 110 } |
| 111 |
| 112 @Override |
| 113 public void unregister(final ObjectId objectId) { |
| 114 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 115 @Override |
| 116 public void run() { |
| 117 InvalidationClientImpl.super.unregister(objectId); |
| 118 } |
| 119 }); |
| 120 } |
| 121 |
| 122 @Override |
| 123 public void unregister(final Collection<ObjectId> objectIds) { |
| 124 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 125 @Override |
| 126 public void run() { |
| 127 InvalidationClientImpl.super.unregister(objectIds); |
| 128 } |
| 129 }); |
| 130 } |
| 131 |
| 132 @Override |
| 133 public void acknowledge(final AckHandle ackHandle) { |
| 134 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 135 @Override |
| 136 public void run() { |
| 137 InvalidationClientImpl.super.acknowledge(ackHandle); |
| 138 } |
| 139 }); |
| 140 } |
| 141 |
| 142 // End InvalidationClient methods. |
| 143 |
| 144 @Override // InvalidationClientCore; overriding to add concurrency control. |
| 145 void handleIncomingMessage(final byte[] message) { |
| 146 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 147 @Override |
| 148 public void run() { |
| 149 InvalidationClientImpl.super.handleIncomingMessage(message); |
| 150 } |
| 151 }); |
| 152 } |
| 153 |
| 154 @Override // InvalidationClientCore; overriding to add concurrency control. |
| 155 public void handleNetworkStatusChange(final boolean isOnline) { |
| 156 getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() { |
| 157 @Override |
| 158 public void run() { |
| 159 InvalidationClientImpl.super.handleNetworkStatusChange(isOnline); |
| 160 } |
| 161 }); |
| 162 } |
| 163 } |
| OLD | NEW |