Class: NickelSilver::Server::LocoNetServer
- Inherits:
-
GServer
- Object
- GServer
- NickelSilver::Server::LocoNetServer
- Defined in:
- lib/LocoNetServer.rb
Overview
Summary
An implementation of the LoconetOverTcp protocol version 1 for use with the LocoBuffer-USB awailable from RR-CirKits (www.rr-cirkits.com).
This simple protocol allows clients connected via TCP to access a LocoNet netowrk. Both sending and receiving of packets is supported.
- Author
-
Tobin Richard ([email protected])
- Copyright
-
Copyright © 2008
- License
-
Distributes under the same terms as Ruby
Usage
The following creates a server listening on the default port of 5626 (‘loco’ spelt on a phone keypad) using a LocoBuffer-USB connected to the serial port tty.serialport.
require 'rubygems'
require 'nickel-silver-server'
# connect to a LocoBufferUSB on the virtual serial port /dev/tty.serialport
interface = NickelSilver::Server::Interface::LocoBufferUSB.new( '/dev/tty.serialport' )
# create a server using the default port (i.e. 5626, 'loco' spelt on a phone keypad)
# using our freshly connected LocoBuffer-USB
server = NickelSilver::Server::LocoNetServer.new( interface )
# start the server
server.start
# wait for the server to stop before exiting
server.join
If you want logging of connections, disconnections and other activity then add server.audit = true before server.start.
Protocol
For full details of the LoconetOverTcp protocol see loconetovertcp.sourceforge.net/Protocol/LoconetOverTcp.html
Information is exchanged between the server and clients as plain ASCII strings. The server ignores invalid commands and empty lines.
Clients may send the following commands to the server, as per the protocol specification:
SEND Send a packet out over the LocoNet connection. The packet is not checked for correctness before transmission. E.g. SEND a0 2f 00 70
The server may send the following information to clients, as per the protocol specification:
VERSION: Sent to new clients immediately after they connect. The string which follows describes the LocoNetOverTcp server’s name and version. E.g. VERSION NickelSilver version 0.1
RECEIVE: Sent when a packet is received by the LocoBuffer-USB. E.g. RECEIVE 83 7c
SENT: Sent to clients after an attempt has been made to process a SEND command. First parameter is always OK or ERROR and may be followed by a string describing details fo the transmission. E.g. SENT ERROR Could not communicate with LocoBuffer-USB
Instance Method Summary collapse
-
#initialize(interface, tcp_port = 5626, *args) ⇒ LocoNetServer
constructor
Creates a new LocoNetOverTCP server.
Constructor Details
#initialize(interface, tcp_port = 5626, *args) ⇒ LocoNetServer
Creates a new LocoNetOverTCP server.
You must supply an interface object and you may specify a port if the default of 5626 does not suit your environment.
See the full documentation for this class for an exmaple using the LocoBuffer-USB.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/LocoNetServer.rb', line 73 def initialize( interface, tcp_port=5626, *args ) # we maintain a list of clients to be notified of LocoNet packets @clients = [] # we will require access to the interface's buffers @interface = interface # start the interface buffering in another thread Thread.new { @interface.run } # process incoming packets in another thread Thread.new { process_packets } super( tcp_port, *args ) end |