Package org.graphstream.stream.thread

Examples of org.graphstream.stream.thread.ThreadProxyPipe


    // graph only listen at attributes since we do not intend to add
    // elements directly in the
    // graphic graph.

    Graph main = new MultiGraph("main");
    ThreadProxyPipe toGraphic = new ThreadProxyPipe();
    toGraphic.init(main);
   
    InTheSwingThread viewerThread = new InTheSwingThread(toGraphic);
    ThreadProxyPipe toMain = viewerThread.getProxy();

    toMain.addAttributeSink(main); // Get the graphic graph proxy.

    // Now launch the graphic graph in the Swing thread using a Swing Timer.

    viewerThread.start();

    // We modify the graph in the main thread.

    Node A = main.addNode("A");
    Node B = main.addNode("B");
    Node C = main.addNode("C");
    main.addEdge("AB", "A", "B");
    main.addEdge("BC", "B", "C");
    main.addEdge("CA", "C", "A");

    SpriteManager sman = new SpriteManager(main);
    Sprite S1 = sman.addSprite("S1");
    Sprite S2 = sman.addSprite("S2");
    Sprite S3 = sman.addSprite("S3");

    S3.setPosition(1, 2, 2);
    S3.setPosition(2, 3, 2);
    S3.setPosition(3, 2, 1);

    A.addAttribute("ui.foo", "bar");
    B.addAttribute("ui.bar", "foo");
    C.addAttribute("truc"); // Not prefixed by UI, will not pass.
    S1.addAttribute("ui.foo", "bar");
    main.stepBegins(1);

    toMain.pump();

    // We ask the Swing thread to modify the graphic graph.

    main.stepBegins(2);
    main.addAttribute("ui.EQUIP"); // Remember GraphicGraph filters
                    // attributes.

    // Wait and stop.

    toMain.pump();
    sleep(1000);
    toMain.pump();

    main.addAttribute("ui.STOP");

    toMain.pump();
    sleep(1000);
    toMain.pump();

    // ****************************************************************************************
    // Now we can begin the real test. We ensure the timer in the Swing
    // graph stopped and check
    // If the two graphs (main and graphic) synchronized correctly.
View Full Code Here


      System.err.println(e1.toString());
    } catch (IOException e1) {
      System.err.println(e1.toString());
    }

    ThreadProxyPipe pipe = net.getDefaultStream();

    pipe.addSink(new SinkAdapter() {
      public void graphAttributeAdded(String sourceId, long timeId,
          String attribute, Object value) {
        validate(attribute, value);
      }
      public void graphAttributeChanged(String sourceId, long timeId,
          String attribute, Object oldValue, Object newValue) {
        validate(attribute, newValue);
      }
      private void validate(String attribute, Object value) {
        String valueType = null;
        Class<?> valueClass = value.getClass();
        boolean isArray = valueClass.isArray();
        if (isArray) {
          valueClass = ((Object[]) value)[0].getClass();
        }
        if (valueClass.equals(Boolean.class)) {
          if (isArray) {
            valueType = "booleanArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Boolean[]) value));

          } else {
            valueType = "boolean";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Byte.class)) {
          if (isArray) {
            valueType = "byteArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Byte[]) value));
          } else {
            valueType = "byte";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Short.class)) {
          if (isArray) {
            valueType = "shortArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Short[]) value));
          } else {
            valueType = "short";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Integer.class)) {
          if (isArray) {
            valueType = "intArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Integer[]) value));
          } else {
            valueType = "int";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Long.class)) {
          if (isArray) {
            valueType = "longArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Long[]) value));
          } else {
            valueType = "long";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Float.class)) {
          if (isArray) {
            valueType = "floatArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Float[]) value));
          } else {
            valueType = "float";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Double.class)) {
          if (isArray) {
            valueType = "doubleArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Double[]) value));
          } else {
            valueType = "double";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(String.class)) {
          if (isArray) {
            valueType = "typeArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Boolean[]) value));
          } else {
            valueType = "string";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        }

        System.err.println(valueType.equals(attribute));

      }

    });
   
   
   
    while(true){
      pipe.pump();
      Thread.sleep(100);     
    }
  }
View Full Code Here

    NetStreamReceiver net = new NetStreamReceiver("localhost",2001,false);
   
    net.setUnpacker(new  Base64Unpacker());
   
    // - received events end up in the "default" pipe
    ThreadProxyPipe pipe = net.getStream("default");
    // - plug the pipe to the sink of the graph
    pipe.addSink(g);
    // -The receiver pro-actively checks for events on the ThreadProxyPipe
    while (true) {
      pipe.pump();
      Thread.sleep(100);
    }
  }
