While working on TRC I made a collection of popular IRC clients per platform, only to realise BlackBerry 10 had no real IRC clients. The best one available seemed to be an Android client running in an emulator.
So I asked a friend, collector of phones and owner of Heris IT to help me build a native IRC client for BlackBerry 10 using their Qt based Cascades framework.
This is a beta release that costs half of the final product, but also has half the features. We are doing this early release to collect some feedback, so please let us know what is important to you.
There are a few known issues:
Sending messages is broken on 10.2
The “Settings” button is a dummy.
The “Change nick” button is a dummy.
When the phone is standby the server kicks you of after a few minutes.
I have been trying to implement a CyclicBarrier in core.async, but it took me a while to understand how a channel works.
To support IOC and thread and be fast, the implementation is a lot more verbose and complicated than the bunch of promises I initially expected.
It seems all verbosity and callback hell that can be avoided by using channels is bundled in their implementation. Take this loop from cleanup that removes inactive handlers, can you spot the 2 actual lines of code?
But let’s go back to the beginning. The basic unit of a channel is a handler, a glorified callback. It has a commit method with returns the callback, an id for avoiding deadlock and a method to check if it is active.
The latter is used with alts!!, so that after one possible channel has been acted upon, the other handlers turn inactive, but lets stick to a simple <!!.
(defprotocolHandler(active?[h]"returns true if has callback. Must work w/o lock")(lock-id[h]"a unique id for lock acquisition order, 0 if no lock")(commit[h]"commit to fulfilling its end of the transfer, returns cb. Must be called within lock"));; no locking, always active.(defn-fn-handler[f](reifyLock(lock[_])(unlock[_])impl/Handler(active?[_]true)(lock-id[_]0)(commit[_]f)))
Now let’s look at <!! itself, which is pretty simple. It creates a promise and a callback that delivers the promise. It then calls take! with it. If take! returns nil, deref the promise, otherwise deref the box.
(defn<!!"takes a val from port. Will return nil if closed. Will block
if nothing is available."[port](let[p(promise)ret(impl/take!port(fn-handler(fn[v](deliverpv))))](ifret@ret(derefp))))
Now let’s look at the main course, ManyToManyChannel.
If you look through all the verbosity, take! and put! are pretty similar. There are 3 main code paths in each.
There is someone at the other end. Match up the handlers, call their callbacks and return the value or nil in an IDeref.
There is room in the buffer. Take/put the value in the buffer and return the value or nil in an IDeref.
There is no room. Put the handler in a list and return nil.
(take; remove all inactive handlers(cleanupthis)(let[^Lockhandlerhandler; get the actual callback if it is activecommit-handler(fn[](.lockhandler)(let[take-cb(and(impl/active?handler)(impl/commithandler))](.unlockhandler)take-cb))]; If there are items in the buffer,; take one, put it in a box and return it.; We now have a free spot in the buffer; so we try to find an active put handler.; if we find one, put its value in the buffer; and commit its callback.(if(andbuf(pos?(countbuf)))(do(if-let[take-cb(commit-handler)](let[val(impl/remove!buf)iter(.iteratorputs)cb(when(.hasNextiter)(loop[[^Lockputterval](.nextiter)](.lockputter)(let[cb(and(impl/active?putter)(impl/commitputter))](.unlockputter)(.removeiter)(ifcb(do(impl/add!bufval)cb)(when(.hasNextiter)(recur(.nextiter)))))))](.unlockmutex)(whencb(dispatch/runcb))(boxval))(do(.unlockmutex)nil))); There is nu buffer to take from,; so we search for an active putter; for each putter check if both ends are active.; If so, remove it from the list and let the callbacks and value,(let[iter(.iteratorputs)[take-cbput-cbval](when(.hasNextiter)(loop[[^Lockputterval](.nextiter)]; funny bit where deadlock is avoided(if(<(impl/lock-idhandler)(impl/lock-idputter))(do(.lockhandler)(.lockputter))(do(.lockputter)(.lockhandler)))(let[ret(when(and(impl/active?handler)(impl/active?putter))[(impl/commithandler)(impl/commitputter)val])](.unlockhandler)(.unlockputter)(ifret(do(.removeiter)ret)(when-not(impl/active?putter)(.removeiter)(when(.hasNextiter)(recur(.nextiter))))))))]; if we found 2 callbacks in the previous step; immediately return the value in a box and commit the putter.; if the channel is closed, return nil in a box.; if the channel is open and there is no matching callback; add the handler to the list of takers; and return nil, without a box.(if(andput-cbtake-cb)(do(.unlockmutex)(dispatch/runput-cb)(boxval))(if@closed(do(.unlockmutex)(if-let[take-cb(commit-handler)](boxnil)nil))(do(.addtakeshandler)(.unlockmutex)nil)))))))
I wonder if the code could be made more readable with a few macros for locking and iterators.
Understanding put! is left as a exercise for the reader. Understanding the IOC part is left as an exercise for the Clojure gods.
I will write more about my ideas for barriers and transactions later.
I finally took some time to cut out the private bits from my deploy script and open source it to the world.
This script allows you to deploy an IRC server, bouncer, web interface, bot and some custom modules to anything you can think of: VirtualBox, VPS, EC2, OpenStack, Docker…
The only things that are missing are the SSL certificates and services.
You can add your own certificates, generate self-signed ones, or disable SSL altogether: