Skip to content

Instantly share code, notes, and snippets.

@codification
Last active December 16, 2015 00:30
Show Gist options
  • Save codification/5348492 to your computer and use it in GitHub Desktop.
Save codification/5348492 to your computer and use it in GitHub Desktop.
FP-meetup Token-Ring
(ns tokenring.core
(:require [lamina.core :as lamina])
(:require [aleph.tcp :as aleph])
(:require [gloss.core :as gloss]))
(defn connect-and-send [message]
(println (str "connecting and sending: " message))
(let [ch (lamina/wait-for-result
(aleph/tcp-client {:host "192.168.48.35"
;;:host "localhost"
:port 1234
:frame (gloss/string :utf-8 :delimiters ["\n"])}))]
(lamina/enqueue ch message)))
(defn handle-message [msg]
(-> msg
read-string
inc
str
connect-and-send))
(defn echo-handler [channel client-info]
(println client-info " connected")
(lamina/receive-all channel handle-message))
(defonce server (atom nil))
(defn start-server []
(reset! server (aleph/start-tcp-server #'echo-handler
{:port 1234
:frame (gloss/string :utf-8 :delimiters ["\n"])})))
(defn stop-server []
(@server))
(defproject tokenring "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[aleph "0.3.0-beta16"]]
:repositories [["sonatype-oss-public" {:url "http://oss.sonatype.org/content/groups/public"}]
["sonatype" {:url "http://oss.sonatype.org/content/repositories/releases"}]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment