package io.joynr.messaging.mqtt;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2017 BMW Car IT GmbH
 * %%
 * 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.
 * #L%
 */

import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.joynr.messaging.routing.MulticastAddressCalculator;
import joynr.JoynrMessage;
import joynr.system.RoutingTypes.Address;
import joynr.system.RoutingTypes.MqttAddress;

public class MqttMulticastAddressCalculator implements MulticastAddressCalculator {

    private MqttAddress globalAddress;

    @Inject
    public MqttMulticastAddressCalculator(@Named(MqttModule.PROPERTY_MQTT_GLOBAL_ADDRESS) MqttAddress globalAddress) {
        this.globalAddress = (MqttAddress) globalAddress;
    }

    @Override
    public Address calculate(JoynrMessage message) {
        Address result = null;
        if (globalAddress != null) {
            result = new MqttAddress(globalAddress.getBrokerUri(), message.getTo());
        }
        return result;
    }

    @Override
    public boolean supports(String transport) {
        return "mqtt".equalsIgnoreCase(transport);
    }
}

@@ -1035,16 +1035,77 @@
 Address;
+%0A    private MqttTopicPrefixProvider mqttTopicPrefixProvider;
 %0A%0A    @I
@@ -1225,16 +1225,107 @@
 lAddress
+,%0A                                          MqttTopicPrefixProvider mqttTopicPrefixProvider
 ) %7B%0A    
@@ -1374,24 +1374,88 @@
 balAddress;%0A
+        this.mqttTopicPrefixProvider = mqttTopicPrefixProvider;%0A
     %7D%0A%0A    @
@@ -1580,24 +1580,120 @@
  != null) %7B%0A
+            String topic = mqttTopicPrefixProvider.getMulticastTopicPrefix() + message.getTo();%0A
             
@@ -1747,31 +1747,21 @@
 rUri(), 
-message.getTo()
+topic
 );%0A     

set mqtt topic multicast prefix for outgoing multicasts