Created
April 3, 2012 19:01
-
-
Save shouichi/2294730 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
This gist is for verifying https://github.com/ok2c/lightnio on JPF. | |
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. |
This file contains hidden or 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
import java.io.*; | |
import java.net.*; | |
public class Client { | |
public static void main(String[] args) throws Exception { | |
Socket socket = new Socket("127.0.0.1", 8080); | |
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
out.println("abcdefg"); | |
in.readLine(); | |
out.close(); | |
in.close(); | |
socket.close(); | |
} | |
} |
This file contains hidden or 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
/* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
import java.io.IOException; | |
import java.io.InterruptedIOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import com.ok2c.lightnio.EventMask; | |
import com.ok2c.lightnio.IOEventDispatch; | |
import com.ok2c.lightnio.IOSession; | |
import com.ok2c.lightnio.ListeningIOReactor; | |
import com.ok2c.lightnio.impl.DefaultListeningIOReactor; | |
import com.ok2c.lightnio.impl.IOReactorConfig; | |
public class EchoServer { | |
public static void main(String[] args) throws Exception { | |
IOEventDispatch ioEventDispatch = new DefaultIoEventDispatch(); | |
IOReactorConfig config = new IOReactorConfig(); | |
config.setWorkerCount(2); | |
ListeningIOReactor ioReactor = new DefaultListeningIOReactor(config); | |
ioReactor.listen(new InetSocketAddress(8080)); | |
try { | |
ioReactor.execute(ioEventDispatch); | |
} catch (InterruptedIOException ex) { | |
System.err.println("Interrupted"); | |
} catch (IOException e) { | |
System.err.println("I/O error: " + e.getMessage()); | |
} | |
System.out.println("Shutdown"); | |
} | |
static class DefaultIoEventDispatch implements IOEventDispatch { | |
private final ByteBuffer buffer = ByteBuffer.allocate(1024); | |
public void connected(IOSession session) { | |
System.out.println("connected"); | |
session.setEventMask(EventMask.READ); | |
session.setSocketTimeout(20000); | |
} | |
public void inputReady(final IOSession session) { | |
System.out.println("readable"); | |
try { | |
this.buffer.compact(); | |
int bytesRead = session.channel().read(this.buffer); | |
if (this.buffer.position() > 0) { | |
session.setEventMask(EventMask.READ_WRITE); | |
} | |
System.out.println("Bytes read: " + bytesRead); | |
if (bytesRead == -1) { | |
session.close(); | |
} | |
} catch (IOException ex) { | |
System.err.println("I/O error: " + ex.getMessage()); | |
} | |
} | |
public void outputReady(final IOSession session) { | |
System.out.println("writeable"); | |
try { | |
this.buffer.flip(); | |
int bytesWritten = session.channel().write(this.buffer); | |
if (!this.buffer.hasRemaining()) { | |
session.setEventMask(EventMask.READ); | |
} | |
System.out.println("Bytes written: " + bytesWritten); | |
} catch (IOException ex) { | |
System.err.println("I/O error: " + ex.getMessage()); | |
} | |
} | |
public void timeout(final IOSession session) { | |
System.out.println("timeout"); | |
session.close(); | |
} | |
public void disconnected(final IOSession session) { | |
System.out.println("disconnected"); | |
} | |
} | |
} |
This file contains hidden or 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
ssc=. | |
ssc.classpath=lightnio.jar:. | |
ssc.sourcepath=. | |
site=site.properties | |
target=EchoServer | |
jpf-net-iocache.boot.peer.command=./peer.sh |
This file contains hidden or 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
RM=rm -f | |
JAVAC=javac | |
JAVAC_FLAGS=-g | |
JAVA=java | |
JAVA_FLAGS=-ea | |
CLASSPATH=lightnio.jar:. | |
JPF=~/jpf/jpf-core/bin/jpf | |
MAIN=EchoServer.java | |
SOURCES=$(MAIN) Client.java | |
CLASSES=$(SOURCES:.java=.class) | |
CONF_FILE=$(MAIN:.java=.jpf) | |
all: run | |
run: $(CLASSES) | |
$(JAVA) $(JAVA_FLAGS) -cp $(CLASSPATH) `basename $< .class` | |
jpf: $(CLASSES) $(CONF_FILE) | |
$(JPF) $(CONF_FILE) | |
.SUFFIXES: | |
.SUFFIXES: .java .class | |
.java.class: | |
$(JAVAC) $(JAFAC_FLAG) -cp $(CLASSPATH) $< | |
.PHONY: clean | |
clean: | |
$(RM) *.class |
This file contains hidden or 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
#!/bin/sh | |
java Client |
This file contains hidden or 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
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