All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.io.mqtt.MqttSocketModem Maven / Gradle / Ivy

// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.io.mqtt;

import java.net.InetSocketAddress;
import java.security.Principal;
import java.security.cert.Certificate;
import java.util.Collection;
import swim.codec.Decoder;
import swim.io.FlowControl;
import swim.io.FlowModifier;
import swim.io.Modem;
import swim.io.ModemContext;
import swim.io.Socket;
import swim.mqtt.Mqtt;
import swim.mqtt.MqttPacket;

public class MqttSocketModem implements Modem, MqttSocketContext {
  protected final MqttSocket socket;
  protected final MqttSettings mqttSettings;
  protected ModemContext context;

  public MqttSocketModem(MqttSocket socket, MqttSettings mqttSettings) {
    this.socket = socket;
    this.mqttSettings = mqttSettings;
  }

  @Override
  public ModemContext modemContext() {
    return this.context;
  }

  @Override
  public void setModemContext(ModemContext context) {
    this.context = context;
    this.socket.setMqttSocketContext(this);
  }

  @Override
  public long idleTimeout() {
    return this.socket.idleTimeout();
  }

  @Override
  public void doRead() {
    this.socket.doRead();
  }

  @SuppressWarnings("unchecked")
  @Override
  public void didRead(Object input) {
    this.socket.didRead((MqttPacket) input);
  }

  @Override
  public void doWrite() {
    this.socket.doWrite();
  }

  @SuppressWarnings("unchecked")
  @Override
  public void didWrite(Object output) {
    this.socket.didWrite((MqttPacket) output);
  }

  @Override
  public void willConnect() {
    this.socket.willConnect();
  }

  @Override
  public void didConnect() {
    this.socket.didConnect();
  }

  @Override
  public void willSecure() {
    this.socket.willSecure();
  }

  @Override
  public void didSecure() {
    this.socket.didSecure();
  }

  @Override
  public void willBecome(Socket socket) {
    this.socket.willBecome(socket);
  }

  @Override
  public void didBecome(Socket socket) {
    this.socket.didBecome(socket);
  }

  @Override
  public void didTimeout() {
    this.socket.didTimeout();
  }

  @Override
  public void didDisconnect() {
    this.socket.didDisconnect();
  }

  @Override
  public void didFail(Throwable error) {
    this.socket.didFail(error);
    close();
  }

  @Override
  public boolean isConnected() {
    final ModemContext context = this.context;
    return context != null && context.isConnected();
  }

  @Override
  public boolean isClient() {
    final ModemContext context = this.context;
    return context != null && context.isClient();
  }

  @Override
  public boolean isServer() {
    final ModemContext context = this.context;
    return context != null && context.isServer();
  }

  @Override
  public boolean isSecure() {
    final ModemContext context = this.context;
    return context != null && context.isSecure();
  }

  @Override
  public String securityProtocol() {
    return this.context.securityProtocol();
  }

  @Override
  public String cipherSuite() {
    return this.context.cipherSuite();
  }

  @Override
  public InetSocketAddress localAddress() {
    return this.context.localAddress();
  }

  @Override
  public Principal localPrincipal() {
    return this.context.localPrincipal();
  }

  @Override
  public Collection localCertificates() {
    return this.context.localCertificates();
  }

  @Override
  public InetSocketAddress remoteAddress() {
    return this.context.remoteAddress();
  }

  @Override
  public Principal remotePrincipal() {
    return this.context.remotePrincipal();
  }

  @Override
  public Collection remoteCertificates() {
    return this.context.remoteCertificates();
  }

  @Override
  public FlowControl flowControl() {
    return this.context.flowControl();
  }

  @Override
  public void flowControl(FlowControl flowControl) {
    this.context.flowControl(flowControl);
  }

  @Override
  public FlowControl flowControl(FlowModifier flowModifier) {
    return this.context.flowControl(flowModifier);
  }

  @Override
  public MqttSettings mqttSettings() {
    return this.mqttSettings;
  }

  @Override
  public  void read(Decoder content) {
    this.context.read(Mqtt.standardDecoder().packetDecoder(content));
  }

  @Override
  public  void write(MqttPacket packet) {
    this.context.write(packet.mqttEncoder(Mqtt.standardEncoder()));
  }

  @Override
  public void become(Socket socket) {
    this.context.become(socket);
  }

  @Override
  public void close() {
    this.context.close();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy