package chat
import java.nio.channels.ServerSocketChannel
import java.nio.channels.Selector
import java.net.InetSocketAddress
import java.nio.channels.SelectionKey
import java.nio.channels.SocketChannel
import java.util.LinkedList
import java.nio.ByteBuffer
/**
* @author Shine
*/
object server extends App {
def startListenerAccept(serScCh: Selector) {
println("Starting......")
while(true) {
if (serScCh.select() > 0) {
val keys = serScCh.selectedKeys().iterator();
while(keys.hasNext()) {
val key = keys.next()
keys.remove()
if (key.isAcceptable()) {
addToChatMng(key)
if (!msgThreadStarted) {
msgThreadStarted = true
startMsgListen()
}
}
}
}
}
}
def startMsgListen() {
new Thread {
override def run {
while(true) {
if (msgSelector.select() > 0) {
val rkeys = msgSelector.selectedKeys().iterator()
while(rkeys.hasNext()) {
val rkey = rkeys.next()
rkeys.remove()
if (rkey.isReadable()) {
readAndBroadcastToOther(rkey.channel().asInstanceOf[SocketChannel])
}
}
}
}
}
}.start()
}
def addToChatMng(key: SelectionKey) {
val ch = key.channel().asInstanceOf[ServerSocketChannel].accept()
ch.configureBlocking(false)
msgChs.add(ch)
sendMsg(ch, welcomStr)
ch.register(msgSelector, SelectionKey.OP_READ)
}
def readAndBroadcastToOther(ch: SocketChannel) {
val myMsg = readMsg(ch);
msgChs.toArray().foreach { x => if (x != ch) sendMsg(x.asInstanceOf[SocketChannel], myMsg) }
}
val readBuf = ByteBuffer.allocate(1024)
def readMsg(ch: SocketChannel) = {
val strBd = new StringBuilder
var i = ch.read(readBuf)
while(i > 0) {
strBd.append(new String(readBuf.array(), 0 , i));
readBuf.clear()
i = ch.read(readBuf)
}
strBd.toString()
}
def sendMsg(ch: SocketChannel, msg: String) {
ch.write(ByteBuffer.wrap(msg.getBytes));
}
var msgThreadStarted = false
val welcomStr = "Welcom to Simple Chat"
val msgSelector = Selector.open()
val msgChs = new LinkedList[SocketChannel]()
val serScCh = ServerSocketChannel.open
serScCh.socket().bind(new InetSocketAddress("127.0.0.1", 888))
serScCh.configureBlocking(false)
val acceptSelector = Selector.open
serScCh.register(acceptSelector, SelectionKey.OP_ACCEPT)
startListenerAccept(acceptSelector)
}
package chat
import java.nio.ByteBuffer
import java.nio.channels.SocketChannel
import java.net.Socket
import java.net.InetSocketAddress
import java.io.BufferedReader
import java.io.InputStreamReader
import java.nio.channels.Selector
import java.nio.channels.SelectionKey
/**
* @author Shine
*/
object client extends App {
val readBuf = ByteBuffer.allocate(1024)
def readMsg(ch: SocketChannel) = {
val strBd = new StringBuilder
var i = ch.read(readBuf)
while(i > 0) {
strBd.append(new String(readBuf.array(), 0 , i));
readBuf.clear()
i = ch.read(readBuf)
}
strBd.toString()
}
def sendMsg(ch: SocketChannel, msg: String) {
ch.write(ByteBuffer.wrap(msg.getBytes));
}
val msgSelector = Selector.open()
def startListenerThread(ch: SocketChannel) {
ch.register(msgSelector, SelectionKey.OP_READ)
new Thread {
override def run() {
while(true) {
if (msgSelector.select() > 0) {
val keys = msgSelector.selectedKeys().iterator()
while(keys.hasNext()) {
val key = keys.next()
keys.remove()
if (key.isReadable()) {
println(readMsg(key.channel().asInstanceOf[SocketChannel]))
print(">")
}
}
}
}
}
}.start()
}
val sc = SocketChannel.open()
sc.connect(new InetSocketAddress("127.0.0.1", 888))
sc.configureBlocking(false)
startListenerThread(sc)
while(true) {
print(">")
val bufR = new BufferedReader(new InputStreamReader(System.in))
sendMsg(sc, bufR.readLine())
}
}