@@ -111,8 +111,9 @@ defmodule Boombox.Server do
111111 membrane_source_demand: non_neg_integer ( ) ,
112112 pipeline_supervisor: pid ( ) | nil ,
113113 pipeline: pid ( ) | nil ,
114- ghosted_client: GenServer . from ( ) | pid ( ) | nil ,
115- pipeline_exit_reason: term ( )
114+ ghosted_client: GenServer . from ( ) | Process . dest ( ) | nil ,
115+ pipeline_termination_reason: term ( ) ,
116+ termination_requested: boolean ( )
116117 }
117118
118119 @ enforce_keys [
@@ -131,7 +132,8 @@ defmodule Boombox.Server do
131132 pipeline_supervisor: nil ,
132133 pipeline: nil ,
133134 ghosted_client: nil ,
134- pipeline_exit_reason: nil
135+ pipeline_termination_reason: nil ,
136+ termination_requested: false
135137 ]
136138 end
137139
@@ -349,11 +351,16 @@ defmodule Boombox.Server do
349351
350352 case state . communication_medium do
351353 :calls ->
352- if state . ghosted_client != nil do
353- reply ( state . ghosted_client , :finished )
354- { :stop , reason , state }
355- else
356- { :noreply , % State { state | pipeline_exit_reason: reason } }
354+ cond do
355+ state . ghosted_client != nil ->
356+ reply ( state . ghosted_client , :finished )
357+ { :stop , reason , state }
358+
359+ state . termination_requested ->
360+ { :stop , reason , state }
361+
362+ true ->
363+ { :noreply , % State { state | pipeline_termination_reason: reason } }
357364 end
358365
359366 :messages ->
@@ -386,7 +393,7 @@ defmodule Boombox.Server do
386393 end
387394 end
388395
389- @ spec handle_request ( { :run , boombox_opts ( ) } , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
396+ @ spec handle_request ( { :run , boombox_opts ( ) } , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
390397 { :reply , boombox_mode ( ) , State . t ( ) }
391398 defp handle_request ( { :run , boombox_opts } , from , state ) do
392399 boombox_mode = get_boombox_mode ( boombox_opts )
@@ -406,7 +413,7 @@ defmodule Boombox.Server do
406413 } }
407414 end
408415
409- @ spec handle_request ( :get_pid , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
416+ @ spec handle_request ( :get_pid , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
410417 { :reply , pid ( ) , State . t ( ) }
411418 defp handle_request ( :get_pid , _from , state ) do
412419 { :reply , self ( ) , state }
@@ -418,7 +425,7 @@ defmodule Boombox.Server do
418425
419426 @ spec handle_request (
420427 { :consume_packet , serialized_boombox_packet ( ) | Boombox.Packet . t ( ) } ,
421- GenServer . from ( ) | pid ( ) ,
428+ GenServer . from ( ) | Process . dest ( ) ,
422429 State . t ( )
423430 ) ::
424431 { :reply , :ok | { :error , :incompatible_mode | :boombox_not_running } , State . t ( ) }
@@ -438,8 +445,8 @@ defmodule Boombox.Server do
438445 state = % State { state | membrane_source_demand: state . membrane_source_demand - 1 }
439446
440447 cond do
441- state . pipeline_exit_reason != nil ->
442- { :stop , state . pipeline_exit_reason , :finished , state }
448+ state . pipeline_termination_reason != nil ->
449+ { :stop , state . pipeline_termination_reason , :finished , state }
443450
444451 state . membrane_source_demand == 0 ->
445452 { :noreply , % State { state | ghosted_client: from } }
@@ -457,29 +464,29 @@ defmodule Boombox.Server do
457464 { :reply , { :error , :incompatible_mode } , state }
458465 end
459466
460- @ spec handle_request ( :finish_consuming , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
461- { :reply , :ok | :finished | { :error , :incompatible_mode } , State . t ( ) }
462- | { :stop , term ( ) , :finished , State . t ( ) }
467+ @ spec handle_request ( :finish_consuming , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
468+ { :reply , :ok | { :error , :incompatible_mode } , State . t ( ) }
469+ | { :stop , term ( ) , :ok , State . t ( ) }
463470 defp handle_request ( :finish_consuming , _from , % State { boombox_mode: :consuming } = state ) do
464- if state . pipeline_exit_reason != nil do
465- { :stop , state . pipeline_exit_reason , :finished , state }
471+ if state . pipeline_termination_reason != nil do
472+ { :stop , state . pipeline_termination_reason , :ok , state }
466473 else
467474 send ( state . membrane_source , { :boombox_eos , self ( ) } )
468- { :reply , :ok , state }
475+ { :reply , :ok , % State { state | termination_requested: true } }
469476 end
470477 end
471478
472479 defp handle_request ( :finish_consuming , _from , % State { boombox_mode: _other_mode } = state ) do
473480 { :reply , { :error , :incompatible_mode } , state }
474481 end
475482
476- @ spec handle_request ( :produce_packet , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
483+ @ spec handle_request ( :produce_packet , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
477484 { :reply , { :error , :incompatible_mode | :boombox_not_running } , State . t ( ) }
478485 | { :noreply , State . t ( ) }
479486 | { :stop , term ( ) , :finished , State . t ( ) }
480487 defp handle_request ( :produce_packet , from , % State { boombox_mode: :producing } = state ) do
481- if state . pipeline_exit_reason != nil do
482- { :stop , state . pipeline_exit_reason , :finished , state }
488+ if state . pipeline_termination_reason != nil do
489+ { :stop , state . pipeline_termination_reason , :finished , state }
483490 else
484491 send ( state . membrane_sink , { :boombox_demand , self ( ) } )
485492 { :noreply , % State { state | ghosted_client: from } }
@@ -490,30 +497,34 @@ defmodule Boombox.Server do
490497 { :reply , { :error , :incompatible_mode } , state }
491498 end
492499
493- @ spec handle_request ( :finish_producing , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
500+ @ spec handle_request ( :finish_producing , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
494501 { :reply , :ok | { :error , :incompatible_mode } , State . t ( ) }
495502 defp handle_request ( :finish_producing , _from , % State { boombox_mode: :producing } = state ) do
496503 Membrane.Pipeline . terminate ( state . pipeline , asynchronous?: true )
497- { :reply , :ok , state }
504+ { :reply , :ok , % State { state | termination_requested: true } }
498505 end
499506
500507 defp handle_request ( :finish_producing , _from , % State { boombox_mode: _other_mode } = state ) do
501508 { :reply , { :error , :incompatible_mode } , state }
502509 end
503510
504- @ spec handle_request ( term ( ) , GenServer . from ( ) | pid ( ) , State . t ( ) ) ::
511+ @ spec handle_request ( term ( ) , GenServer . from ( ) | Process . dest ( ) , State . t ( ) ) ::
505512 { :reply , { :error , :invalid_request } , State . t ( ) }
506513 defp handle_request ( _invalid_request , _from , state ) do
507514 { :reply , { :error , :invalid_request } , state }
508515 end
509516
510- @ spec reply ( GenServer . from ( ) | pid ( ) , term ( ) ) :: :ok
511- defp reply ( pid , reply_content ) when is_pid ( pid ) do
512- send ( pid , { :response , reply_content } )
517+ @ spec reply ( GenServer . from ( ) | Process . dest ( ) , term ( ) ) :: :ok
518+ defp reply ( dest , reply_content ) when is_pid ( dest ) or is_port ( dest ) or is_atom ( dest ) do
519+ send ( dest , { :response , reply_content } )
520+ end
521+
522+ defp reply ( { name , node } = dest , reply_content ) when is_atom ( name ) and is_atom ( node ) do
523+ send ( dest , { :response , reply_content } )
513524 end
514525
515- defp reply ( { pid , _tag } = client , reply_content ) when is_pid ( pid ) do
516- GenServer . reply ( client , reply_content )
526+ defp reply ( { pid , _tag } = genserver_from , reply_content ) when is_pid ( pid ) do
527+ GenServer . reply ( genserver_from , reply_content )
517528 end
518529
519530 @ spec get_boombox_mode ( boombox_opts ( ) ) :: boombox_mode ( )
0 commit comments