From b141c304efde5f5982bf99e3de4ef922334e583f Mon Sep 17 00:00:00 2001 From: SeekDaSky Date: Tue, 5 Dec 2017 21:19:18 +0100 Subject: [PATCH] =?UTF-8?q?stabilit=C3=A9=20et=20optimisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Channel.kt | 125 ++++++++++++++++++++++----------------- src/ChannelDispatcher.kt | 2 +- src/Stack.kt | 35 ++++------- 3 files changed, 84 insertions(+), 78 deletions(-) diff --git a/src/Channel.kt b/src/Channel.kt index f856630..64646f4 100644 --- a/src/Channel.kt +++ b/src/Channel.kt @@ -1,21 +1,25 @@ +import kotlinx.coroutines.experimental.async import org.json.JSONArray import org.json.JSONObject import seekdasky.kWebSocket.Client import seekdasky.kWebSocket.Event import seekdasky.kWebSocket.Listeners.AsynchronousListener import seekdasky.kWebSocket.Message.*; +import seekdasky.kWebSocket.Server +import kotlin.test.currentStackTrace class Channel : AsynchronousListener { val channelName : String; var clients : MutableList; val messages : Stack>; - var lock = false; + val serv : Server; - constructor(chanName : String){ + constructor(serv : Server , chanName : String){ this.channelName = chanName; this.clients = mutableListOf(); - this.messages = Stack(mutableListOf()); + this.messages = Stack(20); + this.serv = serv; } override fun filter(c: Client): Boolean { @@ -23,8 +27,8 @@ class Channel : AsynchronousListener { } override fun processClosed(e: Event) { - System.out.println("Client left the channel: "+this.channelName) this.clients.remove(e.client); + System.out.println("A client disconnected ("+this.clients.count()+" clients connected"); } override fun processConnection(c: Client) { @@ -33,63 +37,74 @@ class Channel : AsynchronousListener { } override fun processEvent(e: Event) { - var json = JSONObject(e.message.getString()) - if(e.client.data["IsLogged"] != true){ - //json format : {username:xxx} - if(json.getString("name") != null){ - e.client.data["IsLogged"] = true; - e.client.data["Username"] = json.getString("name"); + try{ + val json = JSONObject(e.message.getString()) - json = JSONObject(); - json.put("success",true); - json.put("error",""); - e.client.send(buildTextMessage(json.toString())); + if(json.has("close")){ + if(json.getBoolean("close")){ + e.client.close("Server closing"); + this.processClosed(e); + } + }else if(e.client.data["IsLogged"] != true){ + //json format : {username:xxx} + if(json.getString("name") != null){ + e.client.data["IsLogged"] = true; + e.client.data["Username"] = json.getString("name"); + + val jsonLogin = JSONObject(); + jsonLogin.put("error",false); + e.client.send(buildTextMessage(jsonLogin.toString())); + + + val array = JSONArray(); + synchronized(this.messages,{ + this.messages.forEach { + array.put(JSONArray(listOf(it.first.data["Username"],it.second))) + } + }) + + val jsonMessage = JSONObject(); + jsonMessage.put("error",false); + jsonMessage.put("msg",array); + + e.client.send(buildTextMessage(jsonMessage.toString())); + + + System.out.println("A client connected ("+this.clients.count()+" clients connected)"); + + }else{ + //tried to access chat without logging in + val jsonError = JSONObject(""); + jsonError.put("error",true); + jsonError.put("error","You must send your credential before sending anything else"); + + e.client.send(buildTextMessage(jsonError.toString())); + + } + + return; + }else if(json.has("message")){ + synchronized(this.messages,{ + this.messages.push(Pair(e.client,json.getString("message"))) + }); val array = JSONArray(); - synchronized(this.lock,{ - this.messages.forEach { - array.put(JSONArray(listOf(it.first.data["Username"],it.second))) + array.put(JSONArray(listOf(e.client.data["Username"],json.getString("message")))) + + val jsonMessage = JSONObject(); + jsonMessage.put("error",false); + jsonMessage.put("msg",array); + for(c in this.clients){ + if(c != e.client && c.data["IsLogged"] == true){ + c.send(buildTextMessage(jsonMessage.toString())); } - }) - - json = JSONObject(); - json.put("error",false); - json.put("msg",array); - e.client.send(buildTextMessage(json.toString())); - + } }else{ - //tried to access chat without logging in - json = JSONObject(""); - json.put("success",false); - json.put("error","You must send your credential before sending anything else"); - e.client.send(buildTextMessage(json.toString())); + System.out.println("unknown JSON: "+json.toString()); } - - return; - } - - if(json.has("message")){ - synchronized(this.lock,{ - this.lock = true; - this.messages.push(Pair(e.client,json.getString("message"))) - if(this.messages.count() > 20){ - this.messages.pop(); - } - }); - - val array = JSONArray(); - array.put(JSONArray(listOf(e.client.data["Username"],json.getString("message")))) - - json = JSONObject(); - json.put("error",false); - json.put("msg",array); - for(c in this.clients){ - if(c != e.client && c.data["IsLogged"] == true){ - c.send(buildTextMessage(json.toString())); - } - } - }else{ - System.out.println("unknown JSON: "+json.toString()); + }catch (e : Exception){ + //System.out.println("Something went wrong (probably JSON parsing error"); + e.printStackTrace(); } } } \ No newline at end of file diff --git a/src/ChannelDispatcher.kt b/src/ChannelDispatcher.kt index d48d915..667729b 100644 --- a/src/ChannelDispatcher.kt +++ b/src/ChannelDispatcher.kt @@ -30,7 +30,7 @@ class ChannelDispatcher : AsynchronousListener{ override fun processConnection(c: Client) { System.out.println("new channel created: "+c.URL); //if a client is handled here it means we have no channel at this address, let's create it - var newChan = Channel(c.URL); + var newChan = Channel(this.serv,c.URL); this.serv.addListener(newChan); //we transfer the client to the new channel newChan.processConnection(c); diff --git a/src/Stack.kt b/src/Stack.kt index e30d60b..ca1471e 100644 --- a/src/Stack.kt +++ b/src/Stack.kt @@ -1,35 +1,28 @@ -class Stack(list:MutableList):Iterator { - - var itCounter: Int = 0 - - var items: MutableList = list +class Stack(size : Int):Iterator { + var itCounter = 0; + val size = size; + var items:MutableList = mutableListOf() fun isEmpty():Boolean = this.items.isEmpty() fun count():Int = this.items.count() - fun push(element:T) { - val position = this.count() - this.items.add(position, element) - } - override fun toString() = this.items.toString() - fun pop():T? { - if (this.isEmpty()) { - return null - } else { - val item = this.items.count() - 1 - return this.items.removeAt(item) + fun push(element: T){ + //BEWARE this stack is programmed to empty itself automatically if you push an item when the stack is full + if(this.items.count() == this.size ){ + this.pull(); } + this.items.add(element) } - fun peek():T? { - if (isEmpty()) { + fun pull():T?{ + if (this.isEmpty()){ return null } else { - return this.items[this.items.count() - 1] + return this.items.removeAt(0) } } @@ -44,9 +37,7 @@ class Stack(list:MutableList):Iterator { override fun next(): T { if (hasNext()) { - val topPos: Int = (count() - 1) - itCounter - itCounter++ - return this.items[topPos] + return this.items[this.itCounter++] } else { throw NoSuchElementException("No such element") }