Clojure server-socket ClassNotFoundException error - emacs

I'm trying to test mire.
When I execute this code
(ns mire
(:use [clojure.contrib server-socket duck-streams]))
I have java.io.FileNotFoundException error.
Googling to find that clojure.contrib is deprecated, I added server-socket as dependencies in the project.clj, and executed leon repl to install the jar libraries
mire> lein repl
Retrieving server-socket/server-socket/1.0.0/server-socket-1.0.0.pom from clojars
Retrieving server-socket/server-socket/1.0.0/server-socket-1.0.0.jar from clojar
Then in the emacs/cider, I tried to use this code to get the same FileNotFoundException error.
(ns mire
(:use [server.socket server-socket duck-streams]))
java.io.FileNotFoundException: Could not locate server/socket/server_socket__init.class or
server/socket/server_socket.clj on class path: 
In command line REPL, (use [server.socket server-socket]) gives me the same error message.
What might be wrong? I use emacs/cider/nREPL. This is the project.clj.
(defproject mire "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.5.1"]
[server-socket "1.0.0"]])
This is the code that causes an error:
(ns mire
(:use [clojure.contrib server-socket duck-streams]))
(def port (* 3 1111))
(defn mire-handle-client [in out]
(binding [*in* (reader in)
*out* (writer out)]
(loop []
(println (read-line))
(recur))))
(def server (create-server port mire-handle-client))

