简体   繁体   English

通过UDP接收实时GPS数据

[英]Receiving real-time GPS data via UDP

I'm building a real-time GPS tracking system, which will receive GPS data sent from a couple of Arduino devices using UDP. 我正在构建一个实时GPS跟踪系统,它将接收使用UDP从几个Arduino设备发送的GPS数据。 I have this code so far: 到目前为止,我有以下代码:

PreparedStatement stmt ...

DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];

while(true){
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    String received = new String( receivePacket.getData());
    System.out.println("RECEIVED: " + received);

    stmt.set...
    stmt.execute();
}

1 - Anyone with more knowledge could tell me if there's a better way of doing this? 1-有更多知识的人都可以告诉我是否有更好的方法? I really don't know how the JVM handles this, but I don't like that infinite loop. 我真的不知道JVM如何处理这个问题,但是我不喜欢那个无限循环。

2 - Lets say that I have 50 Arduinos sending data. 2-假设我有50个Arduino发送数据。 I need to use threads or something like this? 我需要使用线程或类似的东西吗?

3 - It's best to use a thread per "connection" (UDP is connectionless) like an answer below or use frameworks/libs like Apache Mina or Netty? 3-最好在每个“连接”中使用一个线程(UDP是无连接的),例如下面的答案,或者使用Apache Mina或Netty等框架/库?

There is no problem using an infinite loop in this case. 在这种情况下,使用无限循环是没有问题的。 Calling receive waits until a new datagram is delivered: 调用接收将等待,直到传递了新的数据报:

This method blocks until a datagram is received. 该方法将阻塞,直到接收到数据报为止。 T... 的...

So no CPU power is wasted here, it simply waits until new data is available. 因此,这里没有浪费CPU资源,它只是等到有新数据可用。

If you have many clients or if processing the packet isn't completely trivial, you should start a new thread for processing each one, so that the main thread that receives the datagrams doesn't get blocked. 如果您有许多客户端,或者如果处理数据包不是很简单,则应该启动一个新线程来处理每个数据包,以使接收数据报的主线程不会被阻塞。 Probably the best approach is to use thread pools that will create threads for you, and at the same time prevent creating too many threads if your application is overloaded by requests. 最好的方法可能是使用将为您创建线程的线程池 ,同时如果您的应用程序被请求重载,则同时防止创建太多线程。

I'd proceed as follows: 我将进行如下操作:

  1. Create a dedicated thread for receiving the datagrams. 创建一个专用线程来接收数据报。 It could also create a thread pool for dispatching processing the requests. 它还可以创建一个线程池,用于分派处理请求。 Something like: 就像是:

     int maxNumberOfThreads = ...; // your choice int bufSize = ...; // your choice ExecutorService exec = Executors.newFixedThreadPool(maxNumberOfThreads); DatagramSocket serverSocket = new DatagramSocket(9876); while (true) { // we need to create a new buffer every time because // multiple threads will be working with the data DatagramPacket receivePacket = new DatagramPacket(new byte[bufSize], bufSize); serverSocket.receive(receivePacket); exec.submit(new YourTask(receivePacket)); } 
  2. Create class YourTask that processes the datagrams: 创建用于处理数据报的类YourTask

     // We don't use return values for anything here, so // we just use Object. public class YourTask extends Callable<Object> { private DatagramPacket received; public YourTask(DatagramPacket received) { this.received = received; } public Object call() { // do your processing here System.out.println("RECEIVED from " + received.getAddress() + ": " + new String(received.getData(), 0, received.getLength())); return null; } } 

I recommend you look at Apache MINA (http://mina.apache.org/), a great framework for network applications. 我建议您看一下Apache MINA(http://mina.apache.org/),它是网络应用程序的绝佳框架。 With MINA you don't need to implement a loop or worry about threading. 使用MINA,您无需实现循环或担心线程问题。

The actual problem I see in your question is the "Real Time" term. 我在您的问题中看到的实际问题是“实时”一词。 What do you mean with that? 你那是什么意思? Do you need a highly predictable (in terms of timing) application, is it safety/mission critical? 您是否需要一个高度可预测的(就时间而言)应用程序,它对安全/任务至关重要吗? If so there may be problem in using Java, as it is for many reasons (ie garbage collector, etc.) not realtime. 如果是这样,使用Java可能会出现问题,因为很多原因(例如垃圾收集器等)不是实时的。 There are however some realtime JVM as http://www.atego.com/products/aonix-perc/ . 但是,有一些实时JVM作为http://www.atego.com/products/aonix-perc/ I do like Java but I guess in this case, if you really need a RT system, C++ would be a better choice. 我确实喜欢Java,但我想在这种情况下,如果您确实需要RT系统,那么C ++将是一个更好的选择。

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

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