processing a socket stream

45 views
Skip to first unread message

finbeu

unread,
Mar 3, 2011, 5:25:53 AM3/3/11
to Clojure
Hello!

I'm new to clojure and socket programming in particular and I kindly
ask you to help me a little bit in how to design my application (all
this is still overwhelming). This is a task I gave me to have
something usefull while learning the clojure api:

The scenario is as follows:

I have a client application that connects to a server on a port, opens
a socket stream and pumps in a stream of xml data. This streams never
stops, but the protocol is simple, key/value data. That's it.

1/ I want to write a server in clojure that accepts the connection
from this client application, parses the xml-input stream and builds a
cache with the key/values received from xml-parsing this stream.

2/ I want to start threads within my server that do some work (get
notified) each time the cache has new/updated items.

My naive approach is as follows:

1) use create-server to build the server that accepts the socket
connection
2) use an atom as the cache that gets updated by the input stream
handler/thread
3) use futures to start my working threads

But how do I notify my working threads that the cache was updated? Are
there some (clojure) patterns that I can use?

Also some boilerplate or examples would help me a lot to get
started! :-)

Thanks!

- Finn (finbeu)



James Reeves

unread,
Mar 3, 2011, 7:04:00 AM3/3/11
to clo...@googlegroups.com
On 3 March 2011 10:25, finbeu <info_...@t-online.de> wrote:
> But how do I notify my working threads that the cache was updated? Are
> there some (clojure) patterns that I can use?

Presumably you're starting your worker processes to avoid the overhead
of starting a new thread each time? In which case, a better solution
may be to use a thread pool instead.

- James

Miki

unread,
Mar 3, 2011, 9:03:04 AM3/3/11
to clo...@googlegroups.com

But how do I notify my working threads that the cache was updated? Are
there some (clojure) patterns that I can use?

finbeu

unread,
Mar 3, 2011, 9:39:18 AM3/3/11
to Clojure
With my limited clojure skills I just managed to get the create-server
up and running :-)

Now I found out that I don't know how to do the following thing (sorry
for my beginner questions):

The stream arrives with a sequence of 10 characters. These 10
characters (e.g. 0000000190) give me the exact size (190 characters)
of the next full xml-message. So I have to read 10 characters first,
then the subsequent 190 characters which give me the xml-message, and
so on and so forth

But I have right now no clue to do this (navigating to the stream
forth by a fixed number of bytes). Is there a clojure function to do
this or do I have to fall back to java?

Thanks.

James Reeves

unread,
Mar 3, 2011, 9:59:53 AM3/3/11
to clo...@googlegroups.com
On 3 March 2011 14:39, finbeu <info_...@t-online.de> wrote:
> But I have right now no clue to do this (navigating to the stream
> forth by a fixed number of bytes). Is there a clojure function to do
> this or do I have to fall back to java?

You're better off falling back to Java for this. Clojure doesn't yet
have a native mechanism for handling streaming data, so the standard
Java stream classes are generally used when dealing with I/O.

However, there are a bunch of helper functions in clojure.java.io that
you'll definitely want to use.

Once you have your stream, you probably want a function like:

(defn read-bytes [stream n]
(let [bytes (byte-array n)]
(.read stream bytes)
bytes))

And then turn it into a string with:

(String. bytes "UTF-8")

- James

finbeu

unread,
Mar 3, 2011, 10:10:01 AM3/3/11
to Clojure
James thx. That is what I need for my project: Some coaching when to
fall back to java, when to use clojure function and so on. That's
cool.

Rgds

- Finn

On 3 Mrz., 15:59, James Reeves <jree...@weavejester.com> wrote:
> On 3 March 2011 14:39, finbeu <info_pe...@t-online.de> wrote:
>
> > But I have right now no clue to do this (navigating to the stream

Reply all
Reply to author
Forward
0 new messages