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

MyTranslator.java (template)

  import edu.gatech.cc.realjin.umiddle.UMiddleEntityException;
  import edu.gatech.cc.realjin.umiddle.directory.DirectoryException;
  import edu.gatech.cc.realjin.umiddle.entity.ServiceEntity;

  public class MyTranslator extends ServiceEntity{
      private String deviceID;
      private SomeNativeDevice nativeDevice;
      private ReceiverThread thread;
      //private DataInputPort umiddleIn;   //uncomment/modify if this is an outbound translator
      //private DataOutputPort umiddleOut; //uncomment/modify if this is an inbound translator

      public MyTranslator(String devID) 
      throws DirectoryException, UMiddleEntityException {
          deviceID = devID;
      }
    
      protected void init() throws Exception {
          nativeDevice = new SomeNativeDevice(deviceID); //create your own native device object or something
          receiverThread = new ReceiverThread();
          //umiddleIn = addDataInputPort("dataIn", "image/jpg");    //for an outbound translator
          //umiddleOut = addDataOutputPort("dataOut", "image/jpg"); //for an inbound translator
      }

      protected void start() throws Exception {
          receiverThread.setActive(true);
      }

      protected void stop() throws Exception {
          receiverThread.setActive(false);
      }

      class ReceiverThread extends Thread{
          private boolean isActive = false;

          void setActive(boolean b){
              isActive = b;
              if(!isActive && b){
                  isActive = true;
                  start();
              }else if(isActive && !b){
                  isActive = false;
                  interrupt();
              }
          }

          public void run(){
              while(isActive){
                  try{
                      /** Uncomment and modify for outbound translator
                       * Message message = dataIn.receive();      //receive from uMiddle ports
                       * nativeDevice.send(message.getMessage()); //send to the native device
                       **/
 
                      /** Uncomment and modify for inbound translator
                       * byte[] someData = nativeDevice.receive();//receive from the native device
                       * dataOut.send(new Message(someData));     //send to uMiddle ports
                       **/
                  }catch(Exception e){
                       e.printStackTrace();
                  }
              }
          }
      }
  }


Link to this Page