.. index:: single: linda_server
.. _linda_server/0:

.. rst-class:: right

**category**

``linda_server``
================

Linda server predicates and tuple-space state. Import into a threaded object together with the linda_client category.

| **Availability:** 
|    ``logtalk_load(linda(loader))``

| **Author:** Paulo Moura
| **Version:** 2:0:4
| **Date:** 2026-07-14

| **Compilation flags:**
|    ``static``


| **Provides:**
|    :ref:`logtalk::message_prefix_stream/4 <logtalk/0::message_prefix_stream/4>`
| **Uses:**
|    :ref:`list <list/0>`
|    :ref:`logtalk <logtalk/0>`
|    :ref:`os <os/0>`
|    :ref:`socket <socket/0>`

| **Remarks:**
|    (none)

| **Inherited public predicates:**
|    (none)

.. contents::
   :local:
   :backlinks: top

Public predicates
-----------------

.. index:: linda/1
.. _linda_server/0::linda/1:

``linda/1``
^^^^^^^^^^^

Starts a Linda server with the given options. The predicate succeeds when all clients have disconnected after a shutdown request.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``linda(Options)``
| **Meta-predicate template:**
|    ``linda(::)``
| **Mode and number of proofs:**
|    ``linda(+list)`` - ``one``

| **Remarks:**

    - Option ``port(Port)``: Use ``Port`` as the server port. Must be an integer and an available port.
    - Option ``Address-Goal``: ``Address`` is unified with ``Host:Port`` and ``Goal`` is called when the server starts. Useful for saving the address or starting clients.
    - Option ``accept_hook(Client,Input,Output,Goal)``: When a client connects, ``Client`` is unified with the client address, ``Input`` and ``Output`` with the connection streams, and ``Goal`` is called. If ``Goal`` fails, the connection is rejected.


------------

.. index:: linda/0
.. _linda_server/0::linda/0:

``linda/0``
^^^^^^^^^^^

Starts a Linda server on an automatically assigned port using default options. The server address (``Host:Port``) is written to the current output stream. The predicate succeeds when all clients have disconnected after a shutdown request.

| **Compilation flags:**
|    ``static``

| **Mode and number of proofs:**
|    ``linda`` - ``one``


------------

Protected predicates
--------------------

(no local declarations; see entity ancestors if any)

Private predicates
------------------

.. index:: server_socket_/1
.. _linda_server/0::server_socket_/1:

``server_socket_/1``
^^^^^^^^^^^^^^^^^^^^

Stores the server socket descriptor.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``server_socket_(ServerSocket)``
| **Mode and number of proofs:**
|    ``server_socket_(?term)`` - ``zero_or_one``


------------

.. index:: client_connection_/3
.. _linda_server/0::client_connection_/3:

``client_connection_/3``
^^^^^^^^^^^^^^^^^^^^^^^^

Stores active client connections. Each client has an ID, input stream, and output stream.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``client_connection_(ClientId,InputStream,OutputStream)``
| **Mode and number of proofs:**
|    ``client_connection_(?term,?term,?term)`` - ``zero_or_more``


------------

.. index:: accept_hook_/1
.. _linda_server/0::accept_hook_/1:

``accept_hook_/1``
^^^^^^^^^^^^^^^^^^

Stores the optional accept hook goal to call when a client connects.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``accept_hook_(Hook)``
| **Mode and number of proofs:**
|    ``accept_hook_(?callable)`` - ``zero_or_one``


------------

.. index:: server_running_/1
.. _linda_server/0::server_running_/1:

``server_running_/1``
^^^^^^^^^^^^^^^^^^^^^

Flag indicating the server is running.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``server_running_(Address)``
| **Mode and number of proofs:**
|    ``server_running_(?address)`` - ``zero_or_one``


------------

.. index:: server_shutdown_/0
.. _linda_server/0::server_shutdown_/0:

``server_shutdown_/0``
^^^^^^^^^^^^^^^^^^^^^^

Flag indicating the server has received a shutdown request.

| **Compilation flags:**
|    ``dynamic``

| **Mode and number of proofs:**
|    ``server_shutdown_`` - ``zero_or_one``


------------

.. index:: accept_loop_stopped_/0
.. _linda_server/0::accept_loop_stopped_/0:

``accept_loop_stopped_/0``
^^^^^^^^^^^^^^^^^^^^^^^^^^

Flag indicating the server accept loop has stopped.

| **Compilation flags:**
|    ``dynamic``

| **Mode and number of proofs:**
|    ``accept_loop_stopped_`` - ``zero_or_one``


------------

.. index:: tuple_/1
.. _linda_server/0::tuple_/1:

``tuple_/1``
^^^^^^^^^^^^

Stores tuples in the Linda tuple space.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``tuple_(Tuple)``
| **Mode and number of proofs:**
|    ``tuple_(?term)`` - ``zero_or_more``


------------

.. index:: waiting_/3
.. _linda_server/0::waiting_/3:

``waiting_/3``
^^^^^^^^^^^^^^

Stores blocked clients waiting for tuples. Records the client ID, request pattern, and output stream.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``waiting_(ClientId,Request,OutputStream)``
| **Mode and number of proofs:**
|    ``waiting_(?term,?term,?term)`` - ``zero_or_more``


------------

.. index:: client_engine_/2
.. _linda_server/0::client_engine_/2:

``client_engine_/2``
^^^^^^^^^^^^^^^^^^^^

Maps client IDs to their corresponding threaded engine names.

| **Compilation flags:**
|    ``dynamic``

| **Template:**
|    ``client_engine_(ClientId,EngineName)``
| **Mode and number of proofs:**
|    ``client_engine_(?term,?atom)`` - ``zero_or_more``


------------

.. index:: ts_out/1
.. _linda_server/0::ts_out/1:

``ts_out/1``
^^^^^^^^^^^^

Synchronized predicate to add a tuple to the tuple space and wake waiting clients.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_out(Tuple)``
| **Mode and number of proofs:**
|    ``ts_out(+term)`` - ``one``


------------

.. index:: ts_in/4
.. _linda_server/0::ts_in/4:

``ts_in/4``
^^^^^^^^^^^

Synchronized predicate to remove a matching tuple or register a waiting client.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_in(Tuple,ClientId,OutputStream,Found)``
| **Mode and number of proofs:**
|    ``ts_in(+term,+term,+term,-compound)`` - ``one``


------------

.. index:: ts_in_noblock/2
.. _linda_server/0::ts_in_noblock/2:

``ts_in_noblock/2``
^^^^^^^^^^^^^^^^^^^

Synchronized predicate to try removing a matching tuple without blocking.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_in_noblock(Tuple,Found)``
| **Mode and number of proofs:**
|    ``ts_in_noblock(+term,-compound)`` - ``zero_or_one``


------------

.. index:: ts_in_list/4
.. _linda_server/0::ts_in_list/4:

``ts_in_list/4``
^^^^^^^^^^^^^^^^

Synchronized predicate to remove a tuple matching one of multiple patterns or register a waiting client.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_in_list(TupleList,ClientId,OutputStream,Found)``
| **Mode and number of proofs:**
|    ``ts_in_list(+list,+term,+term,-compound)`` - ``one``


------------

.. index:: ts_rd/4
.. _linda_server/0::ts_rd/4:

``ts_rd/4``
^^^^^^^^^^^

Synchronized predicate to read a matching tuple or register a waiting client.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_rd(Tuple,ClientId,OutputStream,Found)``
| **Mode and number of proofs:**
|    ``ts_rd(+term,+term,+term,-compound)`` - ``one``


------------

.. index:: ts_rd_noblock/2
.. _linda_server/0::ts_rd_noblock/2:

``ts_rd_noblock/2``
^^^^^^^^^^^^^^^^^^^

Synchronized predicate to try reading a matching tuple without blocking.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_rd_noblock(Tuple,Found)``
| **Mode and number of proofs:**
|    ``ts_rd_noblock(+term,-compound)`` - ``zero_or_one``


------------

.. index:: ts_rd_list/4
.. _linda_server/0::ts_rd_list/4:

``ts_rd_list/4``
^^^^^^^^^^^^^^^^

Synchronized predicate to read a tuple matching one of multiple patterns or register a waiting client.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_rd_list(TupleList,ClientId,OutputStream,Found)``
| **Mode and number of proofs:**
|    ``ts_rd_list(+list,+term,+term,-compound)`` - ``one``


------------

.. index:: ts_findall_rd_noblock/3
.. _linda_server/0::ts_findall_rd_noblock/3:

``ts_findall_rd_noblock/3``
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Synchronized predicate to collect all tuples matching a pattern.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_findall_rd_noblock(Template,Tuple,List)``
| **Mode and number of proofs:**
|    ``ts_findall_rd_noblock(+term,+term,-list)`` - ``zero_or_more``


------------

.. index:: ts_findall_in_noblock/3
.. _linda_server/0::ts_findall_in_noblock/3:

``ts_findall_in_noblock/3``
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Synchronized predicate to collect and remove all tuples matching a pattern.

| **Compilation flags:**
|    ``static, synchronized``

| **Template:**
|    ``ts_findall_in_noblock(Template,Tuple,List)``
| **Mode and number of proofs:**
|    ``ts_findall_in_noblock(+term,+term,-list)`` - ``zero_or_more``


------------

Operators
---------

(none)

