Last active
December 16, 2015 00:30
-
-
Save codification/5348492 to your computer and use it in GitHub Desktop.
FP-meetup Token-Ring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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