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