/*
*
* Copyright (c) 2000 Scott Oaks and Henry Wong. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and
* without fee is hereby granted.
*
* This sample source code is provided for example only,
* on an unsupported, as-is basis.
*
* AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). AUTHOR
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import net.jini.core.lease.*;
import net.jini.core.event.*;
import com.sun.jini.lease.landlord.*;
import com.sun.jini.thread.*;
public class ServerDelivery {
private Remote source; // Owner of this delivery object
private ServerLandlord lord; // The landlord that manages leases
private Vector leases; // Event-specific leases
private long seqnum = 0;
private TaskManager pool;
// The client-specific session data for the event leases
// holds the listener and the callback data
private class SessionData {
public RemoteEventListener listener;
public MarshalledObject key;
}
// This class is used to run the delivery of the event in a separate
// thread, one managed by the TaskManager pool.
private class CallbackTask implements TaskManager.Task {
RemoteEventListener listener;
RemoteEvent re;
Lease lease;
CallbackTask(RemoteEventListener rel, RemoteEvent re, Lease l) {
this.re = re;
this.listener = rel;
this.lease = l;
}
public void run() {
try {
listener.notify(re);
} catch (Exception e) {
// We could retry later, but we'll just drop it.
}
}
public boolean runAfter(List tasks, int size) {
return false;
}
}
public ServerDelivery(Remote source, ServerLandlord ll) {
this.source = source;
lord = ll;
leases = new Vector();
pool = new TaskManager();
}
public synchronized EventRegistration addListener(RemoteEventListener l, long duration, MarshalledObject key) {
SessionData sd = new SessionData();
sd.listener = l;
sd.key = key;
Lease lease = lord.newLease(sd, duration);
leases.addElement(lease);
return new EventRegistration(ConvertEvent.ID, source, lease, seqnum);
}
// Deliver an event to all listeners
public void deliver(int value) {
long seq;
synchronized(this) {
seq = seqnum++; // Deliver request number
}
if (leases.isEmpty())
return;
lord.expireLeases(); // Clean up the leases first
Object[] allLeases = leases.toArray();
for (int i = 0; i < allLeases.length; i++) {
Lease l = (Lease) allLeases[i];
SessionData obj = (SessionData) lord.getSessionData(l);
if (obj == null) {
leases.remove(l);
} else {
deliverEvent(obj, seq, value, l);
}
}
}
// Actually send the event
private void deliverEvent(SessionData data, long seq, int value, Lease lease) {
ConvertEvent event = new ConvertEvent(source, value, seq, data.key);
pool.add(new CallbackTask(data.listener, event, lease));
}
}
|