public void draw(int[] pixels) {

    frameCount++;

    parent.println("draw!");

    String data = "";

    data += "task.animation.set(leds, 50, ";

    if (isLoggedIn) { // && serverIsReady){

      for (int i = 0; i < pixels.length; i++) {
        String value = Integer.toHexString(pixels[i]);
        value = value.substring(2, value.length());
        data += "#" + value + ",";
      }
      data = data.substring(0, data.length() - 1);
      data += ")\n";

      parent.println(data);
      lumoClient.write(data);

      serverIsReady = false;
    }
  }
Example #2
0
  /**
   * @param parent typically use "this"
   * @param port port used to transfer data
   * @param host when multiple NICs are in use, the ip (or name) to bind from
   */
  public MyServer(PApplet parent, int port, String host) {
    this.parent = parent;
    this.port = port;

    try {
      if (host == null) {
        server = new ServerSocket(this.port);
      } else {
        server = new ServerSocket(this.port, 10, InetAddress.getByName(host));
      }
      // clients = new Vector();
      clients = new Client[10];

      thread = new Thread(this);
      thread.start();

      parent.registerMethod("dispose", this);

      // reflection to check whether host applet has a call for
      // public void serverEvent(MyServer s, Client c);
      // which is called when a new guy connects
      try {
        serverEventMethod =
            parent.getClass().getMethod("serverEvent", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        serverEventMethod = null;
      }

      try {
        clientValidationMethod =
            parent
                .getClass()
                .getMethod("clientValidation", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        clientValidationMethod = null;
      }

    } catch (IOException e) {
      // e.printStackTrace();
      thread = null;
      throw new RuntimeException(e);
      // errorMessage("<init>", e);
    }
  }
  public void clientEvent(Client myClient) {

    String response = myClient.readString();

    if (response != null) parent.println("response: " + response);

    if (response != null && isConnected) {

      if (parent.match(response, "100") != null && !isLoggedIn) {

        parent.println("trying to log in...");
        myClient.write("login(test,test)\n");
      }

      if (parent.match(response, "200") != null && !isLoggedIn) {

        isLoggedIn = true;
        myClient.write("task.set(active,true)\n");
        parent.println("we are logged in!");
      }

      if (parent.match(response, "200") != null && isLoggedIn) {

        serverIsReady = true;
        parent.println("server is now ready for tasks!");
      }
    }
  }
  public void setup(PApplet p) {
    // size(600,600);
    // background(20);
    // frameRate(10);

    this.parent = p;

    // try to make a connection to the LumoServer
    try {
      isConnected = true;
      lumoClient = new Client(parent, "localhost", 7070);
      parent.println("Server Connection acquired");

    } catch (Exception e) {
      isConnected = false;
      parent.println("Server Connection failed");
    }
  }
Example #5
0
 protected void addClient(Client client) {
   if (clientCount == clients.length) {
     clients = (Client[]) PApplet.expand(clients);
   }
   clients[clientCount++] = client;
 }
 public static void main(String[] args) {
   PApplet.main(new String[] {"LumoCommunicator"});
 }