View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

IButtonMapper.java (stage 4)


   0: import edu.gatech.cc.realjin.umiddle.mapper.DeviceMappingPolicy;
   1: import edu.gatech.cc.realjin.umiddle.mapper.Mapper;
   2: import edu.gatech.cc.realjin.umiddle.mapper.MapperException;
   3: import java.util.HashMap;
   4: import java.util.Map;
   5: import org.w3c.dom.Node;
   6: 
   7: public class IButtonMapper extends Mapper implements IButtonListener{
   8:     private Map policies = new HashMap();
   9:
  10:     public IButtonMapper() {
  11:         IButtonReader reader = <the reader>;
  12:         reader.addIButtonListener(this); //this registers iButtonInserted and iButtonPulledOut in this class as hooks.
  13:     }
  14: 
  15:     protected void init() {} //we do nothing here
  16:     protected void stop() {} //we do nothing here
  18: 
  19:     public int map(DeviceMappingPolicy policy) throws MapperException {
  20:         String idOrType = parseIButton(policy.getPolicy());
  21:         policies.add(idOrType, policy);
  22:         return POSTPONED; // meaning that the translator instantiation will be done in the future.
  23:     }
  24: 
  25:     protected String getNamespaceURI() {
  26:         return "//edu.gatech.cc.umiddle.ibutton";
  27:     }
  28:
  29:     private String parseIButton(Node node){
  30:         return <id or type contained in the node>; //use your favarite XML parser
  31:     }
  32:     
  33:     public void iButtonInserted(IButtonDevice button){
  34:         if(this ibutton is already mapped)
  35:             return;
  36:     
  37:         String id = button.getID();
  38:         String type = button.getType();
  39:         DeviceMappingPolicy policy = (DeviceMappingPolicy)policies.get(id);
  40:     
  41:         if(policy == null){    ////we don't have USDL document for this paraticular iButton
  42:             policy = (DeviceMappingPolicy)get(type);
  43:             if(policy == null) ////we don't have USDL document for this iButton type
  44:                 return;        ////don't map this device.
  45:         }
  46:     
  47:         try{
  48:               //export container device entity to uMiddle space
  49:             translator = new DeviceEntity(policy.getName());
  50:             IButtonTranslator t = <create translator for this (id, type)>;
  51:               //add this translator to the container device entity
  52:             dev.addService(dev.getServiceMappingPolicy().getName(), t);
  53:         }catch(Exception e){
  54:             System.err.println("iButtonMapper: failed to map "+id);
  55:             e.printStackTrace();
  56:         }
  57:     }
  58:     
  59:     public void iButtonPulledOut(IButtonDevice button){
  60:         //assuming there is only one Bluedot connected to this PC, the current translator
  61:         //is for this iButton.
  62:         translator.unexport(); //this makes the translator stop operation
  63:     }
  64: }


Link to this Page