/**
   * Sends a Spine message. This is a wrapper round the Transmitter thread and registers the message
   * with the retry mechanism if the contract properties require. It will also, for asynchronous
   * messages, start the listener if it is not already running.
   *
   * @param s Concrete instance of Sendable, encapsulating the message to send.
   * @param c SDS details of recipient
   * @throws Exception if there was a Connection Manager boot exception, or if starting any required
   *     listener fails,
   */
  public void send(Sendable s, SdsTransmissionDetails c) throws Exception {
    // Note: check this here so getInstance() doesn't have to throw any
    // exception - that means that we don't have to catch them in the
    // Transmitter, which can just log if anything goes wrong with its
    // own processing.
    //
    if (bootException != null) throw bootException;

    if (!c.isSynchronous()) {
      listen();
      if ((s.getType() != Sendable.ACK) && (c.getDuplicateElimination().contentEquals("always"))) {
        synchronized (LOGSOURCE) {
          if (timer == null) {
            timer = new ScheduledThreadPoolExecutor(TIMER_THREAD_POOL_SIZE);
            RetryProcessor rp = new RetryProcessor();
            timer.scheduleAtFixedRate(
                rp, retryCheckPeriod, retryCheckPeriod, TimeUnit.MILLISECONDS);
          }
        }
        if (!requests.containsKey(s.getMessageId())) {
          requests.put(s.getMessageId(), s);
        }
      }
    }
    Transmitter t = new Transmitter(s);
    t.start();
  }
Пример #2
0
  public boolean connect() {
    if (transmitter != null || targetRover == null) {
      return false;
    }

    Transmitter newTransmitter = new Transmitter(this, communicator, targetRover);
    synchronized (listeners) {
      for (LinkListener listener : listeners) {
        listener.connectStarted(this);
      }
    }
    if (newTransmitter.connect()) {
      transmitter = newTransmitter;
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.connectCompleted(this);
        }
      }
      return true;
    } else {
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.connectFailed(this);
        }
      }
      return false;
    }
  }
 @Override
 public void send(Transmitter transmitter) {
   Object[] expected = expectations.remove();
   assertEquals(expected[0], Method.SEND);
   assertTrue(
       expected[1] + " vs. " + transmitter.takeMessage().getDelta(),
       CcTestingUtils.deltasAreEqual(
           (WaveletDelta) expected[1], transmitter.takeMessage().getDelta()));
 }
Пример #4
0
  /** Gets the default transmitter and receiver, and then links them. */
  private void linkTransmitterToReceiver() {
    try {
      // Set up the sequencer (including its tempo)
      sequencer.open();
      sequencer.setSequence(sequence);
      sequencer.setTempoInBPM(song.getBPM());

      // Get the system's default synthesizer and set that up, too.
      Synthesizer synth = MidiSystem.getSynthesizer();
      synth.open();

      // Get the receiver and transmitter to use and set those up.
      Receiver receiver = synth.getReceiver();
      Transmitter transmitter = sequencer.getTransmitter();
      transmitter.setReceiver(receiver);
    } catch (Exception ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }
  }
Пример #5
0
 /**
  * The constructor for the <code> Transmission </code> class creates a new transmission with
  * several properties like start and end times, first and last bit to be transmitted and the
  * data itself.
  *
  * @param o Transmitter object
  * @param pow Power for the transmission
  * @param freq the frequency for the transmission
  */
 protected Transmission(Transmitter o, double pow, double freq) {
   origin = o;
   power = pow;
   Pt = pow;
   f = freq;
   start = o.clock.getCount();
   end = Long.MAX_VALUE;
   long l = start + o.leadCycles;
   firstBit = origin.getBitNum(l);
   lastBit = Long.MAX_VALUE;
   data = new byte[Arithmetic.roundup(o.medium.maxLength, BYTE_SIZE)];
 }
Пример #6
0
 public void stop() {
   transmitter.stop();
   receiver.stop();
 }
Пример #7
0
 public boolean isConnected() {
   return transmitter != null && transmitter.isConnected();
 }
Пример #8
0
  @Override
  public void run() {
    try {
      communicator = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.bluetoothInitialized(this);
        }
      }
    } catch (Throwable t) {
      t.printStackTrace();
      synchronized (listeners) {
        for (LinkListener listener : listeners) {
          listener.bluetoothFailed(this);
        }
      }
    }

    long lastSearchTime = 0;
    while (!stopRequest) {

      long currentTime = System.currentTimeMillis();

      if (!isConnected()
          && (availableRovers == null
              || (availableRovers.length == 0 && currentTime - lastSearchTime > 5000))) {
        try {
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchStarted(this);
            }
          }
          availableRovers = communicator.search(null, NXTCommFactory.BLUETOOTH);
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchCompleted(this);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
          synchronized (listeners) {
            for (LinkListener listener : listeners) {
              listener.searchFailed(this);
            }
          }
        }
        lastSearchTime = System.currentTimeMillis();
      }

      if (transmitter != null && !transmitter.isConnected()) {
        transmitter = null;
        synchronized (messagesToSend) {
          messagesToSend.clear();
        }
        synchronized (listeners) {
          for (LinkListener listener : listeners) {
            listener.disconnected(this);
          }
        }
      }

      if (isConnected()) {
        transmitter.encodeMessage(new KeepaliveMessage());

        while (messagesToSend.size() > 0) {
          Message message = null;
          synchronized (messagesToSend) {
            message = (Message) messagesToSend.remove(0);
          }
          transmitter.encodeMessage(message);
        }

        // Check for connection timeout.
        if (transmitter.getLastReceiveTime() != 0
            && System.currentTimeMillis() - transmitter.getLastReceiveTime() > 5000) {
          transmitter.disconnect();
        }
      }

      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
Пример #9
0
 public void disconnect() {
   if (transmitter != null && transmitter.isConnected()) {
     transmitter.disconnect();
   }
 }