Class: Hamnet

Inherits:
Object
  • Object
show all
Defined in:
lib/hamnet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mycall, dialfreq, carrier, modem) ⇒ Hamnet

Returns a new instance of Hamnet.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hamnet.rb', line 185

def initialize(mycall, dialfreq, carrier, modem)
  @mycall=mycall.downcase
  @dialfreq=dialfreq.to_f
  @carrier=carrier.to_i
  @modem=modem
  @sequence=0

  @queue_send=Array.new()
  @queue_recv=Array.new()
  @send_lock=Mutex.new()
  @recv_lock=Mutex.new()
  @seq_lock=Mutex.new()

  @fldigi=Fldigi.new()

  # May not need this. Seems to depend on ruby version.
  #if !XMLRPC::Config::ENABLE_NIL_PARSER
  #v, $VERBOSE=$VERBOSE, nil
  #XMLRPC::Config::ENABLE_NIL_PARSER=true
  #$VERBOSE=v
  #end

  # Light up FLDigi.
  @fldigi.receive()
  @fldigi.afc=false
  @fldigi.modem=@modem
  @fldigi.carrier=@carrier
  @fldigi.clear_tx_data()
  @fldigi.get_tx_data()
  @fldigi.get_rx_data()
  @fldigi.config()

  # Crank up the main thread.
  @loopthread=Thread.new { self.main_loop() }
  @loopthread.abort_on_exception=true
end

Instance Attribute Details

#carrierObject

Returns the value of attribute carrier.



183
184
185
# File 'lib/hamnet.rb', line 183

def carrier
  @carrier
end

#dialfreqObject

Returns the value of attribute dialfreq.



183
184
185
# File 'lib/hamnet.rb', line 183

def dialfreq
  @dialfreq
end

#modemObject

Returns the value of attribute modem.



183
184
185
# File 'lib/hamnet.rb', line 183

def modem
  @modem
end

#mycallObject

Returns the value of attribute mycall.



183
184
185
# File 'lib/hamnet.rb', line 183

def mycall
  @mycall
end

Instance Method Details

#main_loopObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/hamnet.rb', line 222

def main_loop
  rxdata=""
  frame=nil
  while true

    # First, send any queued outgoing frames.
    @send_lock.synchronize {
      while @queue_send.length>0 do
        f=@queue_send.shift
        puts "Transmit frame:\n#{f.to_s}"
        @fldigi.add_tx_string(f.to_s)
        @fldigi.send_buffer(true)
      end
    }

    # Get any new data from fldigi. Strip out any Unicode bullshit.
    rx=@fldigi.get_rx_data()
    rx.each_codepoint do |i|
      if i>=32 and i<=126
        rxdata=rxdata+i.chr
      end
    end

    if rxdata.length>1024
      l=rxdata.length
      rxdata=rxdata[80,l-1024]
    end

    if rxdata.scan(/</).length==0
      rxdata=""
    elsif rxdata.scan(/<<</).length>0
      rxdata=rxdata.match(/<<<.*/).to_s
    end

    # If there's a valid frame in there, process it.
    if rxdata.match(/<<<.*>>>/).to_s.length>0
      rawframe=rxdata.reverse.match(/>>>.*?<<</).to_s.reverse

      # Parse the recieved frame.
      if rawframe.length>0
        frame=RxFrame.new(rawframe)

        if frame.valid and frame.to==@mycall
          @recv_lock.synchronize {
            puts "Received valid frame with my call sign:"
            p frame
            @queue_recv.push(frame)
          }
        end

        # Remove the frame text from the buffer.
        rxdata.slice!(rawframe)
      end
    end

    # If there's a valid ping frame in the recv_queue, remove it and
    # generate a reply ping frame.
    @recv_lock.synchronize {
      if @queue_recv.length>0
        puts "length:  #{@queue_recv.length}"
        p @queue_recv
        tmp_queue=Array.new()
        @queue_recv.each do |tmpframe|
          if tmpframe.type==HAMNET_FRAME_PING
            self.send_frame(TxFrame.new(@mycall, tmpframe.from, HAMNET_FRAME_PING_REPLY, 0, @fldigi.radio_freq().to_s, true))
          elsif tmpframe.type==HAMNET_FRAME_PING_REPLY
            puts "Received a reply to our ping:"
            p tmpframe
          else
            tmp_queue.push(tmpframe)
          end
        end
        @queue_recv=tmp_queue
      end
    }

    sleep 1
  end
end

#ping(hiscall) ⇒ Object



326
327
328
329
# File 'lib/hamnet.rb', line 326

def ping(hiscall)
  self.send_frame(TxFrame.new(@mycall, hiscall, HAMNET_FRAME_PING, 0, nil, true))
  return(nil)
end

#recv_frameObject



316
317
318
319
320
321
322
323
324
# File 'lib/hamnet.rb', line 316

def recv_frame()
  frame=nil
  @recv_lock.synchronize {
    if @queue_recv.length>0
      frame=@queue_recv.shift
    end
  }
  return(frame)
end

#send_frame(frame) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/hamnet.rb', line 302

def send_frame(frame)
  len=nil
  @send_lock.synchronize {
    @seq_lock.synchronize {
      frame.sequence=@sequence
      @sequence=@sequence+1
      @queue_send.push(frame)
      puts "Frame added to queue_send:\n#{frame.to_s}"
      len=@queue_send.length
    }
  }
  return(len)
end