Do I need to dynamicaly change source to close connection? If I do it in
client updateSink Success case there is no effect.

2015-04-10 11:59 GMT+03:00 Akka Team <akka.offic...@gmail.com>:

> Hi,
>
> On Tue, Apr 7, 2015 at 3:57 PM, zergood <zergoodso...@gmail.com> wrote:
>
>> Is there an api for closing client connection?
>>
>
> The API is there, but it is "implicit". By completing the input side of
> the TCP Flow the connections write side is automatically closed. For
> example:
>
>   Source.empty.via(connectionFlow)
>
> will immediately close the writing side of the connection (half-close)
> since the empty source immediately completes.
>
> -Endre
>
>
>>
>> вторник, 7 апреля 2015 г., 16:40:46 UTC+3 пользователь Akka Team написал:
>>
>>>
>>>
>>> On Tue, Apr 7, 2015 at 3:38 PM, zergood <zergoo...@gmail.com> wrote:
>>>
>>>> Here is my client code:
>>>>   def downloadUpdate(address: InetSocketAddress, outputFilePath:String
>>>> )(implicit system: ActorSystem):Unit = {
>>>>     import scala.concurrent.duration._
>>>>     implicit val actorStreamMaterializer = ActorFlowMaterializer()
>>>>     val connection = StreamTcp(system).outgoingConnection(address,
>>>>       connectTimeout = 20.seconds, idleTimeout = 20.seconds)
>>>>
>>>>
>>>>     val updateSink = OnCompleteSink[Unit] {
>>>>       case Success(_) =>
>>>>       case Failure(ex) =>
>>>>     }
>>>>
>>>>
>>>>     val download = FlowGraph { implicit b =>
>>>>       import akka.stream.scaladsl.FlowGraphImplicits._
>>>>       val in = Source(Promise().future)
>>>>       val process = Flow[ByteString].map(chunk => {
>>>>         receiveDataStreamChunk(chunk, outputFilePath)
>>>>       })
>>>>
>>>>
>>>>       in ~> connection.flow ~> process ~> updateSink
>>>>     }
>>>>
>>>>
>>>>     download.run()
>>>>   }
>>>>
>>>>
>>>>   def receiveDataStreamChunk(chunk: ByteString, outputFilePath:String):
>>>> Unit
>>>>
>>>> receiveDataStreamChunk method simply write chunks to file.
>>>>
>>>> As I understand you correctly client needs to close connection when all
>>>> data is received. In my scenario only server knows if data is sended
>>>> completly or not. May be I don't understand you but why is TCP connection
>>>> not closed by server automatically when everything is sended?
>>>>
>>>
>>> It does, but TCP has half-close, so it does not terminate the
>>> server-side flow until the client closes its half of the TCP connection.
>>>
>>> -Endre
>>>
>>>
>>>> If there is an exception in the receiveDataStreamChunk method, does it
>>>> lead to closing client TCP connection? Or do I need to close it in
>>>> updateSink Failure case?
>>>>
>>>> вторник, 7 апреля 2015 г., 15:00:39 UTC+3 пользователь drewhk написал:
>>>>
>>>>>
>>>>>
>>>>> On Tue, Apr 7, 2015 at 1:47 PM, zergood <zergoo...@gmail.com> wrote:
>>>>>
>>>>>> Thank you for your answer.
>>>>>>
>>>>>> But If I use onCompleteSink after connection flow, future will not be
>>>>>> completed successfully however data will be downloaded by client.
>>>>>>
>>>>>
>>>>> The only reason for that could be that the client does not close the
>>>>> connection after it downloaded the data you have sent. However, connection
>>>>> close might have nothing to do whether the data has been successfully
>>>>> received or not.
>>>>>
>>>>>
>>>>>> If client is failed while downloading there are still no exceptions
>>>>>> on server side.
>>>>>>
>>>>>  Could you provide me some links from documentation about exception
>>>>>> handling in StreamTcp?
>>>>>>
>>>>>
>>>>> There is nothing special about StreamTcp, it works like any other
>>>>> Flow. The onError signal only travels downwards.
>>>>>
>>>>>
>>>>>> Do I need onCompleteSink in my case at all?
>>>>>>
>>>>>> updateSource ~> connection.flow ~> OnCompleteSink[ByteString]
>>>>>>
>>>>>
>>>>> This OnCompleteSink will provide a future that will finish with
>>>>> success if the connection has been normally close, or failing otherwise. 
>>>>> It
>>>>> can say nothing about whether the user successfully downloaded anything or
>>>>> not by default unless if the client can be assumed to only close the
>>>>> connection once everything has been processed. How does your client look
>>>>> like?
>>>>>
>>>>> -Endre
>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>> вторник, 7 апреля 2015 г., 14:16:13 UTC+3 пользователь Akka Team
>>>>>> написал:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> The connection flow will publish the errors, but you feed them to a
>>>>>>> BackholeSink. Also, your OnCompleteSink is probably at the wrong place 
>>>>>>> --
>>>>>>> it will not tell you anything about whether the TCP connection have sent
>>>>>>> everything fine or not. As such, you will close the binding before you 
>>>>>>> have
>>>>>>> sent everything through TCP.
>>>>>>>
>>>>>>> -Endre
>>>>>>>
>>>>>>> On Mon, Apr 6, 2015 at 3:23 PM, zergood <zergoo...@gmail.com> wrote:
>>>>>>>
>>>>>>>> Hello!
>>>>>>>>
>>>>>>>> My server side:
>>>>>>>>
>>>>>>>>   def update: Iterator[ByteString]
>>>>>>>>
>>>>>>>>   lazy val binding = StreamTcp(system).bind(address, idleTimeout =
>>>>>>>> 20.seconds)
>>>>>>>>
>>>>>>>>   def start(): Future[Unit] = {
>>>>>>>>     val firstCompleted = Promise[Unit]()
>>>>>>>>     val foreachConnection = ForeachSink[IncomingConnection] {
>>>>>>>>       connection =>
>>>>>>>>         val handleConnection = FlowGraph { implicit b =>
>>>>>>>>           import akka.stream.scaladsl.FlowGraphImplicits._
>>>>>>>>           val updateSource = Source[ByteString](() => update)
>>>>>>>>           val broadcast = Broadcast[ByteString]
>>>>>>>>
>>>>>>>>           updateSource ~> broadcast
>>>>>>>>           broadcast ~> connection.flow ~> BlackholeSink
>>>>>>>>           broadcast ~> OnCompleteSink[ByteString] { res =>
>>>>>>>>             firstCompleted.complete(res)
>>>>>>>>           }
>>>>>>>>         }
>>>>>>>>
>>>>>>>>          handleConnection.run()
>>>>>>>>     }
>>>>>>>>
>>>>>>>>
>>>>>>>>     val connectionsMap = binding.connections.to(foreachConnection).
>>>>>>>> run()
>>>>>>>>     firstCompleted.future.flatMap {_ => binding.unbind(
>>>>>>>> connectionsMap)}
>>>>>>>>       .recoverWith { case ex => binding.unbind(connectionsMap).
>>>>>>>> flatMap(_ => {
>>>>>>>>       Future.failed(ex)
>>>>>>>>     })}
>>>>>>>>   }
>>>>>>>>
>>>>>>>>
>>>>>>>> If i have exception on the client side, this future will be
>>>>>>>> completed with Success anyway. Is there a way to handle tcp exceptions
>>>>>>>> (client disconect for example) ?
>>>>>>>>
>>>>>>>> --
>>>>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/c
>>>>>>>> urrent/additional/faq.html
>>>>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/grou
>>>>>>>> p/akka-user
>>>>>>>> ---
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Akka User List" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to akka-user+...@googlegroups.com.
>>>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Akka Team
>>>>>>> Typesafe - Reactive apps on the JVM
>>>>>>> Blog: letitcrash.com
>>>>>>> Twitter: @akkateam
>>>>>>>
>>>>>>  --
>>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/c
>>>>>> urrent/additional/faq.html
>>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/grou
>>>>>> p/akka-user
>>>>>> ---
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Akka User List" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to akka-user+...@googlegroups.com.
>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>  --
>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
>>>> current/additional/faq.html
>>>> >>>>>>>>>> Search the archives: https://groups.google.com/
>>>> group/akka-user
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Akka User List" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to akka-user+...@googlegroups.com.
>>>> To post to this group, send email to akka...@googlegroups.com.
>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> Akka Team
>>> Typesafe - Reactive apps on the JVM
>>> Blog: letitcrash.com
>>> Twitter: @akkateam
>>>
>>  --
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ:
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to akka-user+unsubscr...@googlegroups.com.
>> To post to this group, send email to akka-user@googlegroups.com.
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Akka Team
> Typesafe - Reactive apps on the JVM
> Blog: letitcrash.com
> Twitter: @akkateam
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Akka User List" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/akka-user/xbHOWA4fURA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to