FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   carnivore question
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: carnivore question  (Read 945 times)
diriccio


carnivore question
« on: Oct 8th, 2004, 9:57pm »

hi!  
 
i thought of playing a bit with carnivore for processing, and i was wondering if there's a way to make void loop() not dependent on the data output from carnivore - if i set output to 1 or 2 packets per sec, other looped functions get accordingly slow as well:
 
example:
http://www.strg-n.com/p55/test_carnivore.pde
 
related thread:
http://processing.org/discourse/yabb/board_Tools_action_display__num_1080842851.html
 
please please help me!
 
diriccio


Re: carnivore question
« Reply #1 on: Oct 13th, 2004, 9:18pm »

is this post wrong here?
hello?
 
fjen

WWW
Re: carnivore question
« Reply #2 on: Oct 14th, 2004, 3:01pm »

no it's not ...
 
you might wanna have carnivore run on a different thread ... although it will introduce some other problems it solves the speed thing (don't have carnivore ... so code's not checked):
 
threads at sun:
http://java.sun.com/docs/books/tutorial/essential/threads/
 
Code:
// carnivore
 
Socket socket;
BufferedReader packetreader;
String packet;
 
int ln;
carnivoreChecker carn;
ball b1 = new ball(300,300);
 
float targetX, targetY, targetR;
 
void setup() {
  framerate(25);
  size(600, 400);
  textFont(loadFont("fonts/Univers45.vlw.gz"), 14);
  try {
    socket = new Socket("127.0.0.1", 6667);
    packetreader = new BufferedReader( new InputStreamReader(socket.getInputStream()) );
  } catch(IOException e) { println(e); }
  carn = new carnivoreChecker();
  carn.start();
}
 
void loop(){
   background(0,0,0);
   carn.draw();
   b1.update();
}
 
void mousePressed(){
    b1.myX = ln;
    b1.myY = ln;
}
 
class carnivoreChecker
extends Thread {
 
   public boolean running = false;
 
   carnivoreChecker(){
     targetX = int(random(0, width));
     targetY = int(random(0, height));
   }
   
   public void start ()
   {
     this.running = true;
     super.start();
   }
 
   public void run ()
   {
     while (running){
     this.update();
     try { sleep((long)(1000/25.0)); } // give system some air ... //
     catch (Exception e) {}
     //println("re-run carnivore");
     }
   }
 
   void update(){
     if (packetreader != null) {
  try {
  packet = packetreader.readLine();
  //println("carnivore packet: " + packet);
   
 } catch(IOException e) { println(e);}
   //b1.myX = ln;
 }
    }
     
    void draw()
    {
 if (packet != "" && packet != null) {
  text("data from Carnivore", 10, 30);
  fill(50,50,50);
  text(packet, 10, 50);
  String list[] = splitStrings(packet);
  text(list[1], 10, 90);
  text(list[3], 10, 130);
  ln = packet.length();
  text("packet size:",10,190);
  text(ln, 100, 190);
 }
    }
 
   public void quit()
   {
     this.running = false;
     super.stop();
   }
}
 
  class ball{
    float myX, myY, distanceX,distanceY;
    int originX, originY;
 
    ball(int x,int y){
 originX = 0;
 originY = 0;
 myX = x;
 myY = y;
    }
    void update(){
 distanceX = originX-myX;
 distanceY = originY-myY;
 myX += distanceX/20;
 myY += distanceY/10;
 
 stroke(255);
 point (myX,myY);
    }
 
  }
 
public void stop()
{
   carn.quit();
}
 
diriccio


Re: carnivore question
« Reply #3 on: Oct 15th, 2004, 12:54am »

great! works for me at the moment. thanks for the link on threads as well!
 
Pages: 1 

« Previous topic | Next topic »