forked from gonicus/gofaxip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Modem/Device Management using HylaFax FIFOs
- Loading branch information
Markus Lindenberg
committed
May 7, 2014
1 parent
c15e2cd
commit 25911b9
Showing
11 changed files
with
461 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"gofaxlib" | ||
"gofaxlib/logger" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
) | ||
|
||
const ( | ||
STATE_READY = iota | ||
STATE_BUSY | ||
STATE_DOWN | ||
STATE_LOCKED | ||
|
||
FIFO_PREFIX = "FIFO." | ||
STATUS_DIR = "status" | ||
) | ||
|
||
type Device struct { | ||
Name string | ||
fifoname string | ||
statusfile string | ||
fifostream gofaxlib.FifoStream | ||
|
||
stateSet chan uint | ||
stateGet chan uint | ||
errors chan error | ||
} | ||
|
||
func NewDevice(name string) (*Device, error) { | ||
var err error | ||
|
||
d := Device{ | ||
Name: name, | ||
|
||
fifoname: filepath.Join(gofaxlib.Config.Hylafax.Spooldir, FIFO_PREFIX+name), | ||
statusfile: filepath.Join(gofaxlib.Config.Hylafax.Spooldir, STATUS_DIR, name), | ||
|
||
stateSet: make(chan uint), | ||
stateGet: make(chan uint), | ||
errors: make(chan error), | ||
} | ||
|
||
// Create device FIFO | ||
stat, err := os.Stat(d.fifoname) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
if err = syscall.Mkfifo(d.fifoname, 0600); err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
} else { | ||
if stat.Mode()&os.ModeNamedPipe == 0 { | ||
return nil, errors.New("File exists and is not a FIFO") | ||
} | ||
} | ||
|
||
go d.stateLoop(STATE_READY) | ||
|
||
d.fifostream = gofaxlib.NewFifoStream(d.fifoname) | ||
go d.fifoLoop() | ||
|
||
d.SetReady() | ||
return &d, nil | ||
} | ||
|
||
func (d *Device) fifoLoop() { | ||
|
||
for { | ||
select { | ||
case msg := <-d.fifostream.Messages(): | ||
|
||
if len(msg) == 0 { | ||
continue | ||
} | ||
logger.Logger.Println(d.fifoname, "received message:", msg) | ||
|
||
switch msg[0] { | ||
case 'H': // Hello | ||
d.SetReady() | ||
case 'L': // Lock | ||
d.SetLocked() | ||
case 'S': // Set state | ||
if len(msg) < 2 { | ||
continue | ||
} | ||
switch msg[1] { | ||
case 'R': | ||
d.SetReady() | ||
case 'B': | ||
d.SetBusy("Sending facsimile", true) | ||
} | ||
default: | ||
logger.Logger.Println("Unhandled message:", msg) | ||
} | ||
|
||
case err := <-d.fifostream.Errors(): | ||
logger.Logger.Printf("Error in stream for FIFO %s: %v", d.fifoname, err) | ||
return | ||
} | ||
} | ||
|
||
} | ||
|
||
func (d *Device) stateLoop(state uint) { | ||
for { | ||
select { | ||
case state = <-d.stateSet: | ||
case d.stateGet <- state: | ||
} | ||
} | ||
} | ||
|
||
func (d *Device) handle(msg string) { | ||
} | ||
|
||
func (d *Device) WriteStatusFile(msg string) { | ||
sfh, err := os.OpenFile(d.statusfile, os.O_RDWR|os.O_CREATE, 0666) | ||
if err != nil { | ||
logger.Logger.Print(err) | ||
return | ||
} | ||
|
||
if err = syscall.Flock(int(sfh.Fd()), syscall.LOCK_EX); err != nil { | ||
sfh.Close() | ||
logger.Logger.Print(err) | ||
return | ||
} | ||
|
||
// Truncate after acquiring lock! | ||
sfh.Truncate(0) | ||
|
||
if _, err := sfh.WriteString(msg); err != nil { | ||
logger.Logger.Print(err) | ||
return | ||
} | ||
|
||
if err = sfh.Close(); err != nil { | ||
logger.Logger.Print(err) | ||
return | ||
} | ||
} | ||
|
||
func (d *Device) GetState() uint { | ||
return <-d.stateGet | ||
} | ||
|
||
func (d *Device) SetReady() { | ||
logger.Logger.Printf("Changing state of modem %v to READY", d.Name) | ||
d.stateSet <- STATE_READY | ||
gofaxlib.Faxq.ModemStatus(d.Name, "N") | ||
d.WriteStatusFile("Running and idle") | ||
gofaxlib.Faxq.ModemStatusReady(d.Name) | ||
} | ||
|
||
func (d *Device) SetBusy(msg string, outbound bool) { | ||
logger.Logger.Printf("Changing state of modem %v to BUSY", d.Name) | ||
d.stateSet <- STATE_BUSY | ||
|
||
if outbound { | ||
gofaxlib.Faxq.ModemStatus(d.Name, "U") | ||
} else { | ||
gofaxlib.Faxq.ModemStatus(d.Name, "B") | ||
} | ||
|
||
if msg == "" { | ||
msg = "Busy" | ||
} | ||
d.WriteStatusFile(msg) | ||
} | ||
|
||
func (d *Device) SetDown() { | ||
logger.Logger.Printf("Changing state of modem %v to DOWN", d.Name) | ||
d.stateSet <- STATE_DOWN | ||
gofaxlib.Faxq.ModemStatus(d.Name, "D") | ||
d.WriteStatusFile("Down") | ||
} | ||
|
||
func (d *Device) SetLocked() { | ||
logger.Logger.Printf("Changing state of modem %v to LOCKED", d.Name) | ||
d.stateSet <- STATE_LOCKED | ||
d.WriteStatusFile("Locked for sending") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type manager struct { | ||
devices []*Device | ||
} | ||
|
||
func NewManager(nameprefix string, count uint) (*manager, error) { | ||
var err error | ||
|
||
m := &manager{ | ||
devices: make([]*Device, count), | ||
} | ||
|
||
for i := uint(0); i < count; i++ { | ||
m.devices[i], err = NewDevice(fmt.Sprintf("%v%v", nameprefix, i)) | ||
if err != nil { | ||
m.SetAllDown() | ||
return nil, err | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m *manager) SetAllDown() { | ||
for _, d := range m.devices { | ||
if d != nil { | ||
d.SetDown() | ||
} | ||
} | ||
} | ||
|
||
func (m *manager) FindDevice(msg string) (*Device, error) { | ||
for _, d := range m.devices { | ||
if d.GetState() == STATE_READY { | ||
d.SetBusy(msg, false) | ||
return d, nil | ||
} | ||
} | ||
return nil, errors.New("No available modem found.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.