com.google.ipc.invalidation.ticl.InvalidationClientCore Java Examples
The following examples show how to use
com.google.ipc.invalidation.ticl.InvalidationClientCore.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidClientProxy.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** * Creates a new InvalidationClient instance that the proxy will delegate requests to and listen * for events from. */ // Overridden by tests to inject mock clients or for listener interception InvalidationClient createClient(SystemResources resources, int clientType, byte[] clientName, String applicationName, InvalidationListener listener, ClientConfigP config) { // We always use C2DM, so set the channel-supports-offline-delivery bit on our config. final ClientConfigP.Builder configBuilder; if (config == null) { configBuilder = InvalidationClientCore.createConfig(); } else { configBuilder = ClientConfigP.newBuilder(config); } configBuilder.setChannelSupportsOfflineDelivery(true); config = configBuilder.build(); Random random = new Random(resources.getInternalScheduler().getCurrentTimeMs()); return new InvalidationClientImpl(resources, random, clientType, clientName, config, applicationName, listener); }
Example #2
Source File: AndroidInvalidationService.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** * Returns the persisted state for the client with key {@code clientKey} in * {@code storageForClient}, or {@code null} if no valid state could be found. * <p> * REQUIRES: {@code storageForClient}.load() has been called successfully. */ PersistentTiclState decodeTiclState(final String clientKey, AndroidStorage storageForClient) { synchronized (lock) { // Retrieve the serialized state. final Map<String, byte[]> properties = storageForClient.getPropertiesUnsafe(); byte[] clientStateBytes = TypedUtil.mapGet(properties, InvalidationClientCore.CLIENT_TOKEN_KEY); if (clientStateBytes == null) { logger.warning("No client state found in storage for %s: %s", clientKey, properties.keySet()); return null; } // Deserialize it. PersistentTiclState clientState = PersistenceUtils.deserializeState(logger, clientStateBytes, digestFn); if (clientState == null) { logger.warning("Invalid client state found in storage for %s", clientKey); return null; } return clientState; } }
Example #3
Source File: AndroidListener.java From 365browser with Apache License 2.0 | 6 votes |
/** Tries to handle a start intent. Returns {@code true} iff the intent is a start intent. */ private boolean tryHandleStartIntent(Intent intent) { StartCommand command = AndroidListenerIntents.findStartCommand(intent); if ((command == null) || !AndroidListenerProtos.isValidStartCommand(command)) { return false; } // Reset the state so that we make no assumptions about desired registrations and can ignore // messages directed at the wrong instance. state = new AndroidListenerState(initialMaxDelayMs, maxDelayFactor); boolean skipStartForTest = false; ClientConfigP clientConfig = InvalidationClientCore.createConfig(); if (command.getAllowSuppression() != clientConfig.getAllowSuppression()) { ClientConfigP.Builder clientConfigBuilder = clientConfig.toBuilder(); clientConfigBuilder.allowSuppression = command.getAllowSuppression(); clientConfig = clientConfigBuilder.build(); } Intent startIntent = ProtocolIntents.InternalDowncalls.newCreateClientIntent( command.getClientType(), command.getClientName(), clientConfig, skipStartForTest); AndroidListenerIntents.issueTiclIntent(getApplicationContext(), startIntent); return true; }
Example #4
Source File: AndroidClientProxy.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** * Creates a new InvalidationClient instance that the proxy will delegate requests to and listen * for events from. */ // Overridden by tests to inject mock clients or for listener interception InvalidationClient createClient(SystemResources resources, int clientType, byte[] clientName, String applicationName, InvalidationListener listener, ClientConfigP config) { // We always use C2DM, so set the channel-supports-offline-delivery bit on our config. final ClientConfigP.Builder configBuilder; if (config == null) { configBuilder = InvalidationClientCore.createConfig(); } else { configBuilder = ClientConfigP.newBuilder(config); } configBuilder.setChannelSupportsOfflineDelivery(true); config = configBuilder.build(); Random random = new Random(resources.getInternalScheduler().getCurrentTimeMs()); return new InvalidationClientImpl(resources, random, clientType, clientName, config, applicationName, listener); }
Example #5
Source File: AndroidInvalidationService.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** * Returns the persisted state for the client with key {@code clientKey} in * {@code storageForClient}, or {@code null} if no valid state could be found. * <p> * REQUIRES: {@code storageForClient}.load() has been called successfully. */ PersistentTiclState decodeTiclState(final String clientKey, AndroidStorage storageForClient) { synchronized (lock) { // Retrieve the serialized state. final Map<String, byte[]> properties = storageForClient.getPropertiesUnsafe(); byte[] clientStateBytes = TypedUtil.mapGet(properties, InvalidationClientCore.CLIENT_TOKEN_KEY); if (clientStateBytes == null) { logger.warning("No client state found in storage for %s: %s", clientKey, properties.keySet()); return null; } // Deserialize it. PersistentTiclState clientState = PersistenceUtils.deserializeState(logger, clientStateBytes, digestFn); if (clientState == null) { logger.warning("Invalid client state found in storage for %s", clientKey); return null; } return clientState; } }
Example #6
Source File: AndroidStorage.java From 365browser with Apache License 2.0 | 5 votes |
@Override public void readAllKeys(Callback<SimplePair<Status, String>> keyCallback) { // If the state file exists, supply the CLIENT_TOKEN_KEY as a present key. if (context.getFileStreamPath(STATE_FILENAME).exists()) { Status status = Status.newInstance(Status.Code.SUCCESS, ""); keyCallback.accept(SimplePair.of(status, InvalidationClientCore.CLIENT_TOKEN_KEY)); } keyCallback.accept(null); }
Example #7
Source File: InvalidationClientFactory.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Constructs an invalidation client library instance. * * @param resources {@link SystemResources} to use for logging, scheduling, persistence, and * network connectivity * @param clientConfig application provided configuration for the client. * @param listener callback object for invalidation events */ public static InvalidationClient createClient(SystemResources resources, InvalidationClientConfig clientConfig, InvalidationListener listener) { ClientConfigP internalConfig = InvalidationClientCore.createConfig() .setAllowSuppression(clientConfig.allowSuppression) .build(); Random random = new Random(resources.getInternalScheduler().getCurrentTimeMs()); return new InvalidationClientImpl(resources, random, clientConfig.clientType, clientConfig.clientName, internalConfig, clientConfig.applicationName, listener); }
Example #8
Source File: AndroidInvalidationService.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Handles receipt of a GCM message for a client that was unknown or not started. If the client * was unknown, drops the message. If the client was not started, rewrites the client's * persistent state to have a last-message-sent-time of 0, ensuring that the client will * send a heartbeat to the server when restarted. Since we drop the received GCM message, * the client will be disconnected by the invalidation pusher; this heartbeat ensures a * timely reconnection. */ private void handleGcmMessageForUnstartedClient(AndroidClientProxy proxy) { if (proxy == null) { // Unknown client; nothing to do. return; } // Client is not started. Open its storage. We are going to use unsafe calls here that // bypass the normal storage API. This is safe in this context because we hold a lock // that prevents anyone else from starting this client or accessing its storage. We // really should not be holding a lock across I/O, but at least this is only local // file I/O, and we're only writing a few bytes. Additionally, since we currently only // have one Ticl, we should only ever enter this function if we're not being used for // anything else. final String clientKey = proxy.getClientKey(); logger.info("Received message for unloaded client; rewriting state file: %s", clientKey); // This storage must have been loaded, because we got this proxy from the client manager, // which always ensures that its entries have that property. AndroidStorage storageForClient = proxy.getStorage(); PersistentTiclState clientState = decodeTiclState(clientKey, storageForClient); if (clientState == null) { // Logging done in decodeTiclState. return; } // Rewrite the last message sent time. PersistentTiclState newState = PersistentTiclState.newBuilder(clientState) .setLastMessageSendTimeMs(0).build(); // Serialize the new state. byte[] newClientState = PersistenceUtils.serializeState(newState, digestFn); // Write it out. storageForClient.getPropertiesUnsafe().put(InvalidationClientCore.CLIENT_TOKEN_KEY, newClientState); storageForClient.storeUnsafe(); }
Example #9
Source File: AndroidStorage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void readAllKeys(Callback<SimplePair<Status, String>> keyCallback) { // If the state file exists, supply the CLIENT_TOKEN_KEY as a present key. if (context.getFileStreamPath(STATE_FILENAME).exists()) { Status status = Status.newInstance(Status.Code.SUCCESS, ""); keyCallback.accept(SimplePair.of(status, InvalidationClientCore.CLIENT_TOKEN_KEY)); } keyCallback.accept(null); }
Example #10
Source File: AndroidStorage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void deleteKey(String key, Callback<Boolean> done) { // We only support the CLIENT_TOKEN_KEY. if (!key.equals(InvalidationClientCore.CLIENT_TOKEN_KEY)) { done.accept(false); return; } if (!context.getFileStreamPath(STATE_FILENAME).exists()) { // Deletion "succeeds" if the key didn't exist. done.accept(true); } else { // Otherwise it succeeds based on whether the IO operation succeeded. done.accept(context.deleteFile(STATE_FILENAME)); } }
Example #11
Source File: InvalidationClientFactory.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Constructs an invalidation client library instance. * * @param resources {@link SystemResources} to use for logging, scheduling, persistence, and * network connectivity * @param clientConfig application provided configuration for the client. * @param listener callback object for invalidation events */ public static InvalidationClient createClient(SystemResources resources, InvalidationClientConfig clientConfig, InvalidationListener listener) { ClientConfigP internalConfig = InvalidationClientCore.createConfig() .setAllowSuppression(clientConfig.allowSuppression) .build(); Random random = new Random(resources.getInternalScheduler().getCurrentTimeMs()); return new InvalidationClientImpl(resources, random, clientConfig.clientType, clientConfig.clientName, internalConfig, clientConfig.applicationName, listener); }
Example #12
Source File: AndroidInvalidationService.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Handles receipt of a GCM message for a client that was unknown or not started. If the client * was unknown, drops the message. If the client was not started, rewrites the client's * persistent state to have a last-message-sent-time of 0, ensuring that the client will * send a heartbeat to the server when restarted. Since we drop the received GCM message, * the client will be disconnected by the invalidation pusher; this heartbeat ensures a * timely reconnection. */ private void handleGcmMessageForUnstartedClient(AndroidClientProxy proxy) { if (proxy == null) { // Unknown client; nothing to do. return; } // Client is not started. Open its storage. We are going to use unsafe calls here that // bypass the normal storage API. This is safe in this context because we hold a lock // that prevents anyone else from starting this client or accessing its storage. We // really should not be holding a lock across I/O, but at least this is only local // file I/O, and we're only writing a few bytes. Additionally, since we currently only // have one Ticl, we should only ever enter this function if we're not being used for // anything else. final String clientKey = proxy.getClientKey(); logger.info("Received message for unloaded client; rewriting state file: %s", clientKey); // This storage must have been loaded, because we got this proxy from the client manager, // which always ensures that its entries have that property. AndroidStorage storageForClient = proxy.getStorage(); PersistentTiclState clientState = decodeTiclState(clientKey, storageForClient); if (clientState == null) { // Logging done in decodeTiclState. return; } // Rewrite the last message sent time. PersistentTiclState newState = PersistentTiclState.newBuilder(clientState) .setLastMessageSendTimeMs(0).build(); // Serialize the new state. byte[] newClientState = PersistenceUtils.serializeState(newState, digestFn); // Write it out. storageForClient.getPropertiesUnsafe().put(InvalidationClientCore.CLIENT_TOKEN_KEY, newClientState); storageForClient.storeUnsafe(); }
Example #13
Source File: AndroidStorage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void readAllKeys(Callback<SimplePair<Status, String>> keyCallback) { // If the state file exists, supply the CLIENT_TOKEN_KEY as a present key. if (context.getFileStreamPath(STATE_FILENAME).exists()) { Status status = Status.newInstance(Status.Code.SUCCESS, ""); keyCallback.accept(SimplePair.of(status, InvalidationClientCore.CLIENT_TOKEN_KEY)); } keyCallback.accept(null); }
Example #14
Source File: AndroidStorage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void deleteKey(String key, Callback<Boolean> done) { // We only support the CLIENT_TOKEN_KEY. if (!key.equals(InvalidationClientCore.CLIENT_TOKEN_KEY)) { done.accept(false); return; } if (!context.getFileStreamPath(STATE_FILENAME).exists()) { // Deletion "succeeds" if the key didn't exist. done.accept(true); } else { // Otherwise it succeeds based on whether the IO operation succeeded. done.accept(context.deleteFile(STATE_FILENAME)); } }
Example #15
Source File: InvalidationClientFactory.java From 365browser with Apache License 2.0 | 5 votes |
/** * Constructs an invalidation client library instance. * * @param resources {@link SystemResources} to use for logging, scheduling, persistence, and * network connectivity * @param clientConfig application provided configuration for the client. * @param listener callback object for invalidation events */ public static InvalidationClient createClient(SystemResources resources, InvalidationClientConfig clientConfig, InvalidationListener listener) { ClientConfigP.Builder internalConfigBuilder = InvalidationClientCore.createConfig().toBuilder(); internalConfigBuilder.allowSuppression = clientConfig.allowSuppression; ClientConfigP internalConfig = internalConfigBuilder.build(); Random random = new Random(resources.getInternalScheduler().getCurrentTimeMs()); return new InvalidationClientImpl(resources, random, clientConfig.clientType, clientConfig.clientName, internalConfig, clientConfig.applicationName, listener); }
Example #16
Source File: AndroidStorage.java From 365browser with Apache License 2.0 | 5 votes |
@Override public void deleteKey(String key, Callback<Boolean> done) { // We only support the CLIENT_TOKEN_KEY. if (!key.equals(InvalidationClientCore.CLIENT_TOKEN_KEY)) { done.accept(false); return; } if (!context.getFileStreamPath(STATE_FILENAME).exists()) { // Deletion "succeeds" if the key didn't exist. done.accept(true); } else { // Otherwise it succeeds based on whether the IO operation succeeded. done.accept(context.deleteFile(STATE_FILENAME)); } }
Example #17
Source File: TiclService.java From android-chromium with BSD 2-Clause "Simplified" License | 4 votes |
/** * Handles a {@code message} for a {@code ticl}. If the {@code ticl} is started, delivers the * message. If the {@code ticl} is not started, drops the message and clears the last message send * time in the Ticl persistent storage so that the Ticl will send a heartbeat the next time it * starts. */ private void handleServerMessage(boolean isTiclStarted, byte[] message) { if (isTiclStarted) { // Normal case -- message for a started Ticl. Deliver the message. resources.getNetworkListener().onMessageReceived(message); return; } // The Ticl isn't started. Rewrite persistent storage so that the last-send-time is a long // time ago. The next time the Ticl starts, it will send a message to the data center, which // ensures that it will be marked online and that the dropped message (or an equivalent) will // be delivered. // Android storage implementations are required to execute callbacks inline, so this code // all executes synchronously. resources.getLogger().fine("Message for unstarted Ticl; rewrite state"); resources.getStorage().readKey(InvalidationClientCore.CLIENT_TOKEN_KEY, new Callback<SimplePair<Status, byte[]>>() { @Override public void accept(SimplePair<Status, byte[]> result) { byte[] stateBytes = result.second; if (stateBytes == null) { resources.getLogger().info("No persistent state found for client; not rewriting"); return; } // Create new state identical to the old state except with a cleared // lastMessageSendTimeMs. PersistentTiclState state = PersistenceUtils.deserializeState( resources.getLogger(), stateBytes, digestFn); if (state == null) { resources.getLogger().warning("Ignoring invalid Ticl state: %s", stateBytes); return; } PersistentTiclState newState = PersistentTiclState.newBuilder(state) .setLastMessageSendTimeMs(0) .build(); // Serialize the new state and write it to storage. byte[] newClientState = PersistenceUtils.serializeState(newState, digestFn); resources.getStorage().writeKey(InvalidationClientCore.CLIENT_TOKEN_KEY, newClientState, new Callback<Status>() { @Override public void accept(Status status) { if (status.getCode() != Status.Code.SUCCESS) { resources.getLogger().warning( "Failed saving rewritten persistent state to storage"); } } }); } }); }
Example #18
Source File: TiclService.java From android-chromium with BSD 2-Clause "Simplified" License | 4 votes |
/** * Handles a {@code message} for a {@code ticl}. If the {@code ticl} is started, delivers the * message. If the {@code ticl} is not started, drops the message and clears the last message send * time in the Ticl persistent storage so that the Ticl will send a heartbeat the next time it * starts. */ private void handleServerMessage(boolean isTiclStarted, byte[] message) { if (isTiclStarted) { // Normal case -- message for a started Ticl. Deliver the message. resources.getNetworkListener().onMessageReceived(message); return; } // The Ticl isn't started. Rewrite persistent storage so that the last-send-time is a long // time ago. The next time the Ticl starts, it will send a message to the data center, which // ensures that it will be marked online and that the dropped message (or an equivalent) will // be delivered. // Android storage implementations are required to execute callbacks inline, so this code // all executes synchronously. resources.getLogger().fine("Message for unstarted Ticl; rewrite state"); resources.getStorage().readKey(InvalidationClientCore.CLIENT_TOKEN_KEY, new Callback<SimplePair<Status, byte[]>>() { @Override public void accept(SimplePair<Status, byte[]> result) { byte[] stateBytes = result.second; if (stateBytes == null) { resources.getLogger().info("No persistent state found for client; not rewriting"); return; } // Create new state identical to the old state except with a cleared // lastMessageSendTimeMs. PersistentTiclState state = PersistenceUtils.deserializeState( resources.getLogger(), stateBytes, digestFn); if (state == null) { resources.getLogger().warning("Ignoring invalid Ticl state: %s", stateBytes); return; } PersistentTiclState newState = PersistentTiclState.newBuilder(state) .setLastMessageSendTimeMs(0) .build(); // Serialize the new state and write it to storage. byte[] newClientState = PersistenceUtils.serializeState(newState, digestFn); resources.getStorage().writeKey(InvalidationClientCore.CLIENT_TOKEN_KEY, newClientState, new Callback<Status>() { @Override public void accept(Status status) { if (status.getCode() != Status.Code.SUCCESS) { resources.getLogger().warning( "Failed saving rewritten persistent state to storage"); } } }); } }); }
Example #19
Source File: TiclService.java From 365browser with Apache License 2.0 | 4 votes |
/** * Handles a {@code message} for a {@code ticl}. If the {@code ticl} is started, delivers the * message. If the {@code ticl} is not started, drops the message and clears the last message send * time in the Ticl persistent storage so that the Ticl will send a heartbeat the next time it * starts. */ private void handleServerMessage(boolean isTiclStarted, byte[] message) { if (isTiclStarted) { // Normal case -- message for a started Ticl. Deliver the message. resources.getNetworkListener().onMessageReceived(message); return; } // Even if the client is stopped, attempt to send invalidations if the client is configured to // receive them. maybeSendBackgroundInvalidationIntent(message); // The Ticl isn't started. Rewrite persistent storage so that the last-send-time is a long // time ago. The next time the Ticl starts, it will send a message to the data center, which // ensures that it will be marked online and that the dropped message (or an equivalent) will // be delivered. // Android storage implementations are required to execute callbacks inline, so this code // all executes synchronously. resources.getLogger().fine("Message for unstarted Ticl; rewrite state"); resources.getStorage().readKey(InvalidationClientCore.CLIENT_TOKEN_KEY, new Callback<SimplePair<Status, byte[]>>() { @Override public void accept(SimplePair<Status, byte[]> result) { byte[] stateBytes = result.second; if (stateBytes == null) { resources.getLogger().info("No persistent state found for client; not rewriting"); return; } // Create new state identical to the old state except with a cleared // lastMessageSendTimeMs. PersistentTiclState state = PersistenceUtils.deserializeState( resources.getLogger(), stateBytes, digestFn); if (state == null) { resources.getLogger().warning("Ignoring invalid Ticl state: %s", Bytes.toLazyCompactString(stateBytes)); return; } PersistentTiclState.Builder stateBuilder = state.toBuilder(); stateBuilder.lastMessageSendTimeMs = 0L; state = stateBuilder.build(); // Serialize the new state and write it to storage. byte[] newClientState = PersistenceUtils.serializeState(state, digestFn); resources.getStorage().writeKey(InvalidationClientCore.CLIENT_TOKEN_KEY, newClientState, new Callback<Status>() { @Override public void accept(Status status) { if (status.getCode() != Status.Code.SUCCESS) { resources.getLogger().warning( "Failed saving rewritten persistent state to storage"); } } }); } }); }
Example #20
Source File: AndroidClientFactory.java From android-chromium with BSD 2-Clause "Simplified" License | 3 votes |
/** * Creates a new client. * <p> * REQUIRES: no client exist, or a client exists with the same type and name as provided. In * the latter case, this call is a no-op. * * @param context Android system context * @param clientType type of the client to create * @param clientName name of the client to create */ public static void createClient(Context context, ClientType.Type clientType, byte[] clientName) { ClientConfigP config = InvalidationClientCore.createConfig().build(); Intent intent = ProtocolIntents.InternalDowncalls.newCreateClientIntent( clientType.getNumber(), clientName, config, false); intent.setClassName(context, new AndroidTiclManifest(context).getTiclServiceClass()); context.startService(intent); }
Example #21
Source File: AndroidClientFactory.java From 365browser with Apache License 2.0 | 3 votes |
/** * Creates a new client. * <p> * REQUIRES: no client exist, or a client exists with the same type and name as provided. In * the latter case, this call is a no-op. * * @param context Android system context * @param clientType type of the client to create * @param clientName name of the client to create */ public static void createClient(Context context, int clientType, byte[] clientName) { ClientConfigP config = InvalidationClientCore.createConfig(); Intent intent = ProtocolIntents.InternalDowncalls.newCreateClientIntent( clientType, Bytes.fromByteArray(clientName), config, false); intent.setClassName(context, new AndroidTiclManifest(context).getTiclServiceClass()); context.startService(intent); }
Example #22
Source File: AndroidClientFactory.java From android-chromium with BSD 2-Clause "Simplified" License | 3 votes |
/** * Creates a new client. * <p> * REQUIRES: no client exist, or a client exists with the same type and name as provided. In * the latter case, this call is a no-op. * * @param context Android system context * @param clientType type of the client to create * @param clientName name of the client to create */ public static void createClient(Context context, ClientType.Type clientType, byte[] clientName) { ClientConfigP config = InvalidationClientCore.createConfig().build(); Intent intent = ProtocolIntents.InternalDowncalls.newCreateClientIntent( clientType.getNumber(), clientName, config, false); intent.setClassName(context, new AndroidTiclManifest(context).getTiclServiceClass()); context.startService(intent); }