stabilité et optimisation
This commit is contained in:
parent
8c959ec818
commit
b141c304ef
125
src/Channel.kt
125
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<Client>;
|
||||
val messages : Stack<Pair<Client,String>>;
|
||||
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<Any?>(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<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());
|
||||
}catch (e : Exception){
|
||||
//System.out.println("Something went wrong (probably JSON parsing error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
35
src/Stack.kt
35
src/Stack.kt
|
@ -1,35 +1,28 @@
|
|||
class Stack<T>(list:MutableList<T>):Iterator<T> {
|
||||
|
||||
var itCounter: Int = 0
|
||||
|
||||
var items: MutableList<T> = list
|
||||
class Stack<T>(size : Int):Iterator<T> {
|
||||
|
||||
var itCounter = 0;
|
||||
val size = size;
|
||||
var items:MutableList<T> = 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<T>(list:MutableList<T>):Iterator<T> {
|
|||
|
||||
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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue