简体   繁体   English

存储与应用程序捆绑在一起的静态数据的最佳方法是什么?

[英]Best way to store static data to be bundled with application?

I'm writing an application in java that will facilitate creating wireless sensor networks using off the shelf micro controllers, sensors, and radios. 我正在用java编写一个应用程序,这将有助于使用现成的微控制器,传感器和无线电创建无线传感器网络。 Each sensor and radio will most likely require unique code. 每个传感器和无线电很可能需要唯一的代码。 I'm planning on creating skeletons for each platform and and then having modular bits of code for each sensor and radio that can be plugged into these skeletons. 我打算为每个平台创建骨架,然后为每个传感器和无线电设备提供模块化的代码,这些代码可以插入到这些骨架中。 This will result in a library of static information that will be used to dynamically generate code for these sensors. 这将生成一个静态信息库,用于为这些传感器动态生成代码。

I'm not sure what the best way to store and organize this data would be. 我不确定存储和组织这些数据的最佳方法是什么。 I started off trying to create classes for each sensor encapsulating its unique properties but using objects for data storage only seems weird. 我开始尝试为每个传感器创建封装其独特属性的类,但使用对象进行数据存储只是看起来很奇怪。 I feel like SQL would be overkill as the data isn't really changing and I would also like to keep everything in version control. 我觉得SQL会有点矫枉过正,因为数据并没有真正改变,我也希望将所有内容保存在版本控制中。 Should I just use flat files? 我应该只使用平面文件吗? XML? XML? Any advice on how to architect this project would be very welcome. 关于如何设计这个项目的任何建议都将非常受欢迎。

Instead of generating source, I'd go binary. 而不是生成源,我会去二进制。 Conceptually, that is. 从概念上讲,就是这样。

Why would the source code need to change if a device is plugged in or out? 如果设备插入或拔出,​​为什么源代码需要更改? Simply compile binary device driver libraries and link them to the main app. 只需编译二进制设备驱动程序库并将它们链接到主应用程序。 There is an assembler, so likely there is a linker. 有一个汇编程序,很可能有一个链接器。

If there is no linker, and you are forced to use a monolithic source file, then at least we can use the concepts of a linker. 如果没有链接器,并且您被迫使用单片源文件,那么至少我们可以使用链接器的概念

Linking Source Code 链接源代码

For inspiration and details I'd look into Operating System Design a little bit, for the concepts of device drivers , and IO devices , and network sockets . 有关灵感和细节,我将详细介绍操作系统设计 ,了解设备驱动程序IO设备网络套接字的概念 I'd use this to take a hard look at the source that would be generated, and what exactly changes if a device is changed, and fix it so that as little as possible, ideally nothing, has to be changed. 我将使用它来仔细研究将要生成的源,以及如果更改设备会发生什么变化,并将其修复以便尽可能少地删除,理想情况下不需要更改。

The code for the app running on the (presumably embedded) system should be maintained separate from the device drivers, so here is where the abstraction needs to begin. 在(可能是嵌入式)系统上运行的应用程序的代码应该与设备驱动程序分开维护,因此这里需要开始抽象。 It needs to be refactored to abstract away the particulars of the devices into abstract device classes . 需要对其进行重构,以将设备的细节抽象抽象设备类

So this is the first step: refactor the generated source to abstract out the particulars of the device drivers so that you have a main application that calls functions via symbols . 所以这是第一步:重构生成的源以抽象出设备驱动程序的细节,以便您拥有一个通过符号调用函数的主应用程序。

This allows the main app to work regardless of the number and kind of devices available. 无论可用设备的数量和种类如何,这都允许主应用程序工作。

Next, I'd look into compiler theory , particularly the concepts of symbol resolution and static/dynamic linking , and stub . 接下来,我将研究编译器理论 ,特别是符号解析静态/动态链接以及存根的概念 Since the generated source is refactored so that there is a main application and a list of device drivers, all that is left is to make the devices available to the application. 由于生成的源被重构,因此存在主应用程序和设备驱动程序列表,剩下的就是使设备可用于应用程序。

Illustration 插图

The application could generate the source code to be assembled by concatenating the source for the main application with the source for the device drivers. 应用程序可以通过将主应用程序的源与设备驱动程序的源连接来生成要组装的源代码。

It would provide a stub as well: a small library providing a function to iterate the devices and interrogate their classes. 它还将提供一个存根 :一个小型库,提供迭代设备和查询其类的功能。

Your application then becomes so simple that a one-liner on a *NIX prompt could do it. 然后,您的应用程序变得如此简单,以至于* NIX提示符上的单行可以执行此操作。 No Java required: 不需要Java:

cat program stub drivers/foo drivers/bar > generated-source-or-binary

In it's simplest form, the program would contain a call to an iterate_devices label in stub . 在它最简单的形式中, program将包含对stubiterate_devices标签的调用。

Here's a layout of the source and/or binary image: 这是源和/或二进制映像的布局:

// application
main() {
   for ( device in list_devices() ) {
      switch ( device.class ) {
         ....
      }
   }
}

// stub
list_devices() {
   for ( device = first; device != null; device += *cur )
      yield device;       
}
first: // drivers follow

// drivers/foo
dev_foo: .long dev_foo_end - . // size
....
dev_foo_end

// drivers/bar
dev_bar: .long dev_bar_end - .
....
dev_bar_end

Organizing Driver Sources 组织驱动程序源

This shouldn't have to be more complicated than a directory with files. 这不应该比带有文件的目录更复杂。

A simple approach would be to include these in the .jar in a specific package. 一种简单的方法是将这些包含在特定包中的.jar中。 For instance, having a class provide driver sources like this: 例如,让一个类提供如下驱动程序源:

package myapp.drivers;
public class DriverSource {
    public static InputStream getDriverSource( String identifier ) {
        return this.getClass().getClassLoader().getResourceAsStream(
           this.getClass().getPackage().getName().replace('.', '/')
           + '/' + identifier + '.source'
        );
    }
}

would require the driver sources to be put in myapp/drivers/{identifier}.source . 需要将驱动程序源放在myapp/drivers/{identifier}.source In a standard eclipse project, you'd place the files in src/myapp/drivers/ . 在标准的eclipse项目中,您将文件放在src/myapp/drivers/ Using Maven, you'd put them in src/main/resources/myapp/drivers/ . 使用Maven,你可以将它们放在src/main/resources/myapp/drivers/ You can also put them in another directory, as long as they are copied as resources to the proper package directory. 您也可以将它们放在另一个目录中,只要它们作为资源复制到正确的包目录即可。

The above class could also serve as a basis for more complex storage: you could query a remote service and download the source files, or query an SQL database. 上述类也可以作为更复杂存储的基础:您可以查询远程服务并下载源文件,或查询SQL数据库。 But resource files will be a decent start. 但资源文件将是一个不错的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM