We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › p5 and Carnivore PE
Page Index Toggle Pages: 1
p5 and Carnivore PE (Read 965 times)
p5 and Carnivore PE
Jun 22nd, 2005, 12:52am
 
hy!!

i'm working integrating p5 and Carnivore PE (http://www.rhizome.org/carnivore). In the 69th p5's version all work fine but now im gonna try to move my code to 90 version but it does not work. I post below the basic structure of the client. Does any one know wat's wrong???

cheers

G.


code:
Socket socket;
BufferedReader packetreader;
CarnivoreListener CL;
String packet;
String packet_datestamp;
String packet_fromIP;
String packet_fromPort;
String packet_toIP;
String packet_toPort;
   
void setup(){
 size(800, 200);
 textFont(loadFont("Gungsuh.vlw"), 24);

 //start listening to carnivore
 CL = new CarnivoreListener();
 CL.startListening();
}

void loop(){
 background(50,50,50);
 fill(130,130,130);
 text("data from Carnivore", 10, 30);

 // "minivore" packet looks like ":CarnivorePE:13:26:02 192.168.1.101.49178 > 192.168.1.100.3689:"
 //                               carniheader  time     senderIP      port    receiverIP    port
 packet = CL.getPacket();
 fill(200,200,200);
 text(packet, 10, 50);    
 //println("loop: from=" + from + " to=" + to);
}

///////////////////////////////////////////////////////////////////////////
class CarnivoreListener implements Runnable {

   // For connecting to socket
   String Host = "127.0.0.1";
   int Port = 6667;

   // Stream stuff
   Socket s;
   DataInputStream in;
   PrintStream out;
   boolean connected = false;
   boolean stopthread = false;

   // Runs the listener
   Thread runner;

   //Packet stuff
   String _packet = "";

   ////////////////////////////////////////////////////
   // Start the Carni listener with the output panel as param.
   // Set default connection method to Carnivore Server.
   
   public CarnivoreListener() {
   }

   /////////////////////////////////////////////////////
   // Start/stop the listening loop

   public void startListening() {
       // Start the repaint thread
       runner = new Thread(this);
       runner.start();
       stopthread = false;
   }

   public void stopListening() {
       runner = null;
       stopthread = true;        
       disconnectFromServer();
   }


   /////////////////////////////////////////////////////
   // connect to the data source

   public boolean connectToServer() {
       connected = false;


       // default: open Socket
       message("Connecting to host " + Host + ":" + Port + "...");
       try {
               // connect to server socket
               s = new Socket(Host,Port);
               // to read from server
               in = new DataInputStream(s.getInputStream());
               // to send to server
               out = new PrintStream(s.getOutputStream());
               message("Connected to " + Host + ":" + Port);
               connected = true;
           }
           catch (Exception e) {
               message("connect(): " + e);
               connected = false;
       }
       return connected;
   }

   public void disconnectFromServer() {
       message("disconnect");
       try {
           if (out != null) {
               out.println("QUIT");
               out.close();
           }
           if (in != null) {
               in.close();
           }
       }
       catch (IOException e) {
           message("Error when closing inputStream: " + e);
       }
       connected = false;
   }

   public String getPacket() {
   
return _packet;
   }

   /////////////////////////////////////////////////////
   // Thread runs in two states
   // 1) loop until connected to server
   // 2) read lines until thread stops or socket fails
   //    if socket fails, return to state 1.
   //
   public void run() {
       String line;
       connected = false;
       message("run()");
       while (stopthread==false) {
           // Get connection
           while (connected==false && stopthread==false) {
               if (connectToServer() == false) {
                   try{
                       Thread.sleep(60000);  // retry in 60 secs
                   } catch (InterruptedException e) {
                       message("Interrupted: " + e);
                   }
               }
           }
           // Read lines from Carni Server
           try {
               byte bb=0;
               while (connected==true && stopthread==false) {
                   line = in.readLine();
                   if (line == null) {
                       break;
                   }
                   _packet = line;
                   // Yield a little time for repaint operations to work
                   Thread.sleep(10);
               }
           }
           catch (Exception e) {
               message("Error when listening to " + Host + " " + Port + ": " + e);
           }
           finally {
               message("Connection closed by server.");
               disconnectFromServer();
               // will loop back to connectToServer() to retry connection every 10 secs
           }
       }
   }

   public void message(String msg) {
       System.out.println("CarnivoreListener: " + msg);
   }
}


public void stop(){

CL.stopListening();

super.stop();
}
Re: p5 and Carnivore PE
Reply #1 - Jun 22nd, 2005, 6:39pm
 
please don't double post.
Page Index Toggle Pages: 1