fix: concurrence et interop
This commit is contained in:
parent
77a15f9958
commit
8ba61305cc
|
@ -28,7 +28,8 @@ class Channel : AsynchronousListener {
|
|||
|
||||
override fun processClosed(e: Event) {
|
||||
this.clients.remove(e.client);
|
||||
System.out.println("A client disconnected ("+this.clients.count()+" clients connected");
|
||||
ConnectChannel.notifyDisconnect(e.client);
|
||||
System.out.println("A client disconnected ("+this.clients.count()+" clients connected)");
|
||||
}
|
||||
|
||||
override fun processConnection(c: Client) {
|
||||
|
@ -40,13 +41,18 @@ class Channel : AsynchronousListener {
|
|||
try{
|
||||
val json = JSONObject(e.message.getString())
|
||||
|
||||
if(json.has("close")){
|
||||
return
|
||||
}
|
||||
|
||||
if(e.client.data["IsLogged"] != true){
|
||||
//json format : {username:xxx}
|
||||
if(json.getString("name") != null){
|
||||
e.client.data["IsLogged"] = true;
|
||||
if(json.has("name") && json.getString("name") != null){
|
||||
e.client.data["Username"] = json.getString("name");
|
||||
|
||||
if(ConnectChannel.isConnected(e.client)){
|
||||
System.out.println("A client connected ("+this.clients.count()+" clients connected)");
|
||||
e.client.data["IsLogged"] = true;
|
||||
val jsonLogin = JSONObject();
|
||||
jsonLogin.put("error",false);
|
||||
e.client.send(buildTextMessage(jsonLogin.toString()));
|
||||
|
@ -54,7 +60,7 @@ class Channel : AsynchronousListener {
|
|||
|
||||
val array = JSONArray();
|
||||
synchronized(this.messages,{
|
||||
this.messages.forEach {
|
||||
this.messages.toList().forEach {
|
||||
array.put(JSONArray(listOf(it.first.data["Username"],it.second)))
|
||||
}
|
||||
})
|
||||
|
@ -73,7 +79,7 @@ class Channel : AsynchronousListener {
|
|||
|
||||
}else{
|
||||
//tried to access chat without logging in
|
||||
val jsonError = JSONObject("");
|
||||
val jsonError = JSONObject();
|
||||
jsonError.put("error","You must send your credential before sending anything else");
|
||||
|
||||
e.client.send(buildTextMessage(jsonError.toString()));
|
||||
|
|
|
@ -7,10 +7,10 @@ import seekdasky.kWebSocket.Message.InteropMessage
|
|||
|
||||
object ConnectChannel : InteropListener {
|
||||
|
||||
var lastGuestIndex = 0;
|
||||
private var lastGuestIndex = 0;
|
||||
|
||||
val notBoundYet = Stack<Pair<String,String>>(50);
|
||||
val connected = mutableMapOf<Client, String>();
|
||||
private val notBoundYet = Stack<Pair<String,String>>(500);
|
||||
private val connected = mutableMapOf<Client, String>();
|
||||
|
||||
|
||||
override fun filter(e: InteropEvent): Boolean {
|
||||
|
@ -27,7 +27,7 @@ object ConnectChannel : InteropListener {
|
|||
val json = JSONObject(e.message.string);
|
||||
|
||||
if(json.getString("type") == "guest"){
|
||||
this.lastGuestIndex = ++this.lastGuestIndex % 10;
|
||||
this.lastGuestIndex = ++this.lastGuestIndex % 500;
|
||||
val guestName = "Guest"+this.lastGuestIndex;
|
||||
val jsonReturn = JSONObject();
|
||||
jsonReturn.put("error", false);
|
||||
|
@ -53,17 +53,28 @@ object ConnectChannel : InteropListener {
|
|||
}
|
||||
|
||||
fun isConnected(client : Client) : Boolean{
|
||||
if(!this.connected.containsKey(client)){
|
||||
for(p in this.notBoundYet) {
|
||||
if (p.second == client.data.get("Username")) {
|
||||
for(c in this.connected.keys){
|
||||
if(c.data["Username"] == client.data["Username"]){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for(p in this.notBoundYet.toList()) {
|
||||
if (p.second == client.data["Username"]) {
|
||||
this.connected.put(client, p.first);
|
||||
this.notBoundYet.remove(p);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
|
||||
fun notifyDisconnect(client : Client){
|
||||
val clientType = this.connected[client];
|
||||
if(clientType != null){
|
||||
this.notBoundYet.push(Pair(clientType,client.data["Username"].toString()));
|
||||
this.connected.remove(client);
|
||||
}
|
||||
}
|
||||
}
|
19
src/Stack.kt
19
src/Stack.kt
|
@ -1,4 +1,4 @@
|
|||
class Stack<T>(size : Int):Iterator<T> {
|
||||
class Stack<T>(size : Int){
|
||||
|
||||
var itCounter = 0;
|
||||
val size = size;
|
||||
|
@ -30,21 +30,8 @@ class Stack<T>(size : Int):Iterator<T> {
|
|||
this.items.remove(item);
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
val hasNext = itCounter < count()
|
||||
|
||||
// As soon as condition fails, reset the counter
|
||||
if (!hasNext) itCounter = 0
|
||||
|
||||
return hasNext
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (hasNext()) {
|
||||
return this.items[this.itCounter++]
|
||||
} else {
|
||||
throw NoSuchElementException("No such element")
|
||||
}
|
||||
fun toList() : List<T>{
|
||||
return this.items.toList();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue