package com.invertor.modbus.slave;

import com.invertor.modbus.Modbus;
import com.invertor.modbus.ModbusSlave;
import com.invertor.modbus.data.DataHolder;
import com.invertor.modbus.exception.ModbusIOException;
import com.invertor.modbus.exception.ModbusNumberException;
import com.invertor.modbus.msg.base.ModbusMessage;
import com.invertor.modbus.msg.base.ModbusRequest;
import com.invertor.modbus.net.ModbusConnection;
import com.invertor.modbus.net.transport.ModbusTransport;

/*
 * Copyright (C) 2016 "Invertor" Factory", JSC
 * [http://www.sbp-invertor.ru]
 *
 * This file is part of JLibModbus.
 *
 * 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.
 *
 * Authors: Vladislav Y. Kochedykov, software engineer.
 * email: vladislav.kochedykov@gmail.com
 */
class RequestHandlerTCP extends RequestHandler {

    RequestHandlerTCP(ModbusSlave slave, ModbusConnection conn) {
        super(slave, conn);
    }

    @Override
    public void run() {
        setListening(true);
        try {
            do {
                DataHolder dataHolder = getSlave().getDataHolder();
                ModbusTransport transport = getConn().getTransport();
                ModbusRequest request = (ModbusRequest) transport.readRequest();

                if (    //default tcp session
                        request.getServerAddress() == Modbus.TCP_DEFAULT_ID ||
                                //via gateway
                                request.getServerAddress() == getSlave().getServerAddress() ||
                                //broadcast
                                (request.getServerAddress() == Modbus.BROADCAST_ID && getSlave().isBroadcastEnabled())) {
                    ModbusMessage response = request.process(dataHolder);
                    response.setTransactionId(request.getTransactionId());
                    transport.send(response);
                }
            } while (isListening());

        } catch (ModbusNumberException e) {
            Modbus.log().warning(e.getLocalizedMessage());
        } catch (ModbusIOException e) {
            Modbus.log().warning(e.getLocalizedMessage());
        } finally {
            setListening(false);
            try {
                getConn().close();
            } catch (ModbusIOException ioe) {
                Modbus.log().warning(ioe.getMessage());
            }
        }
    }
}
@@ -1742,22 +1742,18 @@
     if (
-    /
 /
+*
 default 
@@ -1767,33 +1767,10 @@
 sion
-%0A                        
+*/
 requ
@@ -1848,62 +1848,19 @@
     
-        //via gateway%0A                                
+/*gateway*/
 requ
@@ -1914,19 +1914,19 @@
 ddress()
- %7C%7C
+) %7B
 %0A       
@@ -1942,43 +1942,137 @@
     
-            //broadcast%0A           
+ModbusMessage response = request.process(dataHolder);%0A                    response.setTransactionId(request.getTransactionId());%0A
     
@@ -2087,16 +2087,18 @@
         
+if
  (reques
@@ -2110,33 +2110,33 @@
 ServerAddress() 
-=
+!
 = Modbus.BROADCA
@@ -2144,240 +2144,293 @@
 T_ID
- && getSlave().isBroadcastEnabled())) %7B%0A                    ModbusMessage response = request.process(dataHolder);%0A                    response.setTransactionId(request.getTransactionId());%0A                    transport.send(response
+)%0A                        transport.send(response);%0A                %7D else if (/*broadcast*/ request.getServerAddress() == Modbus.BROADCAST_ID && getSlave().isBroadcastEnabled()) %7B%0A                    //we do not answer broadcast requests%0A                    request.process(dataHolder
 );%0A 

fix handling of broadcast requests.