@@ -16,9 +16,20 @@ if (process.env.WEBPORT === undefined) {
1616 config = require ( "./config.js" ) ;
1717} else {
1818 config = { maxcache : process . env . MAXCACHE , webport : process . env . WEBPORT , baseUrl : process . env . WEBURL , dxcc_lookup_wavelog_url : process . env . WAVELOG_URL , dxcc_lookup_wavelog_key : process . env . WAVELOG_KEY , includepotaspots : process . env . POTA_INTEGRATION , potapollinterval : process . env . POTA_POLLING_INTERVAL } ;
19- config . dxc = { host : process . env . DXHOST , port : process . env . DXPORT , loginPrompt : 'login:' , call : process . env . DXCALL , password : process . env . DXPASSWORD } ;
19+ config . clusters = JSON . parse ( process . env . CLUSTERS || '[]' ) ;
20+ config . dxc = { host : process . env . DXHOST , port : process . env . DXPORT , loginPrompt : 'login:' , call : process . env . DXCALL , password : process . env . DXPASSWORD } ;
2021}
2122
23+ let clusters = [ ] ;
24+ if ( config . clusters . length > 0 ) {
25+ // New format: host:port,host:port,host:port
26+ clusters = config . clusters ;
27+ } else {
28+ // Old format: single cluster via CLUSTER_HOST/CLUSTER_PORT
29+ clusters [ 0 ] = config . dxc ;
30+ }
31+
32+
2233morgan . token ( 'remote-addr' , function ( req , res ) {
2334 var ffHeaderValue = req . headers [ 'x-forwarded-for' ] ;
2435 return ffHeaderValue || req . connection . remoteAddress ;
@@ -30,9 +41,9 @@ app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:htt
3041app . use ( cors ( { origin : '*' } ) ) ;
3142
3243// DXCluster connection and spot cache
33- let conn = new DXCluster ( )
3444let spots = [ ] ;
3545
46+
3647// -----------------------------------
3748// Utility Functions
3849// -----------------------------------
@@ -82,45 +93,55 @@ function toUcWord(string) {
8293/**
8394 * Initiates a connection to the DXCluster and logs events.
8495 */
85- function reconnect ( ) {
86- logConnectionState ( 'attempting' , config . dxc , 'DXCluster server for receiving spots' ) ;
87- conn . connect ( config . dxc )
88- . then ( ( ) => {
89- logConnectionState ( 'connected' , config . dxc , 'DXCluster server for receiving spots' ) ;
90- } )
91- . catch ( ( err ) => {
92- logConnectionState ( 'failed' , config . dxc , 'DXCluster server for receiving spots' , err ) ;
93- setTimeout ( reconnect , 5000 ) ; // Retry connection after 5 seconds
94- } ) ;
95- }
96-
97- // Event listeners for connection status changes
98- conn . on ( 'close' , ( ) => {
99- logConnectionState ( 'closed' , config . dxc , 'DXCluster server connection closed' ) ;
100- reconnect ( ) ;
101- } ) ;
102-
103- conn . on ( 'timeout' , ( ) => {
104- logConnectionState ( 'timeout' , config . dxc , 'DXCluster server connection timed out' ) ;
105- reconnect ( ) ;
106- } ) ;
10796
108- conn . on ( 'error' , ( err ) => {
109- logConnectionState ( 'error' , config . dxc , 'DXCluster server connection error' , err ) ;
110- reconnect ( ) ;
111- } ) ;
112-
113- // -----------------------------------
114- // DXCluster Spot Handling
115- // -----------------------------------
116-
117- /**
118- * Processes spots received from DXCluster.
119- */
120- conn . on ( 'spot' , async function x ( spot ) {
121- await handlespot ( spot , "cluster" ) ;
122- } )
97+ function reconnect ( ) {
98+ function conn_one ( cluster ) {
99+ logConnectionState ( 'attempting' , cluster . host , 'DXCluster server for receiving spots' ) ;
100+ const conn = new DXCluster ( ) ;
101+ try {
102+ conn . connect ( cluster ) . then ( ( ) => {
103+ logConnectionState ( 'connected' , cluster . host , 'DXCluster server for receiving spots' ) ;
104+ } )
105+ . catch ( ( err ) => {
106+ logConnectionState ( 'failed' , cluster . host , 'DXCluster server for receiving spots' , err ) ;
107+ conn_one ( cluster ) ;
108+ } ) ;
109+
110+ // Event listeners for connection status changes
111+ conn . on ( 'close' , ( ) => {
112+ logConnectionState ( 'closed' , cluster . host , 'DXCluster server connection closed' ) ;
113+ conn_one ( cluster ) ;
114+ } ) ;
115+
116+ conn . on ( 'timeout' , ( ) => {
117+ logConnectionState ( 'timeout' , cluster . host , 'DXCluster server connection timed out' ) ;
118+ conn_one ( cluster ) ;
119+ } ) ;
120+
121+ conn . on ( 'error' , ( err ) => {
122+ logConnectionState ( 'error' , cluster . host , 'DXCluster server connection error' , err ) ;
123+ conn_one ( cluster ) ;
124+ } ) ;
125+ // -----------------------------------
126+ // DXCluster Spot Handling
127+ // -----------------------------------
128+
129+ /**
130+ * Processes spots received from DXCluster.
131+ */
132+ conn . on ( 'spot' , async function x ( spot ) {
133+ await handlespot ( spot , ( cluster . cluster || 'cluster' ) ) ;
134+ } ) ;
135+ } catch ( e ) {
136+ logConnectionState ( 'error' , config . host , 'DXCluster not reachable ' ) ;
137+ console . log ( e ) ;
138+ }
139+ }
123140
141+ clusters . forEach ( cluster => {
142+ conn_one ( cluster ) ;
143+ } ) ;
144+ }
124145// -----------------------------------
125146// API Endpoints
126147// -----------------------------------
0 commit comments