add support for configuring distributor addresses
Stefan Schuermans

Stefan Schuermans commited on 2016-11-22 20:41:16
Showing 3 changed files, with 72 additions and 15 deletions.

... ...
@@ -101,6 +101,45 @@ class Config
101 101
     m_display.m_distriCnt++;
102 102
   }
103 103
 
104
+  /**
105
+   * @brief process distributor address from config file
106
+   *
107
+   * @param[in] settingPart2 second half of setting to process
108
+   * @param[in] value value of setting
109
+   */
110
+  void procDistriAddr(String settingPart2, String value)
111
+    throws Exception
112
+  {
113
+    int dist;
114
+    Distri distri;
115
+    InetSocketAddress addr;
116
+
117
+    // get distributor number
118
+    try {
119
+      dist = Integer.parseInt(settingPart2);
120
+    } catch (NumberFormatException e) {
121
+      dist = Constants.distriMaxCnt; // force error in next line
122
+    }
123
+    if (dist >= Constants.distriMaxCnt)
124
+      errorExc(String.format("invalid distributor number \"%d\"" +
125
+                             " in line %d of config file", dist, m_lineNo));
126
+
127
+    // get distributor
128
+    distri = m_display.m_distris[dist];
129
+    if (distri == null)
130
+      errorExc(String.format("no distributor with number \"%d\"" +
131
+                             " in line %d of config file", dist, m_lineNo));
132
+
133
+    // get address
134
+    addr = AddrParser.parseAddr(value);
135
+    if (addr == null) {
136
+      errorExc("invalid addess \"" + value +
137
+               "\" for distributor with number \"%u\"" +
138
+               String.format(" in line %d of config file", m_lineNo));
139
+    }
140
+    distri.m_addr = addr;
141
+  }
142
+
104 143
   /**
105 144
    * @brief process mapping from config file
106 145
    *
... ...
@@ -331,6 +370,12 @@ class Config
331 370
       return;
332 371
     }
333 372
 
373
+    // distributor address
374
+    if (setting.startsWith("distributorAddr ")) {
375
+      procDistriAddr(setting.substring(16), value);
376
+      return;
377
+    }
378
+
334 379
     // mapping
335 380
     if (setting.startsWith("mapping ")) {
336 381
       procMapping(setting.substring(8), value);
... ...
@@ -168,26 +168,16 @@ public class Display
168 168
       distri = m_distris[dist];
169 169
       if (distri != null) {
170 170
 
171
+        if (distri.m_addr != null) {
171 172
           try {
172
-          // assemble destination address of distributor
173
-          int intIp = Constants.destIpBase +
174
-                      distri.m_distri * Constants.destIpStep;
175
-          byte [] byteIp = {
176
-            (byte)(intIp >> 24),
177
-            (byte)(intIp >> 16),
178
-            (byte)(intIp >>  8),
179
-            (byte)intIp
180
-          };
181
-          InetAddress ip = InetAddress.getByAddress(byteIp);
182
-
183 173
             // send message as UDP packet
184 174
             DatagramPacket pack = new DatagramPacket(distri.m_msgBuf,
185 175
                                                      distri.m_msgBuf.length,
186
-                                                   ip, Constants.destPort);
176
+                                                     distri.m_addr);
187 177
             m_sock.send(pack);
188
-        } catch (java.net.UnknownHostException e) {
189 178
           } catch (java.io.IOException e) {
190 179
           }
180
+        }
191 181
 
192 182
       } // if distri
193 183
     } // for distri
... ...
@@ -18,6 +18,7 @@
18 18
 
19 19
 package org.blinkenarea.JFlexiPix;
20 20
 
21
+import java.net.*;
21 22
 import java.util.Arrays;
22 23
 
23 24
 /// FlexiPix distributor
... ...
@@ -36,6 +37,24 @@ class Distri
36 37
     m_outputCnt = outputCnt;
37 38
     m_pixelCnt  = pixelCnt;
38 39
 
40
+    // initialize address to default
41
+    try {
42
+      int intIp = Constants.destIpBase +
43
+                  m_distri * Constants.destIpStep;
44
+      byte [] byteIp = {
45
+        (byte)(intIp >> 24),
46
+        (byte)(intIp >> 16),
47
+        (byte)(intIp >>  8),
48
+        (byte)intIp
49
+      };
50
+      m_addr = new InetSocketAddress(InetAddress.getByAddress(byteIp),
51
+                                     Constants.destPort);
52
+    } catch (java.net.UnknownHostException e) {
53
+      m_addr = null;
54
+    } catch (java.io.IOException e) {
55
+      m_addr = null;
56
+    }
57
+
39 58
     // allocate default mappings
40 59
     m_mapRed   = new Mapping();
41 60
     m_mapGreen = new Mapping();
... ...
@@ -55,11 +74,14 @@ class Distri
55 74
   int               m_distri;    ///< number of this distributor
56 75
   int               m_outputCnt; ///< number of outputs
57 76
   int               m_pixelCnt;  ///< number pixels connected to every output
77
+  InetSocketAddress m_addr;      ///< network address of distributor
58 78
   Mapping           m_mapRed;    ///< mapping information for red channel
59 79
   Mapping           m_mapGreen;  ///< mapping information for Green channel
60 80
   Mapping           m_mapBlue;   ///< mapping information for blue channel
61
-  Pixel [] m_pixels;    /**< information about pixels of this distributor,
81
+  Pixel []          m_pixels;    /**< information about pixels of this
82
+                                      distributor,
62 83
                                       index = output * m_pixelCnt + pixel */
63
-  byte []  m_msgBuf;    ///< buffer for current message to send to distributor
84
+  byte []           m_msgBuf;    /**< buffer for current message to send to
85
+                                      distributor */
64 86
 }
65 87
 
66 88