<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:int="http://www.springframework.org/schema/integration"
	   xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/ip https://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

	<context:property-placeholder/>

	<!-- Client side -->

	<int:gateway id="gw"
				 service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
				 default-request-channel="input"/>

	<int-ip:tcp-connection-factory id="client"
								   type="client"
								   host="localhost"
								   port="#{crLfServer.port}"
								   single-use="true"
								   so-timeout="10000"/>

	<int:channel id="input"/>

	<int-ip:tcp-outbound-gateway id="outGateway"
								 auto-startup="false"
								 request-channel="input"
								 reply-channel="clientBytes2StringChannel"
								 connection-factory="client"
								 request-timeout="10000"
								 reply-timeout="10000"/>

	<int:object-to-string-transformer id="clientBytes2String"
									  input-channel="clientBytes2StringChannel"/>

	<!-- Server side -->

	<int-ip:tcp-connection-factory id="crLfServer"
								   type="server"
								   port="0"/>

	<int-ip:tcp-inbound-gateway id="gatewayCrLf"
								connection-factory="crLfServer"
								request-channel="serverBytes2StringChannel"
								error-channel="errorChannel"/>

	<int:channel id="toSA"/>

	<int:service-activator input-channel="toSA"
						   ref="echoService"
						   method="test"/>

	<bean id="echoService"
		  class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

	<int:object-to-string-transformer id="serverBytes2String"
									  input-channel="serverBytes2StringChannel"
									  output-channel="toSA"/>

	<int:transformer id="errorHandler"
					 input-channel="errorChannel"
					 expression="payload.failedMessage.payload + ':' + payload.cause.message"/>

</beans>
