Tutorial: Timed Event

Source File: see TimedEvent.java in examples/rooms/.
This tutorial demonstrates how to set up a timed event on the server side in a Room. It is very simple and broadcasts the Room uptime every 10 seconds to all the connected Clients.

Building the Room

First we want to make sure we have a reference to the RoomServices for our Room. We could access it via Services but it's more efficient to not have to do the lookup every time. The reference is passed when the Room is initialized so set up an instance variable and copy the reference in the onRoomInit method.
    private RoomServices m_roomServices = null;
    
    public void onRoomInit(RoomServices roomServices) {
        m_roomServices = roomServices;
    }
Our Room must implement Runnable so we can start a new thread for our timer.

public class TimedEvent implements RoomListener, Runnable { 
Next add an instance variable for the Thread which will be timing our event.

  private Thread m_timer = null;
We want this Thread to start running when the Room is initialized. So start it in onRoomInit.
    public void onRoomInit(RoomServices roomServices) {
        m_roomServices = roomServices;
        
        m_timer = new Thread(this);
        m_timer.start();
    }
Now add the code to the run() method that will actually do the update. It will run as long as the Room is alive. We'll use a UPC method "updateRoomUptime" to represent our message to the Clients. After it does the update have it sleep for 10000 milliseconds.
    public void run() {
        UPCMessage message = new UPCMessage("updateRoomUptime");
        long creationTime = m_roomServices.getCreationTime();
        ArrayList args = new ArrayList();
        args.add("0");
        
        while (!m_roomServices.isDying()) {         
            args.set(0,String.valueOf((new Date()).getTime() - creationTime));    
            message.setArgs(args);
            m_roomServices.broadcastRoom(UPCBuilder.build(message));
        
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                System.out.println("Error in TimedEvent Room.");
            }        
        }
    }
Since we only have 1 UPCMessage and that message only has 1 argument it's more efficient to create the UPCMessage once and then update the argument list with new values.

Finally make sure we clean up the reference to RoomServices when the Room dies.
    public void onRoomDie() {
        m_roomServices = null;
    }

Deploying the Room

Ensure the example_rooms.jar file that came with the Server is in the ./rooms directory.

Edit uconfig.xml as below.
<UNITY>
    <SERVER>
        <SERVER_PORT>9100</SERVER_PORT>
        <ADMIN_PORT>9101</ADMIN_PORT>
        <ADMIN_PASSWORD>password</ADMIN_PASSWORD>
    </SERVER>
    <TYPES>
        <ROOM>
            <ID>TimedEvent</ID>
         <CLASS>org.moock.unity.examples.rooms.TimedEvent</CLASS>
       </ROOM>
    </TYPES>  
    <INSTANCES> 
        <ROOM>
            <ID>timedEvent</ID>
            <TYPE_ID>TimedEvent</TYPE_ID>
        </ROOM>
    </INSTANCES>  
</UNITY>