Java Network Simulator Getting Started - java

I am working on simulating a network topology with Java Network Simulator(JNS). I have followed the tutorial which is in documentation of JNS download. Following is the code of the example:
package simulator_;
import java.awt.peer.TextComponentPeer;
import java.io.IOException;
import jns.Simulator;
import jns.element.DuplexInterface;
import jns.element.DuplexLink;
import jns.element.Interface;
import jns.element.Link;
import jns.element.Node;
import jns.trace.Event;
import jns.trace.Trace;
import jns.util.IPAddr;
public class simu {
public static void main(String[] args) {
Simulator sim=Simulator.getInstance();
Node src=new Node("Source node");
Node router=new Node("Router");
Node dest=new Node("Destination node");
sim.attach(src);
sim.attach(router);
sim.attach(dest);
Interface src_iface=new DuplexInterface(new IPAddr(192,168,1,10));
src.attach(src_iface);
sim.attach(src_iface);
Interface dest_iface=new DuplexInterface(new IPAddr(128,116,11,20));
dest.attach(dest_iface);
sim.attach(dest_iface);
Interface route_iface192=new DuplexInterface(new IPAddr(192,168,1,1));
Interface route_iface128=new DuplexInterface(new IPAddr(128,116,11,1));
router.attach(route_iface192);
router.attach(route_iface128);
sim.attach(route_iface192);
sim.attach(route_iface128);
Link link_src_router=new DuplexLink(1000000,0.001);
Link link_router_dest=new DuplexLink(64000,0.1);
src_iface.attach(link_src_router,true);
route_iface192.attach(link_src_router,true);
sim.attach(link_src_router);
route_iface128.attach(link_router_dest,true);
dest_iface.attach(link_router_dest,true);
sim.attach(link_router_dest);
src.addDefaultRoute(src_iface);
dest.addDefaultRoute(dest_iface);
router.addRoute(new IPAddr(192,168,1,0),new IPAddr(255,255,255,0),
route_iface192);
router.addRoute(new IPAddr(128,116,11,0),new IPAddr(255,255,255,0),
route_iface128);
sim.run();
}
}
The problem I am facing is when I call sim.run(), the program throws me NullPointerException error. I am a newbie in JNS. Kindly guide if how can I successfully create a file foe javis to Simulate with the help of above code. I am using Eclipse IDE.
Thanks in advance.

try setting a trace instance
sim.setTrace(new JavisTrace("output.txt"));

Related

fabric-sdk-java How to implement the Network.addBlockListener method in my App.java

I am new to Hyperlegder-Fabric and already manage to connect to a network through the org.hyperledger.fabric.gateway package.
This is my App.java class:
//Fabric Imports
import org.hyperledger.fabric.gateway.*;
import org.hyperledger.fabric.sdk.BlockEvent;
import org.hyperledger.fabric.sdk.ChaincodeEventListener;
//Other Imports
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public class App{
public static void main(String[] args) throws IOException {
// Load an existing wallet holding identities used to access the network.
Path walletDirectory = Paths.get("wallet");
Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);
// Path to a common connection profile describing the network.
Path networkConfigFile = Paths.get("connection.json");
// Configure the gateway connection used to access the network.
Gateway.Builder builder = Gateway.createBuilder()
.identity(wallet, "user1")
.networkConfig(networkConfigFile);
// Create a gateway connection
try (Gateway gateway = builder.connect()) {
// Obtain a smart contract deployed on the network.
Network network = gateway.getNetwork("mychannel");
Contract contract = network.getContract("fabcar");
// Submit transactions that store state to the ledger.
byte[] createCarResult = contract.createTransaction("createCar")
.submit("CAR10", "VW", "Polo", "Grey", "Mary");
System.out.println(new String(createCarResult, StandardCharsets.UTF_8));
// Evaluate transactions that query state from the ledger.
byte[] queryAllCarsResult = contract.evaluateTransaction("queryAllCars");
System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));
} catch (ContractException | TimeoutException | InterruptedException e) {
e.printStackTrace();
}
}
}
Now I need to listen for block events, so I see in the documentation that the Network package contains the addBlockListener method (https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html)
My doubt is how i can implement this method in the above App.java file so i can get the block number, etc. I am not a java developer i am struggling a lot on this since.
Appreciate any help.
The listener you attach is an implementation of the Consumer<BlockEvent> type, where Consumer is a standard Java functional interface:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Consumer.html
To define your listener you could:
write your own listener class that implements that interface; or
define an anonymous class in-line that implements that interface; or
use a lambda expression as a short-hand for the anonymous class implementation to minimise boiler-plate code.
Using a lambda expression, your code might look like this:
network.addBlockListener(blockEvent -> {
long blockNumber = blockEvent.getBlockNumber();
System.out.println("Received block number " + blockNumber);
});
If you add the listener to the network before submitting your transaction, you should see the block event created when your submitted transaction is recorded on the ledger.

Android create Toast using class reflection?

I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.

Compile Error: Package sqlj.runtime does not exist

I am trying to do a homework assignment in a database course that requires me to use sqlj. It is supposed to make a connection to a SQL database running on my computer. When copying and pasting the example code into Netbeans I get a compile error:
package sqlj.runtime does not exist: import sqlj.runtime.*;
import java.sql.*;
import java.io.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
import oracle.sqlj.runtime.*;
public class A10Q1 {
public static void main(String[] args) {
DefaultContext cntxt = oracle.getConnection("url", "user", "pswd", true);
}
}
I changed the inputs on the getConnection function.

The import org.opencv.imgcodecs.Imgcodecs cannot be resolved

I installed OpenCV (opencv-3.0.0-alpha) and it works properly but I can't use that import:
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
public class Main {
public static void main(String[] args) {
// System.loadLibrary("opencv_java244");
// Mat m = Highgui.imread("C:/Users/raj/Desktop/sa1.png",
// Highgui.CV_LOAD_IMAGE_COLOR);
// new LoadImage("C:/Users/raj/Desktop/dst1.jpg", m);
}
}
I get this error
The import org.opencv.imgcodecs.Imgcodecs cannot be resolved
How can I solve this?
OpenCV 3.0.0 is using the import:
import org.opencv.imgcodecs.Imgcodecs;
However the library you are using (OpenCV 2.4.1) is using different import for the same functionalites:
import org.opencv.highgui.Highgui;
https://fossies.org/diffs/opencv/2.4.11_vs_3.0.0-rc1/modules/java/android_test/src/org/opencv/test/highgui/HighguiTest.java-diff.html
Basically you are trying to import something that doesn't exist in the version you are using.
Now you can either use Highgui or get the jar for OpenCV 3.1.x

passing argument to WordCram code

I have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.

Categories