initialise repo
[debian/orchestra.git] / src / player / interface.go
1 // interface.go
2 //
3 // Score Interface
4 //
5 // This provides the interface that the score interfaces need to conform to.
6
7 package main
8
9 import (
10         o "orchestra"
11         "os"
12 )
13
14 var (
15         interfaces      = make(map[string]func(*TaskRequest)(ScoreInterface))
16 )
17
18 type ExecutionEnvironment struct {
19         Environment     map[string]string
20         Arguments       []string
21         Files           []*os.File
22 }
23
24 func NewExecutionEnvironment() (ee *ExecutionEnvironment) {
25         ee = new(ExecutionEnvironment)
26         ee.Environment = make(map[string]string)
27
28         return ee
29 }
30
31 type ScoreInterface interface {
32         // prepare gets called up front before the fork.  It should do
33         // any/all lifting required.
34         //
35         // returns false if there are any problems.
36         Prepare() bool
37
38         // SetupEnvironment gets called prior to starting the child
39         // process.  It should return an ExecutionEnvironment with the
40         // bits filled in the way it wants.
41         SetupProcess() *ExecutionEnvironment
42
43         // Cleanup is responsible for mopping up the mess, filing any
44         // results that need to be stored, etc.  This will be called
45         // only from the main thread to ensure that results can be updated
46         // safely.
47         Cleanup()
48 }
49
50 func HasInterface(ifname string) bool {
51         _, exists := interfaces[ifname]
52
53         return exists
54 }
55
56 func RegisterInterface(ifname string, initfunc func(*TaskRequest)(ScoreInterface)) {
57         _, exists := interfaces[ifname]
58         if exists {
59                 o.Assert("Multiple attempts to register %s interface", ifname)
60         }
61         interfaces[ifname] = initfunc
62 }
63
64 func NewScoreInterface(task *TaskRequest) (iface ScoreInterface) {
65         score, exists := Scores[task.Score]
66         if !exists {
67                 return nil
68         }
69         if !HasInterface(score.Interface) {
70                 return nil
71         }
72         ifinit, _ := interfaces[score.Interface]
73         
74         iface = ifinit(task)
75
76         return iface
77 }