createWriter() ouput based on time of the day

edited December 2017 in Library Questions

Hi I'm using the carnivore library/code to get some data of my network. I want to analyse and read as much packets as possible.

For the moment I'm troubled with the ascii convertion and saving the output of the console log.

I've used the createWriter() to log the console to a file. This only stops after hitting a key.

How could I turn this into a daily or maybe hourly 'save to file' happening. So I have multiple files with shorter amounts of code in it?

Answers

  • I think you could do something like this:

    int prevHour = hour();
    
    void setup() {
    }
    
    void draw() {
      if (prevHour != hour()) {
        String date = hour() + ":" + day() + "-" + month() + "-" + year();
        createWriter(date + ".txt");
        prevHour = hour();
        //here you want to clear your array or other date type your using to save the console log so it only saves what happend that hour
      }
    
      //do your other stuff
    }
    

    If I understood your question correct this should do it.

  • edited December 2017

    Ok I thought this was going to be easier ^^

    Umh I followed the createWriter and joined your lines adapted to what I want to ouput.

    No file is created when I start the sketch?

    Here's the complete small code

    import org.rsg.carnivore.*;
    import org.rsg.lib.Log;
    
    CarnivoreP5 c;
    
    int prevHour = hour();
    PrintWriter  output;
    
    void setup(){
      size(600, 400);
      background(255);
      Log.setDebug(true); // Uncomment for verbose mode
      c = new CarnivoreP5(this); 
      //c.setVolumeLimit(4); //limit the output volume (optional)
      //c.setShouldSkipUDP(true); //tcp packets only (optional)
    }
    
    void draw(){
      if (prevHour != hour()) {
        String date = hour () + ":" + day() + "-" + month() + "-" + year();
        ouput = createWriter(date + ".txt");
        prevHour = hour();
        output.flush();
        output.close();
    }
    }
    // Called each time a new packet arrives
    void packetEvent(CarnivorePacket p){
      println("(" + p.strTransportProtocol + " packet) " + p.senderSocket() + " > " + p.receiverSocket());
      //println("Payload: " + p.ascii());
      //println("---------------------------\n");
    }
    
  • Update!!! I get a file every hour but it's empt...

  • edited December 2017

    @Layzfat you made a typo in your code at line 21 you use ouput = createWriter(date + ".txt"); instead of output = createWriter(date + ".txt");.

    I've never used printWriter but I think your files are empty because at line 29 you use println, Shouldn't it be output.println?

  • Hi, my bad copied the wrong sketch. The output was there. I removed the println because of this error before trying to 'compile'.

    The global variable "println" does not exist .

    Though the example doesn't make a variable of the println.. and I even don't think that's even possible/allowed..

  • edited December 2017

    @Lazyfat could you post the whole code? Since the code you posted before with my improvments runs just fine, altough Carnivore doesn't detect a network card so It creates empty logs

  • @schotsl I did gave you the full code, you have to download the carnivore library in the library import menu. Otherwise it doesn't work.

  • @Lazyfat I've downloaded the carnivore library, Try this code (I think you made a type somewhere)

    import org.rsg.carnivore.*;
    import org.rsg.lib.Log;
    
    CarnivoreP5 c;
    
    int prevHour = hour();
    PrintWriter  output;
    
    void setup() {
      size(600, 400);
      background(255);
      Log.setDebug(true); 
      c = new CarnivoreP5(this); 
    }
    
    void draw() {
      if (prevHour != hour()) {
        String date = hour () + ":" + day() + "-" + month() + "-" + year();
        output = createWriter(date + ".txt");
        prevHour = hour();
        output.flush();
        output.close();
      }
    }
    
    void packetEvent(CarnivorePacket p) {
      output.println("(" + p.strTransportProtocol + " packet) " + p.senderSocket() + " > " + p.receiverSocket());
    }
    

    Cause this works for me

  • Hi, I've copied and it's running, how did you test if the output files is generated with text. (change the clock?)

  • You can just replace every hour() with minute() then it will save every minute so you just have to wait a minute. Although changing your clock would also probably work.

  • What? I have copied the same code and added these two lines, just println?

    And it doesn't work, empty files.

    import org.rsg.carnivore.*;
    import org.rsg.lib.Log;
    
    CarnivoreP5 c;
    
    int prevHour = minute();
    PrintWriter output;
    
    void setup() {
      size(600, 400);
      background(255);
      Log.setDebug(true); 
      c = new CarnivoreP5(this); 
    }
    
    void draw() {
      if (prevHour != minute()) {
        String date = minute() + ":" + day() + "-" + month() + "-" + year();
        output = createWriter(date + ".txt");
        prevHour = minute();
        output.flush();
        output.close();
      }
    }
    
    void packetEvent(CarnivorePacket p){
      println("(" + p.strTransportProtocol + " packet) " + p.senderSocket() + " > " + p.receiverSocket());
      println("Payload: " + p.ascii());
      println("---------------------------\n");
    }
    
  • Could this be that I run it on a raspberry pi? I'll try on my laptop tomorrow to double check it.

  • @layzfat once again replace every println with output.println so println("---------------------------\n"); would beccome output.println("---------------------------\n");

  • @schotsl I think there is more at hand, because when I change all println to output.println I get this error. I went over the code multiple times and don't see anything missing or no typo.

    ERROR java.lang.reflect.InvocationTargetException at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.rsg.carnivore.CarnivoreP5.newCarnivorePacket(CarnivoreP5.java:91) at org.rsg.carnivore.Core.dispatch(Core.java:230) at org.rsg.carnivore.PacketCacheThread.run(PacketCacheThread.java:37) Caused by: java.lang.NullPointerException at sketch_171213a.packetEvent(sketch_171213a.java:46) ... 6 more

    COMPLETE CODE

    import org.rsg.carnivore.*;
    import org.rsg.lib.Log;
    
    CarnivoreP5 c;
    
    int prevHour = minute();
    PrintWriter output;
    
    void setup() {
      size(600, 400);
      background(255);
      Log.setDebug(true); 
      c = new CarnivoreP5(this); 
    }
    
    void draw() {
      if (prevHour != minute()) {
        String date = minute() + ":" + day() + "-" + month() + "-" + year();
        output = createWriter(date + ".txt");
        prevHour = minute();
        output.flush();
        output.close();
      }
    }
    
    void packetEvent(CarnivorePacket p){
      output.println("(" + p.strTransportProtocol + " packet) " + p.senderSocket() + " > " + p.receiverSocket());
      output.println("Payload: " + p.ascii());
      output.println("---------------------------\n");
    }
    

    I tested it on the raspberry pi and on my mac os. The codes run without the output.println but hangs when I add it.

  • String date = hour () + ":" + day() + "-" + month() + "-" + year();
    

    be careful with this - windows doesn't like : in filenames.

    i would also stick with just YYDDMMHH format because that way the files sort more sensibly. java's SimpleDateFormat is probably better than using hour() etc because hour() doesn't pad single digit hours and that will also cause confusion. (you can use nf() though)

  • @koogs, care to show an example? :) Or is this a library? I'm not at home, will respond tonight if I found more and get it to work.

  • Could this be that I run it on a raspberry pi? I'll try on my laptop tomorrow to double check it.

    Were you running this on a Raspberry-pi? Make sure you do your testing in a laptop. When it is working there, then you can switch to another platform. However, I am not sure if carnivore will work in your raspberry-pi... No experience in that department.

    Notice in your post in Dec 13, you are creating your writer in draw(). That is a no no, like in don't do this in draw(). Instead, move these two lines to setup():

    String date = minute() + ":" + day() + "-" + month() + "-" + year();
    output = createWriter(date + ".txt");
    

    Also, you close your writer handle in a controlled manner meaning after a specific defined action. An example is illustrated in the reference: https://processing.org/reference/createWriter_.html so yes, do not close your output handle 30 times per second as you are doing right now.

    After you have your sketch working in your laptop, then you can start thinking in migrating to raspberry-pi. This will call for creating another post to target that question (in my opinion).

    Kf

Sign In or Register to comment.