~phablet-team/nuntium/trunk

1 by Sergio Schvezov
Init
1
/*
2
 * Copyright 2014 Canonical Ltd.
3
 *
4
 * Authors:
5
 * Sergio Schvezov: sergio.schvezov@cannical.com
6
 *
2 by Sergio Schvezov
Changing name
7
 * This file is part of nuntium.
1 by Sergio Schvezov
Init
8
 *
2 by Sergio Schvezov
Changing name
9
 * nuntium is free software; you can redistribute it and/or modify
1 by Sergio Schvezov
Init
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; version 3.
12
 *
2 by Sergio Schvezov
Changing name
13
 * nuntium is distributed in the hope that it will be useful,
1 by Sergio Schvezov
Init
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
package main
23
24
import (
25
	"log"
26
	"os"
27
	"os/signal"
28
	"syscall"
29
)
30
31
type Mainloop struct {
32
	sigchan  chan os.Signal
33
	termchan chan int
34
	Bindings map[os.Signal]func()
35
}
36
37
/*
38
Start the mainloop.
39
40
This method will block its current thread. The best spot for calling this
41
method is right near the bottom of your application's main() function.
42
*/
43
func (m *Mainloop) Start() {
44
	sigs := make([]os.Signal, len(m.Bindings))
45
	for s, _ := range m.Bindings {
46
		sigs = append(sigs, s)
47
	}
48
	signal.Notify(m.sigchan, sigs...)
49
L:
50
	for {
51
		select {
52
		case sig := <-m.sigchan:
53
			log.Print("Received ", sig)
54
			m.Bindings[sig]()
55
		case _ = <-m.termchan:
56
			break L
57
		}
58
	}
59
	return
60
}
61
62
/*
63
Stops the mainloop.
64
*/
65
func (m *Mainloop) Stop() {
66
	go func() { m.termchan <- 1 }()
67
	return
68
}
69
70
func HupHandler() {
71
	syscall.Exit(1)
72
}
73
74
func IntHandler() {
75
	syscall.Exit(1)
76
}