~jamesh/account-polld/account-refresh

« back to all changes in this revision

Viewing changes to cmd/account-polld/main.go

  • Committer: Sergio Schvezov
  • Date: 2014-07-15 09:42:12 UTC
  • mfrom: (5.1.4 account-polld)
  • Revision ID: sergio.schvezov@canonical.com-20140715094212-jrpb3cp483pouczs
Monitoring accounts with the accounts internal package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
        "log"
24
24
 
 
25
        "launchpad.net/account-polld/accounts"
25
26
        "launchpad.net/account-polld/plugins"
 
27
        "launchpad.net/account-polld/plugins/gmail"
26
28
        "launchpad.net/go-dbus/v1"
27
29
)
28
30
 
31
33
        notifications *[]plugins.Notification
32
34
}
33
35
 
 
36
const (
 
37
        SERVICENAME_GMAIL    = "google-gmail"
 
38
        SERVICENAME_TWITTER  = "twitter-microblog"
 
39
        SERVICENAME_FACEBOOK = "facebook-microblog"
 
40
)
 
41
 
 
42
func init() {
 
43
}
 
44
 
34
45
func main() {
35
46
        // TODO NewAccount called here is just for playing purposes.
36
 
        a := NewAccount("sergiusens@gmail.com", "gmail")
37
 
        defer a.Delete()
38
47
        postWatch := make(chan *PostWatch)
39
48
 
40
49
        if bus, err := dbus.Connect(dbus.SessionBus); err != nil {
43
52
                go postOffice(bus, postWatch)
44
53
        }
45
54
 
46
 
        go a.Loop(postWatch)
 
55
        go monitorAccounts(postWatch)
47
56
 
48
57
        done := make(chan bool)
49
58
        <-done
50
59
}
51
60
 
 
61
func monitorAccounts(postWatch chan *PostWatch) {
 
62
        mgr := make(map[uint]*AccountManager)
 
63
L:
 
64
        for data := range accounts.WatchForService(SERVICENAME_GMAIL, SERVICENAME_FACEBOOK, SERVICENAME_TWITTER) {
 
65
                if account, ok := mgr[data.AccountId]; ok {
 
66
                        log.Printf("New account data for %d - was %#v, now is %#v", data.AccountId, account.authData, data)
 
67
                        if data.Enabled {
 
68
                                account.authData = data
 
69
                        } else {
 
70
                                account.Delete()
 
71
                                delete(mgr, data.AccountId)
 
72
                        }
 
73
                } else if data.Enabled {
 
74
                        var plugin plugins.Plugin
 
75
                        switch data.ServiceName {
 
76
                        case SERVICENAME_GMAIL:
 
77
                                log.Println("Creating account with id", data.AccountId, "for", data.ServiceName)
 
78
                                plugin = gmail.New()
 
79
                        case SERVICENAME_FACEBOOK:
 
80
                                // This is just stubbed until the plugin exists.
 
81
                                log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
 
82
                                continue L
 
83
                        case SERVICENAME_TWITTER:
 
84
                                // This is just stubbed until the plugin exists.
 
85
                                log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
 
86
                                continue L
 
87
                        default:
 
88
                                log.Println("Unhandled account with id", data.AccountId, "for", data.ServiceName)
 
89
                                continue L
 
90
                        }
 
91
                        mgr[data.AccountId] = NewAccountManager(data, plugin)
 
92
                        go mgr[data.AccountId].Loop(postWatch)
 
93
                }
 
94
        }
 
95
}
 
96
 
52
97
func postOffice(bus *dbus.Connection, postWatch chan *PostWatch) {
53
98
        for post := range postWatch {
54
99
                for _, n := range *post.notifications {
55
 
                        fmt.Println("Should be dispathing", n, "to the post office using", bus.UniqueName, "for", post.appId)
 
100
                        fmt.Println("Should be dispatching", n, "to the post office using", bus.UniqueName, "for", post.appId)
56
101
                }
57
102
        }
58
103
}