(tl;dr: How to design an object oriented log file parser. Not exactly, but close enough)
First off, this has nothing to do with networking. My example matches my data format without me needing to describe things you've never heard of. This is not a question on actually parsing data, but on an OO design.
I've got lines of text that are formatted as:
Nxx [type] (N1, N2, ..., Nx) (Mem=x, Disc=x, Eth=x)
N is the node ID, 1-1024
type is the resource type
(N1, N2, ..., Nx) are node IDs that directly connect to this node, so far the most I've seen are 5 but theoretically it could have 1023 connections.
Mem=x is the memory, Disc available disc space, Eth the number of network connections, etc. If the value is 0 the whole field is not present.
For example:
N12 [printer] (30) (Mem=34, Eth=1)
N18 [server] (12, 17, 36, 120) (Mem=194, Disc=740, Eth=4)
N129 [router] (13, 94, 248) (Eth=16)
Not all nodes are defined. That is, there may not be a node 42 at all. That line will not be present (e.g. there are no lines "N42 [] () ()").
More (almost) irrelevant details. Node IDs and connectors don't change, everything else does. Yes, my printer can turn into a router with 1 connection (told you this had nothing to do with networking). Second, each file has a header with a source and a date, my save file also keeps track of the last time a line changed and the source of the change.
I've done this sort of thing several times in awk and perl via regexps and stdin/stdout. But now I want to do it in an object oriented way, probably in Java.
I can think of 2 ways to do it. First, the core object is the node. The main data structure is an array of 1025 nodes (node 0 doesn't exist and I don't want to dork with converting node[0] to N1). The node class reads a line and fills itself in. This seems like procedural programming with OO lipstick.
The other way I can think of is to have the parser itself be the core object, returning a I
VALUE pair. Ex: I
12, TYPE
rinter, CONNECTION:30, etc. But again, this just seems like adding OO lipstick to a procedural program.
The goal? I get reports from various places at various times. Each report has a partial view of the entire system. I want to read a save file, parse a report, update the database, and write a new save file. I'm currently using bash, sort, and Vi to maintain my central database, can you say "pain in the keister?"
Keeping in mind actually parsing the line isn't the problem, but the OO design of the beast is: Comments?
First off, this has nothing to do with networking. My example matches my data format without me needing to describe things you've never heard of. This is not a question on actually parsing data, but on an OO design.
I've got lines of text that are formatted as:
Nxx [type] (N1, N2, ..., Nx) (Mem=x, Disc=x, Eth=x)
N is the node ID, 1-1024
type is the resource type
(N1, N2, ..., Nx) are node IDs that directly connect to this node, so far the most I've seen are 5 but theoretically it could have 1023 connections.
Mem=x is the memory, Disc available disc space, Eth the number of network connections, etc. If the value is 0 the whole field is not present.
For example:
N12 [printer] (30) (Mem=34, Eth=1)
N18 [server] (12, 17, 36, 120) (Mem=194, Disc=740, Eth=4)
N129 [router] (13, 94, 248) (Eth=16)
Not all nodes are defined. That is, there may not be a node 42 at all. That line will not be present (e.g. there are no lines "N42 [] () ()").
More (almost) irrelevant details. Node IDs and connectors don't change, everything else does. Yes, my printer can turn into a router with 1 connection (told you this had nothing to do with networking). Second, each file has a header with a source and a date, my save file also keeps track of the last time a line changed and the source of the change.
I've done this sort of thing several times in awk and perl via regexps and stdin/stdout. But now I want to do it in an object oriented way, probably in Java.
I can think of 2 ways to do it. First, the core object is the node. The main data structure is an array of 1025 nodes (node 0 doesn't exist and I don't want to dork with converting node[0] to N1). The node class reads a line and fills itself in. This seems like procedural programming with OO lipstick.
The other way I can think of is to have the parser itself be the core object, returning a I
VALUE pair. Ex: I
12, TYPEThe goal? I get reports from various places at various times. Each report has a partial view of the entire system. I want to read a save file, parse a report, update the database, and write a new save file. I'm currently using bash, sort, and Vi to maintain my central database, can you say "pain in the keister?"
Keeping in mind actually parsing the line isn't the problem, but the OO design of the beast is: Comments?