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())
//json format : {username:xxx}
if(json.getString("name") != null){
e.client.data["IsLogged"] = true;
e.client.data["Username"] = json.getString("name");
json = JSONObject(); if(json.has("close")){
json.put("success",true); if(json.getBoolean("close")){
json.put("error",""); e.client.close("Server closing");
e.client.send(buildTextMessage(json.toString())); 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(); val array = JSONArray();
synchronized(this.lock,{ array.put(JSONArray(listOf<Any?>(e.client.data["Username"],json.getString("message"))))
this.messages.forEach {
array.put(JSONArray(listOf(it.first.data["Username"],it.second))) 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{ }else{
//tried to access chat without logging in System.out.println("unknown JSON: "+json.toString());
json = JSONObject("");
json.put("success",false);
json.put("error","You must send your credential before sending anything else");
e.client.send(buildTextMessage(json.toString()));
} }
}catch (e : Exception){
return; //System.out.println("Something went wrong (probably JSON parsing error");
} e.printStackTrace();
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<Any?>(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());
} }
} }
} }

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){
if (this.isEmpty()) { //BEWARE this stack is programmed to empty itself automatically if you push an item when the stack is full
return null if(this.items.count() == this.size ){
} else { this.pull();
val item = this.items.count() - 1
return this.items.removeAt(item)
} }
this.items.add(element)
} }
fun peek():T? { fun pull():T?{
if (isEmpty()) { if (this.isEmpty()){
return null return null
} else { } else {
return this.items[this.items.count() - 1] return this.items.removeAt(0)
} }
} }
@ -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")
} }