private void guardedConnect() { if (getUsageCounter() == 1) { try { connect(); } catch(RuntimeException ex) { reportExceptionToAllReadersAndWriters(ex); } } }
private void guardedDisconnect() { if (getUsageCounter() == 0) { try { disconnect(); if (!saveMessageAfterDisconnect()) { lastMessage = null; } connectionPayload = null; } catch (RuntimeException ex) { reportExceptionToAllReadersAndWriters(ex); log.log(Level.WARNING, "Couldn't disconnect channel " + getChannelName(), ex); } } }
@Override protected synchronized void removeReader(ChannelHandlerReadSubscription subscription) { monitors.remove(subscription); readUsageCounter--; guardedDisconnect(); }
/** * Process the next connection payload. This should be called whenever * the connection state has changed. * * @param connectionPayload connection payload; not null */ protected synchronized final void processConnection(ConnectionPayload connectionPayload) { if (log.isLoggable(Level.FINEST)) { log.log(Level.FINEST, "processConnection for channel {0} connectionPayload {1}", new Object[] {getChannelName(), connectionPayload}); } this.connectionPayload = connectionPayload; setConnected(isConnected(connectionPayload)); setWriteConnected(isWriteConnected(connectionPayload)); for (MonitorHandler monitor : monitors.values()) { monitor.findTypeAdapter(); } if (isConnected() && lastMessage != null && processMessageOnReconnect) { processMessage(lastMessage); } if (!isConnected() && lastMessage != null && processMessageOnDisconnect) { processMessage(lastMessage); } }
@Override protected synchronized void addReader(ChannelHandlerReadSubscription subscription) { readUsageCounter++; MonitorHandler monitor = new MonitorHandler(subscription); monitors.put(subscription, monitor); monitor.findTypeAdapter(); guardedConnect(); if (getUsageCounter() > 1) { if (connectionPayload != null) { monitor.processConnection(isConnected()); } if (lastMessage != null) { monitor.processValue(lastMessage); } } }
@Override protected synchronized void addWriter(ChannelHandlerWriteSubscription subscription) { writeUsageCounter++; writeSubscriptions.put(subscription.getWriteCache(), subscription); guardedConnect(); if (connectionPayload != null) { subscription.getConnectionWriteFunction().writeValue(isWriteConnected()); } }
/** * Determines from the payload whether the channel is connected or not. * <p> * By default, this uses the usage counter to determine whether it's * connected or not. One should override this to use the actual * connection payload to check whether the actual protocol connection * has been established. * * @param payload the connection payload * @return true if connected */ protected boolean isConnected(ConnectionPayload payload) { return getUsageCounter() > 0; }
/** * Process the payload for this channel. This should be called whenever * a new value needs to be processed. The handler will take care of * using the correct {@link DataSourceTypeAdapter} * for each read monitor that was setup. * * @param payload the payload of for this type of channel */ protected synchronized final void processMessage(MessagePayload payload) { if (log.isLoggable(Level.FINEST)) { log.log(Level.FINEST, "processMessage for channel {0} messagePayload {1}", new Object[]{getChannelName(), payload}); } lastMessage = payload; for (MonitorHandler monitor : monitors.values()) { monitor.processValue(payload); } }
@Override public void addReader(ChannelHandlerReadSubscription subscription) { super.addReader(subscription); if (!monitorCreated.getAndSet(true)) { // TODO remove this.... for (int i = 0; i < 100 && channel.getConnectionState() == ConnectionState.NEVER_CONNECTED; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { } } // TODO optimize fields channel.createMonitor(this, pvRequest != null ? pvRequest : allPVRequest); } }
@Override protected synchronized void addWriter(ChannelHandlerWriteSubscription subscription) { super.addWriter(subscription); // If already connected and read only, we need to notify this writer if (sentReadOnlyException) { subscription.getExceptionWriteFunction().writeValue(createReadOnlyException()); } }
@Override protected synchronized void removeWrite(ChannelHandlerWriteSubscription subscription) { writeUsageCounter--; writeSubscriptions.remove(subscription.getWriteCache()); guardedDisconnect(); }