In the previous lessons we led you through the Spiderwiz paradigm of the Shared Data Object Tree. You exercised with application code that accessed data rather than communicating with services. One of the major concepts of this paradigm is that as soon as the application starts, it gets all the data that it needs in its space and can access and manipulate it with no extra mechanism.
Under the hood, this concept is implemented with a mechanism called Data Object Reset. When an application starts (or when data integrity is compromised, see below), it broadcasts a Reset request for all the object types it consumes. Producers of these types respond by sending the objects that they have created over the channel through which the Reset request arrived.
Normally the entire process is automatic and programmers need not intervene. However there are cases when the programmer wants to control the process programmatically, for instance for resetting disposable objects that are fetched from an external source and not kept in the shared data object tree. This technique is demonstrated in this lesson.
We stay with the chat service of the previous lessons and add a new service – Room Producer – that creates chat rooms by reading a list of room names (possibly with the ‘+’ adult indicator) from a file, one name per line. Room Producer does not keep the rooms in memory (it overrides isDisposable() of ChatRoom
to return true
), so every time a consumer of this object type asks for reset it needs to open the file, read it, create ChatRoom
objects from it and send them to the requester. This is done in RoomProducerMain
:
package org.spiderwiz.tutorial.lesson9.roomProducer; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import org.spiderwiz.core.DataObject; import org.spiderwiz.core.Resetter; import org.spiderwiz.tutorial.objectLib.ChatRoom; import org.spiderwiz.tutorial.objectLib.ConsoleMain; /** * Provides the entry point of the application. Initializes and executes the Spiderwiz framework. */ public class RoomProducerMain extends ConsoleMain { private static final String ROOT_DIRECTORY = ""; private static final String CONF_FILENAME = "room-producer.conf"; private static final String APP_NAME = "Room Producer"; private static final String APP_VERSION = "Z1.01"; // Version Z1.01: Initial version private static final String ROOM_FILE = "room file"; /** * Class constructor with constant parameters. */ public RoomProducerMain() { super(ROOT_DIRECTORY, CONF_FILENAME, APP_NAME, APP_VERSION); } /** * Application entry point. Instantiate the class and initialize the instance. * * @param args the command line arguments. Not used in this application. */ public static void main(String[] args) { new RoomProducerMain().init(); } /** * @return the list of produced objects, in this case ChatRoom is the only one. */ @Override protected String[] getProducedObjects() { return new String[]{ChatRoom.ObjectCode}; } /** * @return the list of consumed objects, in this case we do not consume any so we return an empty list. */ @Override protected String[] getConsumedObjects() { return new String[]{}; } /** * Add ChatRoom class to the object factory list of this application. * @param factoryList */ @Override protected void populateObjectFactory(List<Class<? extends DataObject>> factoryList) { super.populateObjectFactory(factoryList); factoryList.add(ChatRoomProducer.class); } /** * If room file is not defined in the configuration file, log an error message in the log file and in stdout and quit * the application. * @return true if and only if a configuration file is defined. */ @Override protected boolean preStart() { if (getConfig().getProperty(ROOM_FILE) == null) { getLogger().logEvent("Room file is not defined."); return false; } return true; } /** * If the resetter object is for ChatRoom reset all chat rooms from the file defined in the configuration file and return true; * @param resetter * @return true if reset is done here. */ @Override protected boolean onObjectReset(Resetter resetter) { // Check resetter type if (!resetter.getObjectCode().equals(ChatRoom.ObjectCode)) return false; // Set reset rate to zero in order to eliminate moderation so that every object is sent as soon as possible resetter.setResetRate(0); // Get configured file name String filename = getConfig().getProperty(ROOM_FILE); // Read lines from the file try (BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename)))) { String line; while ((line = in.readLine()) != null) { // Check if the resetter has not been aborted by an overriding reset request. if (resetter.isAborted()) return true; // Parse the line for room name and adult symbol String name[] = line.split("+", -1); String roomName = name[0]; boolean adult = name.length > 1; // Create a ChatRoom object, set its properties and reset it. ChatRoom obj = createTopLevelObject(ChatRoom.class, roomName); obj.setAdult(adult); resetter.resetObject(obj); } // Mark end of data resetter.endOfData(); } catch (IllegalAccessException | NoSuchFieldException ex) { sendExceptionMail(ex, "When creating a ChatRoom object", null, false); } catch (IOException ex) { getLogger().logEvent("Open file failure: %1$s.", ex.getMessage()); sendNotificationMail("Open file failure", ex.getMessage(), null, true); } return true; } /** * Called after delivery of all items. Print item count. * @param resetter the Resetter object used for delivery. */ @Override protected void onResetCompleted(Resetter resetter) { System.out.printf("Reset done. %d items have been delivered.n", resetter.getResetCount()); } /** * @param line * @return true since we do not do any line processing */ @Override protected boolean processConsoleLine(String line) { return true; } }
As expected, the application produces ChatRoom
(line 43) and does not consume anything (line 51). It extends ChatRoom
by ChatRoomProducer
and registers it (line 62).
The file that contains the room names should be specified in the configuration file. In preStart() (line 71) we verify the existence of the room file
property and terminate the program by returning false
if it does not.
The interesting method that performs the reset is the overriding onObjectReset() (line 85). The method is called every time a reset for a specific object type that is produced by this application is requested by any peer application. Its parameter is a Resetter object that specifies the requested object type and also serves as a carrier that delivers objects of this type back to the requester, as we are going to see now.
The first thing onObjectReset()
does is to check whether the requested type is what we care about. This is done by calling getObjectCode() of the resetter
parameter and comparing it to ChatRoom.ObjectCode
.
We call the object’s setResetRate() to eliminate its default moderation effect, because in our test we expect to deliver a relatively small amount of objects that should better be delivered as quickly as possible. If the test file contained a very large number of items and the network speed were constrained then we could set an appropriate rate (or leave the default of 30,000 items per minute) to avoid network congestion.
We then open the configured input file and loop on reading its lines. Within each iteration we first verify that the resetter is still active by calling isAborted(). It might be aborted if, during its operation, another reset request for the same object type arrives on the same channel. In this case, in order to avoid unnecessary duplicate data, the framework aborts the operation of the former resetter assuming that the data would be completely delivered by the latter.
If everything is OK, we parse the input line, create one object by calling createTopLevelObject(), set its values and call the resetter’s resetObject() method to deliver the object.
When we reach the file end we call the reseter’s optional endOfData() method. We will see its use in a moment.
We will use the occasion to dwell upon the two catch
clauses that enclose the code. The first catches IllegalAccessException
and NoSuchFieldException
exceptions that might happen if ChatRoom
class does not define an ObjectCode
static field or the field is not declared public
. In these cases we call sendExceptionMail() to report the exception and its stack trace to the following:
- The standard error stream.
- The application’s log system.
- By mail to the address configured in the application configuration file if relevant. We will see below an example of it.
The second catch clause catches IOException
exceptions, which might happen if, for any reason, the input file could not be opened or read. The clause logs the event in the application’s log system and also calls sendNotificationMail() to report the event by mail. Note that addressees of notification mails, which are usually system administrators, may be configured differently than addressees of exception mails that are usually developers.
The last method to look at is the overriding onResetCompleted() (line 133). We mentioned above that, having submitted all reset items, we call resetter.endOfData()
to mark the end of the operation. Since the submission of the items includes buffering, it may be that some items have not been dispensed yet when the method is called. When they finally are, onResetCompleted()
is called and we override it to print out some information, in this case the total number of items that were reset.
The application package includes one more class – ChatRoomProducer
:
package org.spiderwiz.tutorial.lesson9.roomProducer; import java.util.Map; import java.util.UUID; import org.spiderwiz.tutorial.objectLib.ChatRoom; /** * Overrides ChatRoom to return true in isDisposable */ public class ChatRoomProducer extends ChatRoom { /** * Filter the destination so that logged in chatters will receive the object, and only if either this is not an adult room * or the destination is of an adult user. Destinations other than chat applications will receive the object with no filtering. * @param appUUID application UUID. * @param appName application name. * @param userID n/a * @param remoteAddress remote address of the destination application. * @param appParams application parameter map as set by Main.getAppParams() of the filtered application. If the destination * is a chat application the map is not null and it contains a mapping of the "state" key to any of * "logout", "login" or "adult". The latter means the user is logged in as an adult. * @return true to approve distribution to this application, false to deny it. */ @Override protected boolean filterDestination(UUID appUUID, String appName, String userID, String remoteAddress, Map<String, String> appParams) { if (appParams == null) return true; String state = appParams.get("state"); if (state == null) return true; switch(state) { case "logout": return false; case "login": return !isAdult(); case "adult": return true; default: return false; } } @Override protected boolean isDisposable() { return true; } }
This extends ChatRoom
and overrides two methods – filterDestination() (line 25) and isDisposable() (line 44). Please ignore the first for now – we will come back to it later. The second method is implemented to return true
. Everything would work fine without it, but since we reread item data from file every time we are requested to reset them, there is no reason to keep all ChatRoom
objects in memory.
Again, to run it we need a configuration file:
[log folder]/tests/RoomProducer/Logs [producer-1]ip=localhost;port=10001 [room file]/tests/RoomProducer/rooms.txt [mail system]smtp;server=smtp.gmail.com;user=tester@spiderwiz.org;pwd=dumptrump;port=465;ssl=true [from address]Room Producer<alert@spiderwiz.org> [to email]Spiderwiz Administrator<spiderwiz.admin@gmail.com> [to exception email]Spiderwiz Programmer<spiderwiz.geek@gmail.com>
You can see the custom room file
property that we use to get the room file name. You can also see an example of mail system and address configuration for bug reports and alerts as discussed above.
The discussion so far pertains to the case when an application that receives a reset request handles it programmatically. There are cases when programming intervention is required at the requester side. Consider for example the following:
The Chat Application that we use in this lesson works by
receiving all ChatRoom
objects as
soon as they are available. It also receives a Chatter
object every time a chatter in any peer application enters
any room. However, the application does not let a user join a room before
logging in or registration. Additionally, some rooms may be defined as adult rooms, so the application checks
the user’s age before letting them join such a room.
This works but involves superfluous overhead in terms of
traffic volume and memory. Wouldn’t it be nice if we could hold the
transmission of ChatRoom
and Chatter
objects to a chat application
until its user logs in, and when that happens check the user’s age and refrain
from sending adult room objects to young users?
Yes, we can, and we are going to show now how to do it. For
that we will need to slightly modify the Chat application. Here is ChatMain
:
package org.spiderwiz.tutorial.lesson9.chat; import java.io.PrintStream; import java.text.ParseException; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.spiderwiz.core.DataObject; import org.spiderwiz.tutorial.objectLib.ActiveUserQuery; import org.spiderwiz.tutorial.objectLib.Chat; import org.spiderwiz.tutorial.objectLib.ChatHistoryQuery; import org.spiderwiz.tutorial.objectLib.ChatMessage; import org.spiderwiz.tutorial.objectLib.ChatRoom; import org.spiderwiz.tutorial.objectLib.Chatter; import org.spiderwiz.tutorial.objectLib.ConsoleMain; import org.spiderwiz.tutorial.objectLib.LoginQuery; import org.spiderwiz.zutils.ZDate; import org.spiderwiz.zutils.ZUtilities; /** * Provides the entry point of the application. Initializes and executes the Spiderwiz framework. */ public class ChatMain extends ConsoleMain{ private static final String ROOT_DIRECTORY = ""; private static final String APP_NAME = "Chat Room Client"; private static final String APP_VERSION = "Z9.01"; // Version Z9.01: Lesson 9 modifications private static final String JOIN = "!join"; // join a chat room command private static final String LEAVE = "!leave"; // leave current chat room command private static final String LOGIN = "!login"; // start login procedure private static final String LOGOUT = "!logout"; // log the user out private static final String REGISTER = "!register"; // start registration procedure private static final String HISTORY = "!history"; // print my chat history private String myName = null; // The user login name private ZDate birthday = null; // User birth date. Get it when logging in private Chatter chatter = null; // A Chatter object representing the user chatting in a specific room private ChatMessage message = null; // A ChatMessage object for committing chat messages private Chat privateMessage = null; // A Chat object for committing private messages /** * Class constructor with constant parameters. * @param confFileName configuration file name, provided as a command argument */ public ChatMain(String confFileName) { super(ROOT_DIRECTORY, confFileName, APP_NAME, APP_VERSION); } /** * @return the class instance as ChatMain type. */ public static ChatMain getInstance() { return (ChatMain)ConsoleMain.getInstance(); } /** * Application entry point. Instantiate the class and initialize the instance. * * @param args the command line arguments. The first argument is used as the configuration file name. */ /** * @param args the command line arguments */ public static void main(String[] args) { // Don't do anything if there is no configuration file name if (args.length == 0) { System.out.println("Configuration file has not been defined"); return; } new ChatMain(args[0]).init(); } /** * Print user instructions to the console. * @return true */ @Override protected boolean preStart() { System.out.printf( "Hello %s. Welcome to the dynamic chat system.n" + "To log in type !login.n" + "To log out type !logout.n" + "To register type !register.n" + "To join a chat room or change your current chat room, type "!join <room name>".n" + "To leave your chat room, type "!leave".n" + "To send a message in the current chat room just type the message " + "(a message cannot start with either '>' or '!').n" + "To send a private message type '>' followed by name of the user you want to send " + "the private message to, followed by ':' for by the message.n" + "To print the history of your own messages, type "!history <hours>h" or "!history <days>d"n" + "(for instance "!history 3h" to see messages since 3 hours ago and "!history 1d" to see messages since 1 day" + " ago).n" + "or type just "!history" to see all your messages.n" + "To exit type 'exit'.n", myName ); return true; } /** * @return the list of produced objects - Chatter, Chat, ChatMessage and LoginQuery */ @Override protected String[] getProducedObjects() { return new String[]{ Chatter.ObjectCode, Chat.ObjectCode, ChatMessage.ObjectCode, LoginQuery.ObjectCode, ChatHistoryQuery.ObjectCode }; } /** * @return the list of consumed objects - ChatRoom, Chatter, Chat, ChatMessage and ActiveUserQuery */ @Override protected String[] getConsumedObjects() { return new String[]{ ChatRoom.ObjectCode, Chatter.ObjectCode, Chat.ObjectCode, ChatMessage.ObjectCode, ActiveUserQuery.ObjectCode }; } /** * Add implementation classes to the object factory list of this application. * @param factoryList */ @Override protected void populateObjectFactory(List<Class<? extends DataObject>> factoryList) { super.populateObjectFactory(factoryList); factoryList.add(ChatRoomConsume.class); factoryList.add(ChatterImp.class); factoryList.add(ChatConsume.class); factoryList.add(ChatMessageImp.class); factoryList.add(LoginQuery.class); factoryList.add(ActiveUserQueryReply.class); factoryList.add(ChatHistoryQueryInquire.class); } /** * Maps "state" to either "logout", "login" or "adult" * @return */ @Override public Map<String, String> getAppParams() { return new HashMap<String, String>() { { put("state", myName == null ? "logout" : ZDate.now().diffMonths(birthday) >= 12 * 18 ? "adult" : "login"); } }; } /** * Process an input line * @param line the line to be broadcast as a chat message. * @return true if processed successfully */ @Override protected boolean processConsoleLine(String line) { try { // If the line starts with '!' this is a command line if (line.startsWith("!")) { String command[] = line.trim().split("s+", 2); switch (command[0].toLowerCase()) { case JOIN: // if room name is provided join the room if (command.length > 1) joinRoom(command[1]); break; case LEAVE: // If the user is in a room, leave it leaveRoom(null); break; case LOGIN: login(); break; case LOGOUT: logout(); break; case REGISTER: register(); break; case HISTORY: if (myName == null) System.out.println("You must log in before requesting history."); else if (!getHistory(command.length < 2 ? null : command[1])) System.out.println("Invalid command argument"); break; } return true; } // If the line starts with '>' split the line on the colon (':') and send the test // after the colon as a private message to the user with the name before the colon. if (line.startsWith(">")) { String cmd[] = line.substring(1).split(":"); if (cmd.length < 2) return true; String destination = findUser(cmd[0]); if (destination == null) return true; // If didn't do yet, create an orphan Chatter object for sending private messages, // then prepare a ChatMessage object for sending private messages if (privateMessage == null) { privateMessage = createTopLevelObject(Chat.class, null); privateMessage.setName(myName); } // Set message and commit to destination privateMessage.setMessage(cmd[1]); privateMessage.commit(destination); return true; } // If this is a normal message, make sure the user is in a room and broadcast the message if (message != null) { message.setMessage(line); message.commit(); } return true; } catch (NoSuchFieldException | IllegalAccessException ex) { // Theoretically arriving here if a data object does not contain ObjectCode static field or the field is not public. // In this case, send a command exception message. sendExceptionMail(ex, "Cannot instantiate a data object class", null, false); return false; } } public String getMyName() { return myName; } public ZDate getBirthday() { return birthday; } /** * Get user name and password and log in. * @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding. * @throws NoSuchFieldException If LoginQuery does not define ObjectCode static field. * @throws IllegalAccessException If LoginQuery.ObjectCode is not public. */ private PrintStream login() throws NoSuchFieldException, IllegalAccessException { String username; String password; // Check if already logged in if (myName != null) return System.out.printf("You are already logged in as %s. To log in with another name log out first.n", myName); // Create a login query LoginQuery query = createQuery(LoginQuery.class); while(true) { // Get user name System.out.printf("(type Enter to exit login)n"); username = readConsoleLine("User name:"); if (username.isBlank()) return exitLogin(); do { // Get password password = readConsoleLine("Password:"); if (password.isBlank()) return exitLogin(); // Post the login query query.setNewUser(false); query.setName(username); query.setPassword(password); query.post(5 * ZDate.SECOND); // If the query did not expire check login results, if it did, repeat. if (query.waitForReply()) { switch (query.getResult()) { case OK: // Log in and request a general reset myName = query.getName(); birthday = query.getBirthday(); reset(); return System.out.printf("Logged in successfully as %s.n", myName); case NOT_EXISTS: System.out.printf("User %s does not exist. Please try again.n", username); break; case WRONG_PASSWORD: System.out.printf("Password did not match, please try again.n"); break; } } else return unavailableUserManager(); } while (query.getResult() == LoginQuery.ResultCode.WRONG_PASSWORD); } } /** * Log out if already logged in */ private void logout() { if (myName == null) { System.out.printf("No user is logged in.n"); return; } leaveRoom(null); // Log out and request a general reset. System.out.printf("%s is logged out.n", myName); myName = null; birthday = null; reset(); } private PrintStream register() throws NoSuchFieldException, IllegalAccessException { String username; String password, password2; // Check if already logged in if (myName != null) return System.out.printf("You are already logged in as %s. To register a new user log out first.n", myName); // Create a registration query LoginQuery query = createQuery(LoginQuery.class); while(true) { // Get user name System.out.printf("(type Enter to exit login)n"); username = readConsoleLine("User name:"); if (username.isBlank()) return exitRegistration(); do { // Get password password = readConsoleLine("Password:"); if (password.isBlank()) return exitRegistration(); // Repeat the password password2 = readConsoleLine("Repeat password:"); if (password2.isBlank()) return exitRegistration(); // Check if passwords are equal if (password.equals(password2)) break; System.out.println("Passwords do not match."); } while (true); // Get and parse birth date do { String s = readConsoleLine("Date of birth (yyyy/mm/dd):"); if (s.isBlank()) return exitRegistration(); try { birthday = ZDate.parseTime(s, "yyyy/MM/dd", null); } catch (ParseException ex) { birthday = null; System.out.printf("Invalid date %s. Please try again.n", s); } } while (birthday == null); // Post the registration query query.setNewUser(true); query.setName(username); query.setPassword(password); query.setBirthday(birthday); query.post(5 * ZDate.SECOND); // If the query did not expire check login results, if it did, repeat. if (query.waitForReply()) { switch (query.getResult()) { case OK: myName = username; reset(); return System.out.printf("Registered and logged in successfully as %s.n", myName); case EXISTS: System.out.printf("User %s already exista. Please try again with another name.n", username); break; } } else return unavailableUserManager(); } } /** * Print a message when login existed by typing a blank Enter * @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding. */ private PrintStream exitLogin() { return System.out.printf("Login exitedn"); } /** * Print a message when registration existed by typing a blank Enter * @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding. */ private PrintStream exitRegistration() { return System.out.printf("Registration exitedn"); } /** * Print a message when user management is not available * @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding. */ private PrintStream unavailableUserManager() { return System.out.printf("No user management entity is available to handle this requestn"); } /** * Join a room after checking that the room exists, its adult setting matches user configuration, * and the user is not already there. If the user is in another room leave that room first. * @param room the new room name * @throws NoSuchFieldException If any of the created data objects does not define ObjectCode static field. * @throws IllegalAccessException If ObjectCode is not public. */ private synchronized void joinRoom(String room) throws NoSuchFieldException, IllegalAccessException { // Check whether room exists and the user is not already in the room ChatRoomConsume chatRoom = getRootObject().getChild(ChatRoomConsume.class, room); if (chatRoom == null) { System.out.printf("Room %s does not existn", room); return; } if (chatter != null && room.equalsIgnoreCase(chatter.getParent().getObjectID())) return; // Leave current room if any leaveRoom(null); // Creaet a Chatter object for the user that joins the specified room chatter = chatRoom.createChild(Chatter.class, getAppUUID().toString()); chatter.setName(myName); chatter.setBirthday(birthday); chatter.commit(); // let everybody know I joined the room // Prepare a ChatMessage object for sending messages from now on message = chatter.createChild(ChatMessage.class, null); System.out.printf("Joined room %sn", room); } /** * If the user is in the given room, leave it. * @param room the name of the room to be checked if the user is there. If null, leave the current room without checking */ public synchronized void leaveRoom(String room) { if (isSameRoom(room)) { DataObject removed = chatter.remove(); // leave the room if (removed != null) // can be null if the 'chatter' object has already been removed. removed.commit(); // let everybody know I did System.out.printf("Left room %sn", chatter.getParent().getObjectID()); chatter = null; message = null; } } /** * Check if the user is in the same room as the parameter * @param room the name of the room to be checked if the user is there. If null, return true * @return true if the user in 'room' or 'room' is null */ public synchronized boolean isSameRoom(String room) { return chatter != null && (room == null || room.equalsIgnoreCase(chatter.getParent().getObjectID())); } /** * Check whether the user running the application whose UUID is given is on the same room as us. This is done by getting * our current ChatRoom object and see if it has a child with that ID. * @param appUUID Application UUID to check * @return true if the application is in the same room as us. */ public synchronized boolean isMemberOfMyRoom(UUID appUUID) { try { if (chatter == null) return false; ChatRoom chatRoom = (ChatRoom)chatter.getParent(); return chatRoom != null && chatRoom.getChild(Chatter.class, appUUID.toString()) != null; } catch (NoSuchFieldException | IllegalAccessException ex) { // Theoretically arriving here if a data object does not contain ObjectCode static field or the field is not public. // In this case, send a command exception message. sendExceptionMail(ex, "Cannot instantiate a data object class", null, false); return false; } } /** * Get current room name, if any * @return if the user is currently in a room return the room name, otherwise return null. */ public synchronized String getCurrentRoom() { return chatter == null ? null : chatter.getParent().getObjectID(); } /** * Find the user whose name equals the parameter * @param name user name to look for * @return If a Chatter object for this user found then return its object ID, which is the UUID of the application * that the given user runs, otherwise return null. * @throws NoSuchFieldException If any of the referred data objects does not define ObjectCode static field. * @throws IllegalAccessException If ObjectCode is not public. */ private String findUser(String name) throws NoSuchFieldException, IllegalAccessException { FindUserFilter filter = new FindUserFilter(name); Collection<Chatter> users = getRootObject().getFilteredChildren(filter); // If the returned collection is not empty return the object ID (which is a UUID) of the first, // otherwise return null. for (Chatter user : users) { return user.getObjectID(); } return null; } /** * Execute !history command * @param arg command argument - <n>H or <n>D * @return true if command has been parsed successfully. * @throws NoSuchFieldException If ChatHistoryQuery does not define ObjectCode static field. * @throws IllegalAccessException If ChatHistoryQuery.ObjectCode is not public. */ private boolean getHistory(String arg) throws NoSuchFieldException, IllegalAccessException { // Parse the argument and calculate time ZDate since = null; if (arg != null) { arg = arg.trim().toLowerCase(); int n = ZUtilities.parseInt(arg.substring(0, arg.length() - 1)); if (n <= 0) return false; int unit; switch (arg.substring(arg.length() - 1)) { case "h": unit = ZDate.HOUR; break; case "d": unit = ZDate.DAY; break; default: return false; } since = ZDate.now().add(-n * unit); } // create a query and post it ChatHistoryQuery query = createQuery(ChatHistoryQuery.class); query.setUsername(myName); query.setSince(since); query.post(5 * ZDate.SECOND); return true; } }
The most notable change is the override of getAppParams() in line 151. Recall that this method is used for fine-grained routing by peer applications. In our case we want to filter out ChatRoom
and Chat
objects depending on whether a user is logged in and the age of the user in the case of adult rooms. So we map the key “state”
to either "logout"
, "login"
or "adult"
depending on the values of class variables myName
(that is null
if no user has logged in or registered) and birthday
.
Before looking at how this mapping is used for object
filtering, we need to tackle an intriguing problem that goes back to the issue
of Data Object Reset that is the focus of this lesson. The way the framework
works, the value returned by getAppParams()
is conveyed to peer applications as part of Reset requests posted by the
application. Normally Reset requests are transmitted whenever an application
connects to the network. But in our case the mapping of the “state”
key changes when a user logs in
or logs out, and that has nothing to do with established network connections.
What we need is to be able to programmatically broadcast a new Reset request every time something is changed that may affect object filtering by peer applications, in other words, Manual Data Object Reset at the requester side. This is exactly what the reset() method does. More precisely, the method clears the entire Data Object Tree saved in the memory of the calling application and broadcasts a new Reset request for all the object types that it consumes.
In our case, we call the reset()
method in line 283, after a successful user login, in line 312, after a logout
and in line 373, after user registration.
To see that the trick does the job, we remove the code that
verifies that the user is logged in before joining a room (line 416). Since an
application should not receive any ChatRoom
objects if the user is not logged in, an attempt to join a room in this
situation should result in a message that the room is not available. This
should also happen when an underage user is trying to enter an adult room, so
we remove the code that checks that as well (line 426).
It remains to see how object filtering works with the
procedure that was introduced here. Let’s start first with the Chat
application. We want to filter the transmission of Chatter
objects, so we do it in ChatterImp
that we renamed from ChatterConsume
(because it now handles produced objects as well as the consumed ones):
package org.spiderwiz.tutorial.lesson9.chat; import java.util.Map; import java.util.UUID; import org.spiderwiz.tutorial.objectLib.ChatRoom; import org.spiderwiz.tutorial.objectLib.Chatter; /** * Consumer implementation of Chatter data object. Notify when a chatter enters or leaves my room. */ public class ChatterImp extends Chatter { /** * Notify when a chatter enters my room. */ @Override protected void onNew() { // The room is the parent of this object if (!isStealth() && ChatMain.getInstance().isSameRoom((getParent().getObjectID()))) System.out.printf("%s entered the roomn", getName()); } /** * Notify when a chatter leaves my room. If the chatter is myself leave the room gracefully. * @return true to confirm the removal. */ @Override protected boolean onRemoval() { if (ChatMain.getInstance().getAppUUID().toString().equals(getObjectID())) ChatMain.getInstance().leaveRoom(getParent().getObjectID()); else if (!isStealth() && ChatMain.getInstance().isSameRoom(getParent().getObjectID())) System.out.printf("%s left the roomn", getName()); return true; } /** * Filter the destination so that if it is a chat application, it will receive the object only if the user is logged in, * and if the room the current chatter is in is an adult room, only if the user of the destination application is adult. * Destinations other than chat applications will receive the object with no filtering. * @param appUUID application UUID. * @param appName application name. * @param userID n/a * @param remoteAddress remote address of the destination application. * @param appParams application parameter map as set by Main.getAppParams() of the filtered application. If the destination * is a chat application the map is not null and it contains a mapping of the "state" key to any of * "logout", "login" or "adult". The latter means the user is logged in as an adult. * @return true to approve distribution to this application, false to deny it. */ @Override protected boolean filterDestination(UUID appUUID, String appName, String userID, String remoteAddress, Map<String, String> appParams ) { if (appParams == null) return true; String state = appParams.get("state"); if (state == null) return true; switch(state) { case "logout": return false; case "login": return !((ChatRoom)getParent()).isAdult(); case "adult": return true; default: return false; } } }
The added code is the override of filterDestination() (line 50), which filters
objects according to the mapping of the “state”
key in the given appParams
parameter
and the value of the adult
property
of the object.
You can now understand the part that we asked you above to ignore
when we discussed the ChatRoomProducer
class of the Room Producer
application. It implements filterDestination()
in a similar way for ChatRoom
objects
that the application produces. All done!
More about data integrity
As was mentioned above, Data Object Reset is a procedure that takes place when a consumer of a certain object type detects that some objects are missing. This is the obvious case when an application starts up, and may also happen in situations such as interrupted communication, data loss due to network congestion etc. For an in-depth discussion of this topic, see the blog post Lean and Mean – Under the Spiderwiz hood.
You have seen so far that the Spiderwiz framework is strongly event-driven. In the next lesson we will talk about synchronous vs. asynchronous event handling.