| 1 | // $Id: LoginBroker.java,v 1.6 2007-10-18 04:28:58 behrmann Exp $ |
| 2 | |
| 3 | package dmg.cells.services.login ; |
| 4 | |
| 5 | import java.util.* ; |
| 6 | import dmg.cells.nucleus.*; |
| 7 | import dmg.util.* ; |
| 8 | |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | |
| 12 | public class LoginBroker |
| 13 | extends CellAdapter |
| 14 | implements Runnable |
| 15 | { |
| 16 | private final static Logger _log = |
| 17 | LoggerFactory.getLogger(LoginBroker.class); |
| 18 | |
| 19 | private int _delay = 3; |
| 20 | |
| 21 | /** |
| 22 | * Map from identifiers to entries. |
| 23 | */ |
| 24 | private Map<String, LoginEntry> _hash = |
| 25 | new HashMap<String,LoginEntry>(); |
| 26 | |
| 27 | /** |
| 28 | * Set of identifiers of disabled entries. |
| 29 | */ |
| 30 | private Set<String> _disabled = |
| 31 | new HashSet<String>(); |
| 32 | |
| 33 | private class LoginEntry { |
| 34 | private long _time ; |
| 35 | private LoginBrokerInfo _info ; |
| 36 | private LoginEntry( LoginBrokerInfo info ){ |
| 37 | _time = System.currentTimeMillis() ; |
| 38 | _info = info ; |
| 39 | } |
| 40 | private boolean isValid(){ |
| 41 | return System.currentTimeMillis() < |
| 42 | ( _time + (long)_delay * _info.getUpdateTime() ) ; |
| 43 | } |
| 44 | public LoginBrokerInfo getInfo(){ return _info ; } |
| 45 | public boolean equals( Object obj ){ |
| 46 | return (obj instanceof LoginEntry) && _info.equals( ((LoginEntry)obj).getInfo() ) ; |
| 47 | } |
| 48 | public int hashCode(){ return _info.hashCode() ; } |
| 49 | public String toString(){ |
| 50 | return _info.toString()+ |
| 51 | (_time+_info.getUpdateTime()-System.currentTimeMillis())+ |
| 52 | ";"+ |
| 53 | (isValid()?"VALID":"INVALID")+";" ; |
| 54 | } |
| 55 | public String getIdentifier(){ return _info.getIdentifier() ; } |
| 56 | } |
| 57 | public LoginBroker(String name, String argString) throws Exception |
| 58 | { |
| 59 | super(name, argString, false); |
| 60 | |
| 61 | getNucleus().newThread(this,"Cleaner").start(); |
| 62 | start(); |
| 63 | } |
| 64 | |
| 65 | public CellVersion getCellVersion(){ |
| 66 | return new CellVersion(super.getCellVersion().getRelease(),"$Revision: 1.6 $" ); |
| 67 | } |
| 68 | public String hh_ls = "[-binary] [-protocol=<protocol_1,...,protocol_n>] [-time]"; |
| 69 | public Object ac_ls(Args args) |
| 70 | { |
| 71 | boolean binary = args.getOpt("binary") != null; |
| 72 | String protocols= args.getOpt("protocol"); |
| 73 | ArrayList list = new ArrayList(); |
| 74 | StringBuffer sb = new StringBuffer(); |
| 75 | boolean showTime = args.getOpt("l") != null; |
| 76 | Set<String> protocolSet = null; |
| 77 | |
| 78 | if(protocols != null){ |
| 79 | protocolSet = new HashSet(); |
| 80 | for(String protocol: protocols.split(",")) |
| 81 | protocolSet.add(protocol); |
| 82 | } |
| 83 | |
| 84 | synchronized (this) { |
| 85 | for (LoginEntry entry : _hash.values()) { |
| 86 | LoginBrokerInfo info = entry.getInfo(); |
| 87 | boolean disabled = |
| 88 | _disabled.contains(info.getIdentifier()) |
| 89 | || _disabled.contains(info.getCellName()); |
| 90 | |
| 91 | if (protocols != null && |
| 92 | !protocolSet.contains(info.getProtocolFamily())) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | if (binary) { |
| 97 | if (!disabled) { |
| 98 | list.add(info); |
| 99 | } |
| 100 | } else { |
| 101 | sb.append(showTime ? entry.toString() : info.toString()); |
| 102 | if (disabled) { |
| 103 | sb.append("disabled;"); |
| 104 | } |
| 105 | sb.append("\n"); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | if (binary) { |
| 110 | return list.toArray(new LoginBrokerInfo[list.size()]); |
| 111 | } else { |
| 112 | return sb.toString(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public String hh_disable = "<door> ..."; |
| 117 | public synchronized String ac_disable_$_1_99(Args args) |
| 118 | { |
| 119 | for (int i = 0; i < args.argc(); i++) { |
| 120 | _disabled.add(args.argv(i)); |
| 121 | } |
| 122 | return ""; |
| 123 | } |
| 124 | |
| 125 | public String hh_enable = "<door> ..."; |
| 126 | public synchronized String ac_enable_$_1_99(Args args) |
| 127 | { |
| 128 | for (int i = 0; i < args.argc(); i++) { |
| 129 | _disabled.remove(args.argv(i)); |
| 130 | } |
| 131 | return ""; |
| 132 | } |
| 133 | |
| 134 | public void run(){ |
| 135 | try{ |
| 136 | while( ! Thread.interrupted() ){ |
| 137 | synchronized(this){ |
| 138 | HashMap set = new HashMap() ; |
| 139 | |
| 140 | Iterator i = _hash.values().iterator() ; |
| 141 | while( i.hasNext() ){ |
| 142 | LoginEntry entry = (LoginEntry)i.next() ; |
| 143 | if( entry.isValid() )set.put(entry.getIdentifier(),entry) ; |
| 144 | } |
| 145 | _hash = set ; |
| 146 | } |
| 147 | Thread.sleep(60000); |
| 148 | } |
| 149 | }catch(Exception ee ){ |
| 150 | _log.info("Worker interrupted due to : "+ee ) ; |
| 151 | } |
| 152 | } |
| 153 | public void messageArrived( CellMessage msg ){ |
| 154 | Object obj = msg.getMessageObject() ; |
| 155 | |
| 156 | if( obj instanceof LoginBrokerInfo ){ |
| 157 | synchronized(this){ |
| 158 | LoginEntry entry = new LoginEntry((LoginBrokerInfo)obj) ; |
| 159 | _hash.put( entry.getIdentifier() , entry ) ; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | } |