stabilité et optimisation

This commit is contained in:
SeekDaSky 2017-12-05 21:19:18 +01:00
parent 8c959ec818
commit b141c304ef
3 changed files with 84 additions and 78 deletions

View File

@ -1,21 +1,25 @@
import kotlinx.coroutines.experimental.async
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import seekdasky.kWebSocket.Client import seekdasky.kWebSocket.Client
import seekdasky.kWebSocket.Event import seekdasky.kWebSocket.Event
import seekdasky.kWebSocket.Listeners.AsynchronousListener import seekdasky.kWebSocket.Listeners.AsynchronousListener
import seekdasky.kWebSocket.Message.*; import seekdasky.kWebSocket.Message.*;
import seekdasky.kWebSocket.Server
import kotlin.test.currentStackTrace
class Channel : AsynchronousListener { class Channel : AsynchronousListener {
val channelName : String; val channelName : String;
var clients : MutableList<Client>; var clients : MutableList<Client>;
val messages : Stack<Pair<Client,String>>; val messages : Stack<Pair<Client,String>>;
var lock = false; val serv : Server;
constructor(chanName : String){ constructor(serv : Server , chanName : String){
this.channelName = chanName; this.channelName = chanName;
this.clients = mutableListOf(); this.clients = mutableListOf();
this.messages = Stack(mutableListOf()); this.messages = Stack(20);
this.serv = serv;
} }
override fun filter(c: Client): Boolean { override fun filter(c: Client): Boolean {
@ -23,8 +27,8 @@ class Channel : AsynchronousListener {
} }
override fun processClosed(e: Event) { override fun processClosed(e: Event) {
System.out.println("Client left the channel: "+this.channelName)
this.clients.remove(e.client); this.clients.remove(e.client);
System.out.println("A client disconnected ("+this.clients.count()+" clients connected");
} }
override fun processConnection(c: Client) { override fun processConnection(c: Client) {
@ -33,63 +37,74 @@ class Channel : AsynchronousListener {
} }
override fun processEvent(e: Event) { override fun processEvent(e: Event) {
var json = JSONObject(e.message.getString()) try{
if(e.client.data["IsLogged"] != true){ val json = JSONObject(e.message.getString())
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} //json format : {username:xxx}
if(json.getString("name") != null){ if(json.getString("name") != null){
e.client.data["IsLogged"] = true; e.client.data["IsLogged"] = true;
e.client.data["Username"] = json.getString("name"); e.client.data["Username"] = json.getString("name");
json = JSONObject(); val jsonLogin = JSONObject();
json.put("success",true); jsonLogin.put("error",false);
json.put("error",""); e.client.send(buildTextMessage(jsonLogin.toString()));
e.client.send(buildTextMessage(json.toString()));
val array = JSONArray(); val array = JSONArray();
synchronized(this.lock,{ synchronized(this.messages,{
this.messages.forEach { this.messages.forEach {
array.put(JSONArray(listOf(it.first.data["Username"],it.second))) array.put(JSONArray(listOf(it.first.data["Username"],it.second)))
} }
}) })
json = JSONObject(); val jsonMessage = JSONObject();
json.put("error",false); jsonMessage.put("error",false);
json.put("msg",array); jsonMessage.put("msg",array);
e.client.send(buildTextMessage(json.toString()));
e.client.send(buildTextMessage(jsonMessage.toString()));
System.out.println("A client connected ("+this.clients.count()+" clients connected)");
}else{ }else{
//tried to access chat without logging in //tried to access chat without logging in
json = JSONObject(""); val jsonError = JSONObject("");
json.put("success",false); jsonError.put("error",true);
json.put("error","You must send your credential before sending anything else"); jsonError.put("error","You must send your credential before sending anything else");
e.client.send(buildTextMessage(json.toString()));
e.client.send(buildTextMessage(jsonError.toString()));
} }
return; return;
} }else if(json.has("message")){
synchronized(this.messages,{
if(json.has("message")){
synchronized(this.lock,{
this.lock = true;
this.messages.push(Pair(e.client,json.getString("message"))) this.messages.push(Pair(e.client,json.getString("message")))
if(this.messages.count() > 20){
this.messages.pop();
}
}); });
val array = JSONArray(); val array = JSONArray();
array.put(JSONArray(listOf<Any?>(e.client.data["Username"],json.getString("message")))) array.put(JSONArray(listOf<Any?>(e.client.data["Username"],json.getString("message"))))
json = JSONObject(); val jsonMessage = JSONObject();
json.put("error",false); jsonMessage.put("error",false);
json.put("msg",array); jsonMessage.put("msg",array);
for(c in this.clients){ for(c in this.clients){
if(c != e.client && c.data["IsLogged"] == true){ if(c != e.client && c.data["IsLogged"] == true){
c.send(buildTextMessage(json.toString())); c.send(buildTextMessage(jsonMessage.toString()));
} }
} }
}else{ }else{
System.out.println("unknown JSON: "+json.toString()); System.out.println("unknown JSON: "+json.toString());
} }
}catch (e : Exception){
//System.out.println("Something went wrong (probably JSON parsing error");
e.printStackTrace();
}
} }
} }

View File

@ -30,7 +30,7 @@ class ChannelDispatcher : AsynchronousListener{
override fun processConnection(c: Client) { override fun processConnection(c: Client) {
System.out.println("new channel created: "+c.URL); 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 //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); this.serv.addListener(newChan);
//we transfer the client to the new channel //we transfer the client to the new channel
newChan.processConnection(c); newChan.processConnection(c);

View File

@ -1,35 +1,28 @@
class Stack<T>(list:MutableList<T>):Iterator<T> { class Stack<T>(size : Int):Iterator<T> {
var itCounter: Int = 0
var items: MutableList<T> = list
var itCounter = 0;
val size = size;
var items:MutableList<T> = mutableListOf()
fun isEmpty():Boolean = this.items.isEmpty() fun isEmpty():Boolean = this.items.isEmpty()
fun count():Int = this.items.count() 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() override fun toString() = this.items.toString()
fun pop():T? { 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 pull():T?{
if (this.isEmpty()){ if (this.isEmpty()){
return null return null
} else { } else {
val item = this.items.count() - 1 return this.items.removeAt(0)
return this.items.removeAt(item)
}
}
fun peek():T? {
if (isEmpty()) {
return null
} else {
return this.items[this.items.count() - 1]
} }
} }
@ -44,9 +37,7 @@ class Stack<T>(list:MutableList<T>):Iterator<T> {
override fun next(): T { override fun next(): T {
if (hasNext()) { if (hasNext()) {
val topPos: Int = (count() - 1) - itCounter return this.items[this.itCounter++]
itCounter++
return this.items[topPos]
} else { } else {
throw NoSuchElementException("No such element") throw NoSuchElementException("No such element")
} }