Skip to content

Commit

Permalink
WriteObjectsToConsole moved to JavaFBP 
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaulm committed Jul 7, 2020
1 parent 4c29afd commit ee3c3ae
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
Binary file modified build/distributions/javafbp-4.1.4.tar
Binary file not shown.
Binary file modified build/distributions/javafbp-4.1.4.zip
Binary file not shown.
Binary file modified build/libs/javafbp-4.1.4-javadoc.jar
Binary file not shown.
Binary file modified build/libs/javafbp-4.1.4-sources.jar
Binary file not shown.
Binary file modified build/libs/javafbp-4.1.4.jar
Binary file not shown.
8 changes: 2 additions & 6 deletions pom.xml
Expand Up @@ -13,15 +13,11 @@
</license>
</licenses>
<dependencies>
<dependency>
<groupId>com.google.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>runtime</scope>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,88 @@

package com.jpaulmorrison.fbp.core.components.misc;

import java.lang.reflect.Field;
import com.jpaulmorrison.fbp.core.engine.Component;
import com.jpaulmorrison.fbp.core.engine.ComponentDescription;
import com.jpaulmorrison.fbp.core.engine.InPort;
import com.jpaulmorrison.fbp.core.engine.InputPort;
import com.jpaulmorrison.fbp.core.engine.MustRun;
import com.jpaulmorrison.fbp.core.engine.OutPort;
import com.jpaulmorrison.fbp.core.engine.OutputPort;
import com.jpaulmorrison.fbp.core.engine.Packet;


/**
* Component to write data to the console, using a stream of packets. It is
* specified as "must run" so that the output file will be cleared even if no
* data packets are input.
*/
@ComponentDescription("Write stream of packets to console")
@InPort(value = "IN", description = "Packets to be displayed")
@OutPort(value = "OUT", optional = true, description = "Output port, if connected")
@MustRun
public class WriteObjectsToConsole extends Component {


private InputPort inport;

private OutputPort outport;

@Override
protected void execute() {
Packet<?> p;

while ((p = inport.receive()) != null) {
if (p.getType() == Packet.OPEN) {
System.out.println("===> Open Bracket");
} else if (p.getType() == Packet.CLOSE) {
System.out.println("===> Close Bracket");
} else {
printRegularIP(p.getContent());
//System.out.println((String) p.getContent());
}

if (outport.isConnected()) {
outport.send(p);
} else {
drop(p);
}
}

}

void printRegularIP(Object o) {
// Thanks to https://stackoverflow.com/questions/2989560/how-to-get-the-fields-in-an-object-via-reflection
String str = "";
String delim = "";
Object value = null;
str = o.getClass().getName() + ": {";
for (Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true); // ???

try {
value = field.get(o);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if (value != null) {
//System.out.println(field.getName() + "=" + value);
str += delim + field.getName() + ": " + value;
delim = "; ";
//}
}
str += "}";
System.out.println(str);
}

@Override
protected void openPorts() {
inport = openInput("IN");

outport = openOutput("OUT");

}
}


0 comments on commit ee3c3ae

Please sign in to comment.