summaryrefslogtreecommitdiff
path: root/spooler.go
blob: e11b9803dca6753b8b4de92df8dcfcb42f551070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main

import (
	"github.com/fsnotify/fsnotify"
	"log"
	"os"
	"fmt"
)

// parse a request file and return a SCORSHmessage
func parse_request(fname string) (SCORSHmsg, error) {

	var ret SCORSHmsg
	_, err := os.Open(fname)
	if err != nil {
		log.Printf("Unable to open file: %s\n", fname)
		return ret, SCORSHerr(SCORSH_ERR_NO_FILE)
	}

	// FIXME: Fill in the ret structure
	
	return ret, nil
	
}


func spooler(watcher *fsnotify.Watcher, worker chan SCORSHmsg) {
	
	for {
		select {
		case event := <-watcher.Events:
			if event.Op == fsnotify.Create {
				msg, err := parse_request(event.Name)
				if err != nil {
					log.Printf("Invalid packet received. [%s]\n", err)
				}
				worker <- msg
			}
		case err := <-watcher.Errors:
			log.Println("error:", err)
		}
	}
}


func StartSpooler(master *SCORSHmaster) error {

	watcher, err := fsnotify.NewWatcher()

	if err != nil {
		return fmt.Errorf("Error creating watcher: %s\n", err)
	}

	err = watcher.Add(master.Spooldir)
	if err != nil {
		return fmt.Errorf("Error adding folder: %s\n", err)
	}
	
	go spooler(watcher, master.Spooler)
	
	return nil
	
}