From the code in Building an Echo Server in Clojure - Part 2, this is the working code with server.socket.
(ns mire
(:use server.socket))
(import '[java.io BufferedReader InputStreamReader OutputStreamWriter])
(def port (* 3 1111))
(defn mire-handle-client [in out]
(binding [*in* (BufferedReader. (InputStreamReader. in))
*out* (OutputStreamWriter. out)]
(loop []
(println (read-line))
(recur))))
(def server (create-server port mire-handle-client))

The namespaces are usually dot-separated. server.socket in this library is.
Try this:
(ns mire (:use [server.socket]))

Related

How to use usocket to create a connection? (common lisp)

I'm trying to use socket in lisp to create a connection. Using sbcl, i found "usocket" available. But I failed to sending strings between the server and client. Here's the code:
Server:
(ql:quickload "usocket")
(use-package 'usocket)
(defun default-tcp-handler (stream) ; null
(declare (type stream stream))
(terpri stream))
(defparameter *us* (socket-server "127.0.0.1" 4547 #'default-tcp-handler))
(defparameter *s* (socket-accept *us*))
(defparameter *ss* (usocket:socket-stream *s*))
Client:
(ql:quickload "usocket")
(use-package 'usocket)
(defparameter *us* (socket-connect "127.0.0.1" 4547))
(defparameter *st* (usocket:socket-stream *us*))
I ran the server code first, it freezes. Then I ran the client code. But the server seemed to be no response.
I tried to format a string into the st stream:
(format *st* "hello, server")
But it returns nil.
How can i solve the problem??
Thanks a lot.
I tried your code and there were no errors, so everything seemed to be all right with the connection. However, if you only write to streams without reading (not to mention flushing the output), the connection has no way to manifest itself. Here is a modified version where the server reads a single line from the socket:
;;; server
(ql:quickload "usocket")
(use-package :usocket)
(defun default-tcp-handler (stream)
(declare (type stream stream))
(format t "~A~%" (read-line stream))
(finish-output))
(defparameter *us* (socket-server "127.0.0.1" 4547 #'default-tcp-handler))
;;; client
(ql:quickload "usocket")
(use-package :usocket)
(defparameter *us* (socket-connect "127.0.0.1" 4547))
(defparameter *st* (socket-stream *us*))
(write-line "hello server" *st*)
(finish-output *st*)
In your case format returned nil, because it always returns nil except format nil. The server hangs because it starts listening. If you don't want that, you'll have to work with multiple threads.

How to make swank working with stumpw?

I've added this code snippet to my stumpwmrc file:
(defun load-swank ()
"Load a swank server"
(ql:quickload 'swank)
(require 'swank)
(setq swank:*use-dedicated-output-stream* nil)
(setq slime-net-coding-system 'utf-8-unix)
(swank:create-server :port 4006))
(load-swank)
I am expecting to open a socket server, accepting the "swank" protocol. Thus I could connect to it with emacs (thanks to Slime).
But when I login and and stumpwm is reading its configuration file, here is the error message I get:
15:00:34 Outputting a message:
^B^1*Error loading ^b/home/ybaumes/.stumpwmrc^B: ^nThe name "SWANK" does not designate any package.
How can I fix that? I invoke 'require, or even 'quickload functions. What's the issue here?
A typical error is this:
You load the file and the reader sees the code:
SWANK is not loaded
(defun load-swank ()
"Load a swank server"
SWANK is not loaded
(ql:quickload 'swank)
SWANK is not loaded - remember, we are still reading the form.
(require 'swank)
SWANK is not loaded - remember, we are still reading the form.
Now use us a symbol in package which is not existing... the reader complains:
(setq swank:*use-dedicated-output-stream* nil) ; the package SWANK does not exist yet.
(setq slime-net-coding-system 'utf-8-unix)
(swank:create-server :port 4006))
Now you want to load SWANK:
(load-swank)
You can't use a symbol from a package which does not exist.
For example what works is this inside the function:
(setf (symbol-value (read-from-string "swank:*use-dedicated-output-stream*")) nil)
and so on.
You need to find the symbol at runtime of that function. Use (find-symbol "FOO" "SWANK") (remember Common Lisp is upcase internally) or (read-from-string "SWANK::FOO").

Organize/clean-up clojure library includes with emacs

Eclipse has the action 'Organize Imports', which removes all unused imports and cleans up wildcard-imports so that only the actually used members of the imported classes remain.
Does there exists a similar functionality for emacs and clojure-mode?
For example I have the following:
(ns some.namespace
(:use [some.lib]
[another.lib]))
From some.lib I only use fn1 and fn2. another.lib I don't use at all. That emacs command would then convert this to:
(ns some.namespace
(:use [some.lib :only [fn1 fn2]]))
Or equivalent:
(ns some.namespace
(:require [some.lib :refer :all]
[another.lib]))
converts to
(ns some.namespace
(:require [some.lib :refer [fn1 fn2]]))`
Take a look at slamhound: https://github.com/technomancy/slamhound. You can run it through lein or from emacs via slamhound.el

I cannot run tests in clojure/midje

I run tests with:
lein midje :autotest
And I get error:
Exception in thread "main" java.lang.Exception: No namespace: sprint-is.json-export found
File is in: sprint-is/src/sprint_is/json_export.clj
It contains code:
(ns sprint-is.json-export)
(require [[noir.response :as response]])
(defn serialize [value] (response/json value))
It throws this error even when I don't have no test file. When I create test file, I get similar error:
No namespace: sprint-is.test.json-export found
Test is in: sprint-is/test/sprint_is/json_export.clj
And contains:
(ns sprint-is.test.json-export
(:require [sprint-is.json-export :as json-export]))
(fact "module can serialize scalar values"
(json-export/serialize 123) => 123)
When I try to import it from REPL, it cannot find the namespaces too. I tried to rename file, move files, rename directories, remove ns (it compiles but it doesn't work), asked on Clojure IRC. I compared the code with other projects (including those working on my computer) and it seems same.
Source code is here: https://bitbucket.org/jiriknesl/sprintis
You have a compilation error in one of your namespaces, I suspect sprint-is.json-export
On bitbucket, you have this:
(ns sprint-is.json-export)
(require [[noir.response :as response]])
(defn serialize [value] (response/json value))
which won't compile because noir.response and response are not defined.
you should have:
(ns sprint-is.json-export
(:require [noir.response :as response]))
(defn serialize [value] (response/json value))
If you insist on using require outside of the ns macro, you can do the following, but be aware this is not idiomatic usage.
(ns sprint-is.json-export)
(require '[noir.response :as response])
(defn serialize [value] (response/json value))

loading clojure-contrib

I'm new to the whole JVM thing, and trying to play with clojure. I'm attempting to load clojure-contrib and failing:
# in bash
$ java -cp /path/to/clojure.jar:/path/to/contrib.jar clojure.main
# in REPL
user=> (require 'clojure.contrib.math)
nil
user=> (sqrt 2)
java.lang.Exception: Unable to resolve symbol: sqrt in this context (NO_SOURCE_FILE:10)
Any pointers will be great - thanks.
I'm no expert, but it seemed like a namespace issue. The solution I employed was this:
;; for REPL
user=> (ns user (:use clojure.contrib.math))
nil
user=> (sqrt 2)
1.4142135623730951
You should put refer after require which will map all the symbols into current namespace
(require 'clojure.contrib.math)
(refer 'clojure.contrib.math)
(sqrt 2)
(expt 2 3)
For detailed intro you may read wikibooks article.
http://en.wikibooks.org/wiki/Clojure_Programming/Concepts#Refer_and_Use