Discussion:
Help with the TCP-server example
Robin Mattheussen
2015-10-06 02:33:27 UTC
Permalink
Hi,

I'm extremely new but very interested in developing in Clojure. I'm not a
professional, so please bear with me.

One of the first things I'm trying to do is getting an extremely simple TCP
server up and running with Aleph, but I don't quite understand the examples.

On GitHub, there is the following example:

(require '[aleph.tcp :as tcp])

(defn echo-handler [s info]
(s/connect s s))

(tcp/start-server echo-handler {:port 10001})


The *start-server* part is pretty obvious, and as far as I understand,
*s/connect* basically tells one stream to act as the source for another
stream which will act as a sink. So why are we directing the stream to
itself?
When I follow the link to more "complete" examples
<http://ideolalia.com/aleph/literate.html#aleph.examples.tcp>, it starts
off by immediately using Gloss, which is a little over my head. With this
example I don't even understand how I would go about logging incoming
messages to the console at the moment. Elaborating how I would go about
handling the stream, both in inspecting the content and transforming it,
would help me.

My use case is to take accept messages of unknown length from the clients,
transform said messages (which brings me to the question: can I map over a
stream?) and then ship them over to a queue to be handled by a different
part of the application. Very simple stuff really.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tristan Seligmann
2015-10-07 10:16:40 UTC
Permalink
Post by Robin Mattheussen
I'm extremely new but very interested in developing in Clojure. I'm not a
professional, so please bear with me.
Welcome! I'm not exactly an Aleph expert, but I'll try to help with your
questions.

The *start-server* part is pretty obvious, and as far as I understand,
Post by Robin Mattheussen
*s/connect* basically tells one stream to act as the source for another
stream which will act as a sink. So why are we directing the stream to
itself?
The example is an "echo" server; so anything that the client sends, the
server should send back to the client. Connecting the stream to itself just
feeds anything that comes out of the stream back into the stream, achieving
the desired result.

When I follow the link to more "complete" examples
Post by Robin Mattheussen
<http://ideolalia.com/aleph/literate.html#aleph.examples.tcp>, it starts
off by immediately using Gloss, which is a little over my head. With this
example I don't even understand how I would go about logging incoming
messages to the console at the moment. Elaborating how I would go about
handling the stream, both in inspecting the content and transforming it,
would help me.
The streams in Aleph are Manifold streams, so it may be helpful to look at
the docs for Manifold as well.

My use case is to take accept messages of unknown length from the clients,
Post by Robin Mattheussen
transform said messages (which brings me to the question: can I map over a
stream?) and then ship them over to a queue to be handled by a different
part of the application. Very simple stuff really.
You can map over a stream with map from Manifold:
http://aleph.io/codox/manifold/manifold.stream.html#var-map

However, regarding your messages of unknown length, you will generally need
some kind of framing to handle them; the best way to do this is usually to
prefix the message with its length, but it is also possible to use some
kind of delimeter to separate messages. (It wasn't clear whether you were
talking about designing a new protocol, or implementing some existing
messaging protocol)

Either framing mechanism should be fairly straightforward to parse with
gloss, which lets you turn a stream of bytes (what you get from a TCP
connection) into a stream of messages; you could then map over the stream
to place the messages onto the queue, or use some other way of connecting
the two.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...