This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved NIO stuff into abstractions for server and connection.
- Loading branch information
1 parent
72ecb6e
commit 58b36d5
Showing
10 changed files
with
153 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package dnode; | ||
|
||
public interface Callback { | ||
void call(Object... args) throws RuntimeException; | ||
void call(Object... args) throws RuntimeException; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dnode; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Connection { | ||
void send(String data); | ||
|
||
String read() throws IOException; | ||
|
||
void close() throws IOException; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dnode; | ||
|
||
public interface ConnectionHandler { | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dnode; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Server { | ||
void listen(DNode dnode) throws IOException; | ||
|
||
void shutdown() throws IOException; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dnode.nio; | ||
|
||
import dnode.Connection; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.channels.SocketChannel; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.CharsetDecoder; | ||
import java.nio.charset.CharsetEncoder; | ||
|
||
public class NIOConnection implements Connection { | ||
private static Charset charset = Charset.forName("UTF-8"); | ||
private static CharsetEncoder encoder = charset.newEncoder(); | ||
private static CharsetDecoder decoder = charset.newDecoder(); | ||
|
||
private final SocketChannel channel; | ||
|
||
public NIOConnection(SocketChannel channel) { | ||
this.channel = channel; | ||
} | ||
|
||
public void send(String data) { | ||
try { | ||
channel.write(encoder.encode(CharBuffer.wrap(data + "\r\n"))); | ||
} catch (IOException e) { | ||
try { | ||
close(); | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | ||
} | ||
} | ||
} | ||
|
||
public String read() throws IOException { | ||
ByteBuffer bb = ByteBuffer.allocate(1024); | ||
channel.read(bb); | ||
bb.flip(); | ||
CharBuffer response = decoder.decode(bb); | ||
return response.toString(); | ||
} | ||
|
||
public void close() throws IOException { | ||
channel.close(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dnode.nio; | ||
|
||
import dnode.Connection; | ||
import dnode.DNode; | ||
import dnode.Server; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.nio.channels.ServerSocketChannel; | ||
import java.nio.channels.SocketChannel; | ||
|
||
public class NIOServer implements Server { | ||
private ServerSocketChannel ssc; | ||
private final int port; | ||
|
||
public NIOServer(int port) { | ||
this.port = port; | ||
} | ||
|
||
public void listen(DNode dnode) throws IOException { | ||
ssc = ServerSocketChannel.open(); | ||
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port); | ||
ssc.socket().bind(isa); | ||
dnode.emit("ready"); | ||
SocketChannel channel = ssc.accept(); | ||
|
||
Connection conn = new NIOConnection(channel); | ||
dnode.handle(conn); | ||
} | ||
|
||
public void shutdown() throws IOException { | ||
ssc.close(); | ||
} | ||
|
||
} |
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
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