/*
*
* 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.io.*;
import java.rmi.*;
import java.rmi.server.*;
import net.jini.core.lease.*;
import net.jini.core.event.*;
import net.jini.discovery.*;
import net.jini.lease.*;
import net.jini.event.*;
import com.sun.jini.lease.*;
import com.sun.jini.reliableLog.*;
import com.sun.jini.constants.*;
public class BillingClient extends UnicastRemoteObject
implements RemoteEventListener {
MailboxRegistration mailbox;
LeaseRenewalSet renewalSet;
boolean doneProcessing;
int numRecords = 0;
class BillingClientLogHandler extends LogHandler {
public void snapshot(OutputStream os) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(os);
Object target;
if (mailbox == null)
target = new String();
else target = new MarshalledObject(mailbox);
oos.writeObject(target);
if (renewalSet == null)
target = new String();
else target = new MarshalledObject(renewalSet);
oos.writeObject(target);
}
public void recover(InputStream is) throws Exception {
ObjectInputStream ois = new ObjectInputStream(is);
Object target;
target = ois.readObject();
if (target instanceof MarshalledObject) {
MarshalledObject mo = (MarshalledObject) target;
mailbox = (MailboxRegistration) mo.get();
}
// else it was the dummy string
target = ois.readObject();
if (target instanceof MarshalledObject) {
MarshalledObject mo = (MarshalledObject) target;
renewalSet = (LeaseRenewalSet) mo.get();
}
// else it was the dummy string
}
public void applyUpdate(Object o) {
// we never write updates
}
}
public BillingClient(String logDir) throws RemoteException, IOException {
BillingClientLogHandler bclh = new BillingClientLogHandler();
ReliableLog log = new ReliableLog(logDir, bclh);
log.recover();
if (mailbox == null)
initServices();
renewalSet.renewFor(mailbox.getLease(), TimeConstants.DAYS * 3);
processRecords();
log.snapshot();
System.exit(0);
}
private void initServices() throws IOException, RemoteException {
if (mailbox == null) {
// First time we've run -- find all the services and ask for
// conversion events to be send to us
// Find the event mailbox service and register with it
ServiceFinder sfem = new ServiceFinder(EventMailbox.class);
EventMailbox em = (EventMailbox) sfem.getObject();
mailbox = em.register(Long.MAX_VALUE);
// Find the lease renewal service, and use it to renew the
// mailbox lease. We intend to run every day, but set the
// renewal time to 3 days if we don't run over a weekend.
ServiceFinder sflrs = new ServiceFinder(LeaseRenewalService.class);
LeaseRenewalService lrs = (LeaseRenewalService) sflrs.getObject();
renewalSet = lrs.createLeaseRenewalSet(TimeConstants.DAYS * 3);
// Find the convert service and ask that it send events to the
// mailbox forever (when we cancel the lease renewal set, this
// lease will get cancelled).
ServiceFinder sf = new ServiceFinder(ConvertService.class);
ConvertService cs = (ConvertService) sf.getObject();
EventRegistration er = cs.trackConversions(Long.MAX_VALUE,
mailbox.getListener(), null);
renewalSet.renewFor(er.getLease(), Long.MAX_VALUE);
}
}
// Turn on the mailbox to send us events; they'll be sent to our notify
// method. If 30 seconds elapse without us getting an event from the
// mailbox, we assume that we've drained the mailbox and that we're done.
private void processRecords() {
try {
mailbox.enableDelivery(this);
} catch (RemoteException re) {
System.out.println("Can't enable mailbox delivery");
System.exit(0);
}
doneProcessing = false;
synchronized(this) {
while (!doneProcessing) {
// Set done to true; if its still true after 30 seconds then
// we're done. The notify() method will set it to false, in
// which case we'll try again.
doneProcessing = true;
try {
wait(30 * 1000);
} catch (InterruptedException ie) {}
}
}
System.out.println("Processed " + numRecords + " records");
try {
mailbox.disableDelivery();
} catch (RemoteException re) {
}
}
public synchronized void notify(RemoteEvent re)
throws UnknownEventException {
if (!(re instanceof ConvertEvent))
throw new UnknownEventException("BillingClient");
doneProcessing = false;
ConvertEvent ce = (ConvertEvent) re;
long seq = ce.getSequenceNumber();
int val = ce.getValue();
System.out.println("Convert #" + seq + ": " + val);
numRecords++;
}
public static void main(String[] args) throws Exception {
System.setSecurityManager(new RMISecurityManager());
new BillingClient(args[0]);
}
}
|