View Full Code Here

    } catch (IOException e1) {
      fail(e1.toString());
    }

    try {
      ThreadProxyPipe pipe = net.getDefaultStream();

      pipe.addSink(new SinkAdapter() {

        public void graphAttributeAdded(String sourceId, long timeId,
            String attribute, Object value) {
        }
      });

      Thread t = new Thread() {

        @Override
        public void run() {

          Graph g = new MultiGraph("G", false, true);
          NetStreamSender nsc = null;
          try {
            nsc = new NetStreamSender("localhost", 2000);
          } catch (UnknownHostException e1) {
            error(e1.toString());
            return;
          } catch (IOException e1) {
            error(e1.toString());
            return;
          }

          g.addSink(nsc);

          g.addAttribute("attribute", "foo");
          g.changeAttribute("attribute", false);
          Edge e = g.addEdge("AB", "A", "B");
          e.addAttribute("attribute", "foo");
          e.changeAttribute("attribute", false);
          Node n = e.getNode0();
          n.addAttribute("attribute", "foo");
          n.changeAttribute("attribute", false);

          try {
            nsc.close();
          } catch (IOException e1) {
          }
        }
      };

      t.start();

      try {
        t.join();
      } catch (InterruptedException e) {
        fail(e.getMessage());
      }

      while (pipe.hasPostRemaining() || net.hasActiveConnections()) {
        pipe.pump();

        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
View Full Code Here

      fail(e1.toString());
    }

    net.setUnpacker(new Base64Unpacker());

    ThreadProxyPipe pipe = net.getDefaultStream();

    pipe.addSink(new SinkAdapter() {

      public void graphAttributeAdded(String sourceId, long timeId,
          String attribute, Object value) {
        validate(attribute, value);
      }

      public void graphAttributeChanged(String sourceId, long timeId,
          String attribute, Object oldValue, Object newValue) {
        validate(attribute, newValue);
      }

      private void validate(String attribute, Object value) {

        String valueType = null;
        Class<?> valueClass = value.getClass();
        boolean isArray = valueClass.isArray();
        if (isArray) {
          valueClass = ((Object[]) value)[0].getClass();
        }
        if (valueClass.equals(Boolean.class)) {
          if (isArray) {
            valueType = "booleanArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Boolean[]) value));

          } else {
            valueType = "boolean";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Byte.class)) {
          if (isArray) {
            valueType = "byteArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Byte[]) value));
          } else {
            valueType = "byte";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Short.class)) {
          if (isArray) {
            valueType = "shortArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Short[]) value));
          } else {
            valueType = "short";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Integer.class)) {
          if (isArray) {
            valueType = "intArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Integer[]) value));
          } else {
            valueType = "int";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Long.class)) {
          if (isArray) {
            valueType = "longArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Long[]) value));
          } else {
            valueType = "long";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Float.class)) {
          if (isArray) {
            valueType = "floatArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Float[]) value));
          } else {
            valueType = "float";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(Double.class)) {
          if (isArray) {
            valueType = "doubleArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Double[]) value));
          } else {
            valueType = "double";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        } else if (valueClass.equals(String.class)) {
          if (isArray) {
            valueType = "typeArray";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute,
                Arrays.toString((Boolean[]) value));
          } else {
            valueType = "string";
            System.out.printf("found a %s for attribute %s=%s%n",
                valueType, attribute, value.toString());
          }
        }

        assertTrue(valueType.equals(attribute));

      }

    });

    Thread t = new Thread() {

      @Override
      public void run() {

        Graph g = new MultiGraph("G");
        NetStreamSender nsc = null;
        try {
          nsc = new NetStreamSender("localhost", 2001);
        } catch (UnknownHostException e1) {
          error(e1.toString());
          return;
        } catch (IOException e1) {
          error(e1.toString());
          return;
        }

        nsc.setPacker(new Base64Packer());

        g.addSink(nsc);

        g.addAttribute("intArray", 0, Integer.MAX_VALUE,
            Integer.MIN_VALUE);
        g.addAttribute("floatArray", 0f, Float.MAX_VALUE,
            Float.MIN_VALUE);
        g.addAttribute("doubleArray", 0.0, Double.MAX_VALUE,
            Double.MIN_VALUE);
        g.addAttribute("shortArray", (short) 0, Short.MAX_VALUE,
            Short.MIN_VALUE);
        g.addAttribute("longArray", 0L, Long.MAX_VALUE, Long.MIN_VALUE);
        g.addAttribute("byteArray", (byte) 0, Byte.MAX_VALUE,
            Byte.MIN_VALUE);
        g.addAttribute("booleanArray", true, false);
        // Object[] three = {new Short((short) 3),new Long(3L),"3"};
        // g.addAttribute("typeArray","one", 2 , three);
        g.addAttribute("int", 1);
        g.addAttribute("float", 1f);
        g.addAttribute("double", 1.0);
        g.addAttribute("short", (short) 0);
        g.addAttribute("long", 1L);
        g.addAttribute("byte", (byte) 0);
        g.addAttribute("boolean", true);
        g.addAttribute("string", "true");

        try {
          nsc.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    };

    t.start();

    try {
      t.join();
    } catch (InterruptedException e) {
      fail(e.getMessage());
    }

    while (pipe.hasPostRemaining() || net.hasActiveConnections()) {
      pipe.pump();

      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
      }
View Full Code Here

      fail(e1.toString());
    } catch (IOException e1) {
      fail(e1.toString());
    }

    ThreadProxyPipe pipe1 = net.getStream("G1");
    ThreadProxyPipe pipe2 = net.getStream("G2");

    pipe1.addSink(g1);
    pipe2.addSink(g2);

    Thread t1 = launchClient(2002, "G1", "0");
    Thread t2 = launchClient(2002, "G1", "1");
    Thread t3 = launchClient(2002, "G2", "0");

    try {
      t1.join();
      t2.join();
      t3.join();
    } catch (InterruptedException e) {
      fail(e.getMessage());
    }

    while (pipe1.hasPostRemaining() || pipe2.hasPostRemaining()
        || net.hasActiveConnections()) {
      pipe1.pump();
      pipe2.pump();

      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
      }
View Full Code Here

      fail(e1.toString());
    } catch (IOException e1) {
      fail(e1.toString());
    }

    ThreadProxyPipe pipe = net.getDefaultStream();

    pipe.addSink(g1);

    g1.addSink(new Sink() {

      public void graphAttributeAdded(String sourceId, long timeId,
          String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("graphAttribute", attribute);
      }

      public void graphAttributeChanged(String sourceId, long timeId,
          String attribute, Object oldValue, Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("graphAttribute", attribute);
      }

      public void graphAttributeRemoved(String sourceId, long timeId,
          String attribute) {
        assertEquals("graphAttribute", attribute);
      }

      public void nodeAttributeAdded(String sourceId, long timeId,
          String nodeId, String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("nodeAttribute", attribute);
      }

      public void nodeAttributeChanged(String sourceId, long timeId,
          String nodeId, String attribute, Object oldValue,
          Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("nodeAttribute", attribute);
      }

      public void nodeAttributeRemoved(String sourceId, long timeId,
          String nodeId, String attribute) {
        assertEquals("nodeAttribute", attribute);
      }

      public void edgeAttributeAdded(String sourceId, long timeId,
          String edgeId, String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("edgeAttribute", attribute);
      }

      public void edgeAttributeChanged(String sourceId, long timeId,
          String edgeId, String attribute, Object oldValue,
          Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("edgeAttribute", attribute);
      }

      public void edgeAttributeRemoved(String sourceId, long timeId,
          String edgeId, String attribute) {
        assertEquals("edgeAttribute", attribute);
      }

      public void nodeAdded(String sourceId, long timeId, String nodeId) {
        assertTrue("node0".equals(nodeId) || "node1".equals(nodeId));
      }

      public void nodeRemoved(String sourceId, long timeId, String nodeId) {
        assertTrue("node0".equals(nodeId) || "node1".equals(nodeId));
      }

      public void edgeAdded(String sourceId, long timeId, String edgeId,
          String fromNodeId, String toNodeId, boolean directed) {
        assertEquals("edge", edgeId);
        assertEquals("node0", fromNodeId);
        assertEquals("node1", toNodeId);
        assertEquals(true, directed);
      }

      public void edgeRemoved(String sourceId, long timeId, String edgeId) {
        assertEquals("edge", edgeId);
      }

      public void graphCleared(String sourceId, long timeId) {

      }

      public void stepBegins(String sourceId, long timeId, double step) {
        assertEquals(1.1, step);
      }
    });

    Thread t = new Thread() {

      @Override
      public void run() {

        Graph g = new MultiGraph("G", false, true);

        NetStreamSender nsc = null;

        try {
          nsc = new NetStreamSender("localhost", 2002);
        } catch (UnknownHostException e1) {
          error(e1.toString());
          return;
        } catch (IOException e1) {
          error(e1.toString());
          return;
        }

        g.addSink(nsc);
        Node node0 = g.addNode("node0");
        Edge edge = g.addEdge("edge", "node0", "node1", true);
        node0.addAttribute("nodeAttribute", 0);
        node0.changeAttribute("nodeAttribute", 1);
        node0.removeAttribute("nodeAttribute");
        edge.addAttribute("edgeAttribute", 0);
        edge.changeAttribute("edgeAttribute", 1);
        edge.removeAttribute("edgeAttribute");
        g.addAttribute("graphAttribute", 0);
        g.changeAttribute("graphAttribute", 1);
        g.removeAttribute("graphAttribute");
        g.stepBegins(1.1);
        g.removeEdge("edge");
        g.removeNode("node0");
        g.clear();

        try {
          nsc.close();
        } catch (IOException e1) {
        }
      }
    };

    t.start();

    try {
      t.join();
    } catch (InterruptedException e) {
      fail(e.getMessage());
    }

    while (pipe.hasPostRemaining() || net.hasActiveConnections()) {
      pipe.pump();

      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
      }
View Full Code Here

   
    net.setUnpacker(new Base64Unpacker());
    net.setDebugOn(false);
   
    // - received events end up in the "default" pipe
    ThreadProxyPipe pipe = net.getDefaultStream();
    // - plug the pipe to the sink of the graph
    pipe.addSink(g);

    // ----- The sender side (in another thread) ------
    //
    new Thread() {

      public void run() {
        // - the original graph from which events are generated
        Graph g = new MultiGraph("G");
        // - the sender
        NetStreamSender nsc = null;
        try {
          nsc = new NetStreamSender(2001);
        } catch (UnknownHostException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
       
        nsc.setPacker(new Base64Packer());

       
        // - plug the graph to the sender so that graph events can be
        // sent automatically
        g.addSink(nsc);
        // - generate some events on the client side
        String style = "node{fill-mode:plain;fill-color:#567;size:6px;}";
        g.addAttribute("stylesheet", style);
        g.addAttribute("ui.antialias", true);
        g.addAttribute("layout.stabilization-limit", 0);
        for (int i = 0; i < 5000; i++) {
          g.addNode(i + "");
          if (i > 0) {
            g.addEdge(i + "-" + (i - 1), i + "", (i - 1) + "");
            g.addEdge(i + "--" + (i / 2), i + "", (i / 2) + "");
          }
        }
      }
    }.start();

    // ----- Back to the receiver side -----
    //
    // -The receiver pro-actively checks for events on the ThreadProxyPipe
    while (true) {
      pipe.pump();
      Thread.sleep(100);
    }

  }
View Full Code Here

      fail(e1.toString());
    } catch (IOException e1) {
      fail(e1.toString());
    }

    ThreadProxyPipe pipe = net.getDefaultStream();

    pipe.addSink(new Sink() {
      public void graphAttributeAdded(String sourceId, long timeId,
          String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("graphAttribute", attribute);
      }
     
      public void graphAttributeChanged(String sourceId, long timeId,
          String attribute, Object oldValue, Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("graphAttribute", attribute);
      }
     
      public void graphAttributeRemoved(String sourceId, long timeId,
          String attribute) {
        assertEquals("graphAttribute", attribute);
      }
     
      public void nodeAttributeAdded(String sourceId, long timeId,
          String nodeId, String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("nodeAttribute", attribute);
      }
     
      public void nodeAttributeChanged(String sourceId, long timeId,
          String nodeId, String attribute, Object oldValue,
          Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("nodeAttribute", attribute);
      }
     
      public void nodeAttributeRemoved(String sourceId, long timeId,
          String nodeId, String attribute) {
        assertEquals("nodeAttribute", attribute);
      }
     
      public void edgeAttributeAdded(String sourceId, long timeId,
          String edgeId, String attribute, Object value) {
        assertEquals(0, value);
        assertEquals("edgeAttribute", attribute);
      }
     
      public void edgeAttributeChanged(String sourceId, long timeId,
          String edgeId, String attribute, Object oldValue,
          Object newValue) {
        assertTrue((Integer) newValue == 0 || (Integer) newValue == 1);
        assertEquals("edgeAttribute", attribute);
      }
     
      public void edgeAttributeRemoved(String sourceId, long timeId,
          String edgeId, String attribute) {
        assertEquals("edgeAttribute", attribute);
      }
     
      public void nodeAdded(String sourceId, long timeId, String nodeId) {
        assertTrue("node0".equals(nodeId) || "node1".equals(nodeId));
      }
     
      public void nodeRemoved(String sourceId, long timeId, String nodeId) {
        assertTrue("node0".equals(nodeId) || "node1".equals(nodeId));
      }
     
      public void edgeAdded(String sourceId, long timeId, String edgeId,
          String fromNodeId, String toNodeId, boolean directed) {
        assertEquals("edge", edgeId);
        assertEquals("node0", fromNodeId);
        assertEquals("node1", toNodeId);
        assertEquals(true, directed);
      }
     
      public void edgeRemoved(String sourceId, long timeId, String edgeId) {
        assertEquals("edge", edgeId);
      }
     
      public void graphCleared(String sourceId, long timeId) {
       
      }
     
      public void stepBegins(String sourceId, long timeId, double step) {
        assertEquals(1.1, step);
      }
    });

   


    while(true){
      pipe.pump();
      Thread.sleep(100);     
    }

  }
View Full Code Here

    source.addEdge("CA", "C", "A");

    source.getNode("A").addAttribute("A1", "foo");
    source.getNode("A").addAttribute("A2", "foo");

    ThreadProxyPipe proxy = new ThreadProxyPipe();
    proxy.addSink(target);
    proxy.init(source, true);

    Thread other = new Thread(new AnotherThread(proxy, target) {
      public void run() {
        // The second part of the test starts
        // in this target thread.

        boolean loop = true;

        do {
          proxy.pump();

          if (target.hasAttribute("STOP!"))
            loop = false;
        } while (loop);
      }
View Full Code Here

TOP

Related Classes of org.graphstream.stream.thread.ThreadProxyPipe

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.