Embedding swank-clojure

15 views
Skip to first unread message

HiHeelHottie

unread,
Apr 17, 2010, 11:27:47 PM4/17/10
to Clojure

Based on the Embedding section of http://github.com/technomancy/swank-clojure,
I'm using the following to test it out. Is there a better way to do
this that doesn't use Compiler? Is there a way to programmatically
stop swank? It seems start-repl takes control of the thread. What
would be a good way to spawn off another thread for it and be able to
kill that thread programatically.

import clojure.lang.Compiler;
import java.io.StringReader;

public class Embed {
public static void main(String[] args) throws Exception {
final String startSwankScript =
"(ns my-app\n" +
" (:use [swank.swank :as swank]))\n" +
"(swank/start-repl) ";
Compiler.load(new StringReader(startSwankScript));
}
}

Any help much appreciated,

hhh

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

alux

unread,
Apr 19, 2010, 8:54:22 AM4/19/10
to Clojure
Hello HiHeelHottie!

Summary: Dirty code ahead, that doesnt even really work.

What you do in your code starts a new thread (well, some threads
actually). You will see this if you put a System.out.println("bing")
past your line containing Compiler...

The problem seems to be, swank doesn't come with a stop-repl method.
So what can we do, if we see it as a black box.?

A possible way is to brutally kill swank from Java. But even this isnt
easy, because of some blocking. In the following code I make swank
start in a separate ThreadGroup, so I can go and kill it easier. I
tried to kept it simple, so instead of creating a button to kill it,
or so, I just wait a minute, and kill it then.

Nevertheless this way doesn't really work because of some blocking.
The kill will only work out if I make Emacs send something (after the
kill started) to unblock the the socket (at least thats what I think
is the reason). I.e. hit enter if connected, or try to connect.

Certainly there should be a better way, but may be you can start from
here.

Regards, alux



public static void main(String[] args) throws InterruptedException{
ThreadGroup tg = new ThreadGroup("Swank ThreadGroup");
Runnable swankStart = new Runnable(){
public void run(){
try{
final String startSwankScript =
"(ns my-app\n" +
" (:use [swank.swank :as swank]))\n" +
"(swank/start-repl) ";
System.out.println("Starting swank.");
clojure.lang.Compiler.load(new
StringReader(startSwankScript));
System.out.println("Started.");
printAllThreads();
}catch(Exception ex){
ex.printStackTrace();
}
}
};
Thread t = new Thread(tg, swankStart, "Swank Starter");
t.start();

printAllThreads();
System.out.println("Main thread sleeps a bit.");
Thread.sleep(60000);//old thread sleeps a while

System.out.println("Kill!");
printAllThreads();

//Bad bad code. Don't show it to Rich Hickey.
while(tg.activeCount()>0){
System.out.println("Kill.");
killEm(tg);
Thread.sleep(2000);
}

printAllThreads();

try{
tg.destroy();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
Thread.sleep(1000);
printAllThreads();
}

static void killEm(ThreadGroup tg){
Thread[] threads = new Thread[tg.activeCount()+50];
int threadCount = tg.enumerate(threads, true);
if(threadCount >= threads.length){
System.out.println("to much traffic, will not work properly.");
}
for (int i = 0; i < threadCount; i++) {
System.out.println("Sending kill signal to thread:
"+threads[i].getName());
threads[i].interrupt();
threads[i].stop();
}
}


static void printAllThreads(){
ThreadGroup tg = Thread.currentThread().getThreadGroup();
ThreadGroup parent = tg.getParent();
while(parent != null){
tg = parent;
parent = tg.getParent();
}
Thread[] threads = new Thread[tg.activeCount()+50];
int threadCount = tg.enumerate(threads, true);
if(threadCount < threads.length){
for (int i = 0; i < threadCount; i++) {
System.out.println(threads[i].getName()+" @
"+threads[i].getThreadGroup().getName());
}
}
System.out.println();
}




On 18 Apr., 05:27, HiHeelHottie <hiheelhot...@gmail.com> wrote:
> Based on the Embedding section ofhttp://github.com/technomancy/swank-clojure,
Reply all
Reply to author
Forward
0 new messages