diff --git a/src/dev/peerat/framework/Client.java b/src/dev/peerat/framework/Client.java index 2fb141d..61ffb9c 100644 --- a/src/dev/peerat/framework/Client.java +++ b/src/dev/peerat/framework/Client.java @@ -29,20 +29,20 @@ public class Client extends Thread{ writer.flush(); writer.close(); }catch(AuthException e){ - this.router.getExceptionLogger().setValue(e); + this.router.getExceptionLogger().pushValue(e); }catch(Throwable e){ - this.router.getExceptionLogger().setValue(e); + this.router.getExceptionLogger().pushValue(e); if(context != null && context.getResponseCode() == 0){ try{ context.response(500); writer.flush(); writer.close(); }catch(Exception ex){ - this.router.getExceptionLogger().setValue(ex); + this.router.getExceptionLogger().pushValue(ex); } } } - if(context != null) router.getLogger().setValue(context); + if(context != null) router.getLogger().pushValue(context); } private User isLogin(RequestType type, HttpReader reader) throws AuthException{ @@ -54,7 +54,7 @@ public class Client extends Thread{ writer.flush(); writer.close(); }catch(Exception ex){ - this.router.getExceptionLogger().setValue(ex); + this.router.getExceptionLogger().pushValue(ex); } throw new AuthException(e); } diff --git a/src/dev/peerat/framework/Locker.java b/src/dev/peerat/framework/Locker.java index a970301..e53f95b 100644 --- a/src/dev/peerat/framework/Locker.java +++ b/src/dev/peerat/framework/Locker.java @@ -1,9 +1,8 @@ package dev.peerat.framework; -import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.function.Consumer; import java.util.function.Supplier; @@ -13,7 +12,7 @@ public class Locker{ private Map> map; public Locker(){ - this.map = new HashMap<>(); + this.map = new ConcurrentHashMap<>(); } public void init(Key key){ @@ -28,21 +27,20 @@ public class Locker{ return this.map.get(key); } - public void setValue(V value){ - for(Entry> entry : this.map.entrySet()){ - entry.getValue().add(value); - this.unlock(entry.getKey()); + public void pushValue(V value){ + for(BlockingQueue queue : this.map.values()){ + queue.add(value); + unlock(queue); } } - public V getValue(Key key){ + public V getValue(Key key) throws InterruptedException{ BlockingQueue queue = get(key); - if(queue.isEmpty()) return null; + lock(queue); return queue.poll(); } - public void lock(Key key) throws InterruptedException{ - BlockingQueue queue = get(key); + private void lock(BlockingQueue queue) throws InterruptedException{ if(queue.isEmpty()){ synchronized(queue){ queue.wait(); @@ -50,8 +48,7 @@ public class Locker{ } } - public void unlock(Key key){ - BlockingQueue queue = get(key); + private void unlock(BlockingQueue queue){ synchronized(queue){ queue.notify(); } @@ -61,46 +58,40 @@ public class Locker{ Key key = new Key(); init(key); try { - while(true){ - lock(key); - V value = getValue(key); - action.accept(value); - } + while(true) action.accept(getValue(key)); }catch(Exception e){} remove(key); } - public void listen(Supplier condition,Consumer action){ + public void listen(Consumer action, Consumer onClose){ Key key = new Key(); init(key); try { - while(condition.get()){ - lock(key); - V value = getValue(key); - action.accept(value); - } + while(true) action.accept(getValue(key)); + }catch(Exception e){ + onClose.accept(e); + } + remove(key); + } + + public void listen(Supplier condition, Consumer action){ + Key key = new Key(); + init(key); + try { + while(condition.get()) action.accept(getValue(key)); }catch(Exception e){} remove(key); } - public Thread listenAsync(Consumer action){ - Thread thread = new Thread(new Runnable(){ - public void run(){ - listen(action); - } - }); - thread.start(); - return thread; - } - - public Thread listenAsync(Supplier condition,Consumer action){ - Thread thread = new Thread(new Runnable(){ - public void run(){ - listen(condition, action); - } - }); - thread.start(); - return thread; + public void listen(Supplier condition, Consumer action, Consumer onClose){ + Key key = new Key(); + init(key); + try { + while(condition.get()) action.accept(getValue(key)); + }catch(Exception e){ + onClose.accept(e); + } + remove(key); } public static class Key{ public Key(){} }