Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created March 22, 2012 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shouichi/2155244 to your computer and use it in GitHub Desktop.
Save shouichi/2155244 to your computer and use it in GitHub Desktop.
Run "make" to run on normal JVM, "make jpf" to run on JPF.
Please edit Makefile and site.properties fiels and set correct paths of JPF and net-iocache.
RM=rm -f
JAVAC=javac
JAVAC_FLAGS=-g
JAVA=java
JAVA_FLAGS=-ea
JPF=~/jpf/jpf-core/bin/jpf
SOURCES=ServerSocketChannel.java
CLASSES=$(SOURCES:.java=.class)
CONF_FILE=$(SOURCES:.java=.jpf)
all: run
run: $(CLASSES)
$(JAVA) $(JAVA_FLAGS) `basename $< .class`
jpf: $(CLASSES) $(CONF_FILE)
$(JPF) $(CONF_FILE)
.SUFFIXES:
.SUFFIXES: .java .class
.java.class:
$(JAVAC) $(JAFAC_FLAG) $<
.PHONY: clean
clean:
$(RM) *.class
#!/bin/sh
curl localhost:8888
import java.util.*;
import java.net.*;
public class ServerSocketChannel {
private ServerSocket ss;
private Queue<Socket> queue;
private AcceptorThread acceptor;
public static void main(String[] args) throws Exception {
ServerSocketChannel ssc = new ServerSocketChannel();
ssc.socket().bind(new InetSocketAddress(8888));
ssc.socket().setReuseAddress(true);
ssc.accept();
}
public ServerSocketChannel() throws java.io.IOException {
ss = new ServerSocket();
queue = new LinkedList<Socket>();
acceptor = new AcceptorThread();
acceptor.setDaemon(true);
acceptor.start();
}
public ServerSocket socket() {
return ss;
}
public Socket accept() {
synchronized (queue) {
return queue.poll();
}
}
class AcceptorThread extends Thread {
@Override
public void run() {
while (true) {
synchronized (ss) {
while (ss.isBound() == false) {
try {
ss.wait(1000);
} catch (InterruptedException e) {}
}
}
try {
synchronized (queue) {
while (queue.size() != 0) {
try {
queue.wait(100);
} catch (InterruptedException e) {}
}
}
Socket socket = ss.accept();
synchronized (queue) {
queue.add(socket);
}
}
catch (Exception e) {
System.exit(1);
}
}
}
}
}
ssc=.
ssc.classpath=.
ssc.sourcepath=.
site=site.properties
target=ServerSocketChannel
jpf-net-iocache.boot.peer.command=./peer.sh
jpf-core = ${user.home}/jpf/jpf-core
jpf-net-iocache = ${user.home}/jpf/net-iocache
extensions = ${jpf-core},${jpf-net-iocache}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment