Пример #1
0
 @OnWebSocketMessage
 public void onText(Session session, String message) {
   if (session.isOpen()) {
     System.out.printf("Echoing back message [%s]%n", message);
     // echo the message back
     session.getRemote().sendString(message, null);
   }
 }
Пример #2
0
 @Override
 public boolean isConnected() {
   return connection != null ? connection.isOpen() : false;
 }
Пример #3
0
 @Override
 public boolean isOpen() {
   return webSocketConnection.isOpen();
 }
Пример #4
0
    /**
     * In order to implement a file watcher, we loop forever ensuring requesting to take the next
     * item from the file watchers queue.
     */
    @Override
    public void run() {

      final Path path = Paths.get(spath);
      try {
        // We wait until the file we are told to monitor exists.
        while (!Files.exists(path)) {
          Thread.sleep(200);
        }

        // get the first event before looping
        WatchKey key = null;
        while (session.isOpen() && connected && (key = watcher.take()) != null) {

          try {
            if (!Files.exists(path)) continue;

            for (WatchEvent<?> event : key.pollEvents()) {

              if (!Files.exists(path)) continue;

              Path epath = (Path) event.context();
              if (!Files.isDirectory(path) && !path.endsWith(epath)) continue;

              try {
                // Data has changed, read its shape and publish the event using a web socket.
                final IDataHolder holder =
                    ServiceHolder.getLoaderService().getData(spath, new IMonitor.Stub());
                if (holder == null) continue; // We do not stop if the loader got nothing.

                final ILazyDataset lz =
                    sdataset != null && !"".equals(sdataset)
                        ? holder.getLazyDataset(sdataset)
                        : holder.getLazyDataset(0);
                if (lz == null) continue; // We do not stop if the loader got nothing.

                if (lz instanceof IDynamicDataset) {
                  ((IDynamicDataset) lz).refreshShape();
                }

                if (writing) {
                  ServiceHolder.getLoaderService().clearSoftReferenceCache(spath);
                }
                final DataEvent evt = new DataEvent(lz.getName(), lz.getShape());
                evt.setFilePath(spath);

                // We manually JSON the object because we
                // do not want a dependency and object simple
                String json = evt.encode();
                session.getRemote().sendString(json);
                if (diagInfo != null) diagInfo.record("JSON Send", json);

              } catch (HDF5FunctionArgumentException h5error) {
                // This happens sometimes when the file is not ready to read.
                logger.trace("Path might not be ready to read " + path);
                continue;

              } catch (Exception ne) {
                logger.error("Exception getting data from " + path);
                continue;
              }
              break;
            }

          } finally {
            key.reset();
          }
        }

      } catch (Exception e) {
        logger.error("Exception monitoring " + path, e);
        if (session.isOpen()) session.close(403, e.getMessage());

      } finally {
        if (diagInfo != null) diagInfo.record("Close Thread", Thread.currentThread().getName());
        try {
          watcher.close();
        } catch (IOException e) {
          logger.error("Error closing watcher", e);
        }
      }
    }