Created
November 12, 2012 22:39
-
-
Save mediocregopher/4062522 to your computer and use it in GitHub Desktop.
Holds a connection to socketspam open. To use: make-connection initializes the connection and sets up recovery. send-data sends whatever string you give it to socketspam.
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 socketspam.pconn | |
(:use lamina.core aleph.tcp aleph.formats)) | |
(def socketspam-host "localhost") | |
(def socketspam-port 9000) | |
;Create an endpoint and ground it so messages don't get queued up | |
;if nothing is listening | |
(def endpoint (channel)) | |
(ground endpoint) | |
(defn send-data | |
"Sends the given data to the endpoint channel, which should hopefully | |
be siphoned into the tcp connection" | |
[data] | |
(enqueue endpoint data)) | |
(declare make-connection) | |
(defn connection-established | |
"When a connection is established, forward everything going to endpoint into | |
it. Also, set up recovery handler" | |
[socket] | |
(println "Socket connection established") | |
(on-closed socket make-connection) | |
(siphon endpoint socket)) | |
(defn connection-error | |
"On connection closed, wait a minute and try to recover. We use future | |
to keep the stack from growing (I think that will work)" | |
[error] | |
(println "Error connecting to socketspam: " error) | |
(Thread/sleep 1000) | |
(future (make-connection))) | |
(defn make-connection | |
"Initializes the connection to socketspam" | |
[] | |
(on-realized (tcp-client {:host socketspam-host :port socketspam-port}) | |
connection-established | |
connection-error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment