* @param updatee The newly joined client.
* @param updaterType Updater type as specified by the OCW application.
*/
private void assignUpdater(ServerSession updatee, String updaterType) {
log.info("assignUpdater *****************");
ServerSession from = this.sessionManager.getServerSession();
if (this.updaters.isEmpty()) {
this.addUpdater(updatee, false);
((ServerSession)updatee).deliver(from, "/service/session/join/state",
new ArrayList<String>(), null);
return;
}
String updaterId = null;
ServerSession updater = null;
if (!updaterType.equals("default")) {
/* Try to find a custom updater. */
String matchedType = updaterTypeMatcher.match(updaterType,
getAvailableUpdaterTypes());
if (matchedType != null) {
for (String id : this.updaters.keySet()) {
updater = this.clientids.get(id);
if (updater.getAttribute("updaterType").equals(matchedType)) {
updaterId = id;
log.fine("found an updater type matched to [" + matchedType + "]");
break;
}
}
}
}
if (updaterId == null) {
/* Choose random updater for default types or if a custom updater wasn't
* found. */
Random r = new Random();
int idx = r.nextInt(this.updaters.size());
log.fine("using default updater type");
Object[] keys = this.updaters.keySet().toArray();
updaterId = (String) keys[idx];
updater = this.clientids.get(updaterId);
}
/* Now we've found an updater, so generate a token and ask the updater to
* provide full application state. */
log.fine("assigning updater " + updater.getAttribute("siteid") + " to "
+ updatee.getAttribute("siteid"));
SecureRandom s = new SecureRandom();
String token = new BigInteger(130, s).toString(32);
this.updaters.get(updaterId).add(token);
this.updatees.put(token, updatee);
updater.deliver(from, "/service/session/updater", token, null);
}