Reputation: 41
I'm trying to print out a set of coordinates from a class. Each instance of this class has 3 values: longitute, latitude, and IP address. I'm reading from a log file that logs source and destination IP addresses of various cyber attacks. When I read an IP address from the log file, is there a way for JS to recognize that an IP address belongs to an instance of a class, and then print future class values based on which class it recognizes? What is the best way to do this? Can this be solved by JQuery?
class comp {
constructor(ipa, long, lat){
this.address = ipa;
this.long = long;
this.lat = lat;
ips.push(address);
}
getlong(){
return this.long;
};
getlat(){
return this.lat;
}
getip(){
return this.address;
}
setlatlong(inputLong, inputLat){
this.long = inputLong;
this.lat = inputLat;
return;
}
setip(inputIP){
this.address = inputIP;
}
};
comp1 = new comp('10.0.0.1', '-77.050636', '38.889248');
comp2 = new comp('10.0.0.2', '-78.050636', '39.889248');
//obj = ip addresses
function process(obj){
///
if (ids.includes(obj.id)) {
ids.push(obj.id);
hits.push( { origin : { latitude: comp1.getlat(), longitude: comp1.getlong() },
destination : { latitude: comp2.getlat(), longitude: comp2.getlong() } } );
}
};
Upvotes: 0
Views: 51
Reputation: 836
I'm not sure if I get what you mean, but you could use static properties. This is a pure JS solution, though.
Also you're probably aware of this, but as per OOP norm, I advise you to use Pascal case to name classes, so I took the liberty of renaming it accordingly.
class Comp {
// declare static array
static instances = [];
constructor(ipa, long, lat) {
this.address = ipa;
this.long = long;
this.lat = lat;
// to access static properties, use Class.property syntax
// populates the class's instances array with every new instance
Comp.instances.push(this);
ips.push(this.address);
}
// static method to retrieve instance based on IP
static getInstance(ip) {
return Comp.instances.find(v => v.address == ip, null);
}
getlong(){
return this.long;
};
getlat(){
return this.lat;
}
getip(){
return this.address;
}
setlatlong(inputLong, inputLat){
this.long = inputLong;
this.lat = inputLat;
return;
}
setip(inputIP){
this.address = inputIP;
}
};
comp1 = new Comp('10.0.0.1', '-77.050636', '38.889248');
comp2 = new Comp('10.0.0.2', '-78.050636', '39.889248');
console.log(Comp.getInstance('10.0.0.1')); // prints comp1
Now here's the bit I couldn't really get. Doesn't make sense checking the array for an IP and then pushing it again if true.
So I figured you just want each IP to be instantiated once, so...
// source and target objects
function process(src, tgt){
// gets instance of src, if it exists; else creates new one
let comp1 = Comp.getInstance(src.ipa) || new Comp(src.ipa, src.long, src.lat);
// gets instance of tgt, if it exists; else creates new one
let comp2 = Comp.getInstance(tgt.ipa) || new Comp(tgt.ipa, tgt.long, src.lat);
hits.push({
origin: { latitude: comp1.getlat(), longitude: comp1.getlong() },
destination : { latitude: comp2.getlat(), longitude: comp2.getlong() }
});
};
You can execute console.log(Comp.instances)
on your console to check the stored addresses and coordinates. Another possible idea, if it makes sense, would be throwing an exception if one is trying to instantiate a dupe IP.
Upvotes: 1