Skip to content

Commit

Permalink
refactoring service interface changes
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantgupta24 committed Mar 20, 2019
1 parent 264e84f commit e96d248
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 35 deletions.
22 changes: 17 additions & 5 deletions internal/pkg/service/mouseClickHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type MouseClickHandler struct{}
type MouseClickHandler struct {
tickerCh chan struct{}
}

func (m *MouseClickHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) {
tickerCh = make(chan struct{})
func (m *MouseClickHandler) Start(activityCh chan *activity.Type) {
m.tickerCh = make(chan struct{})
registrationFree := make(chan struct{})

go func() {
go addMouseClickRegistration(activityCh, registrationFree) //run once before first check
for range tickerCh {
for range m.tickerCh {
log.Printf("mouse clicker checked at : %v\n", time.Now())
select {
case _, ok := <-registrationFree:
Expand All @@ -34,8 +36,18 @@ func (m *MouseClickHandler) Start(activityCh chan *activity.Type) (tickerCh chan
log.Printf("stopping click handler")
return
}()
}

return tickerCh
func (m *MouseClickHandler) Trigger() {
//doing it the non-blocking sender way
select {
case m.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (m *MouseClickHandler) Close() {
close(m.tickerCh)
}

func addMouseClickRegistration(activityCh chan *activity.Type, registrationFree chan struct{}) {
Expand Down
22 changes: 17 additions & 5 deletions internal/pkg/service/mouseCursorHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type MouseCursorHandler struct{}
type MouseCursorHandler struct {
tickerCh chan struct{}
}

type cursorInfo struct {
didCursorMove bool
currentMousePos *mouse.Position
}

func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) {
func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) {

tickerCh = make(chan struct{})
m.tickerCh = make(chan struct{})

go func() {
lastMousePos := mouse.GetPosition()
for range tickerCh {
for range m.tickerCh {
log.Printf("mouse cursor checked at : %v\n", time.Now())
commCh := make(chan *cursorInfo)
go checkCursorChange(commCh, lastMousePos)
Expand All @@ -41,8 +43,18 @@ func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) (tickerCh cha
log.Printf("stopping cursor handler")
return
}()
}

return tickerCh
func (m *MouseCursorHandler) Trigger() {
//doing it the non-blocking sender way
select {
case m.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (m *MouseCursorHandler) Close() {
close(m.tickerCh)
}

func checkCursorChange(commCh chan *cursorInfo, lastMousePos *mouse.Position) {
Expand Down
23 changes: 18 additions & 5 deletions internal/pkg/service/screenChangeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type ScreenChangeHandler struct{}
type ScreenChangeHandler struct {
tickerCh chan struct{}
}

type screenInfo struct {
didScreenChange bool
currentPixelColor string
}

func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) {
func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) {

tickerCh = make(chan struct{})
s.tickerCh = make(chan struct{})

go func() {
screenSizeX, screenSizeY := robotgo.GetScreenSize()
pixelPointX := int(screenSizeX / 2)
pixelPointY := int(screenSizeY / 2)
lastPixelColor := robotgo.GetPixelColor(pixelPointX, pixelPointY)
for range tickerCh {
for range s.tickerCh {
log.Printf("screen change checked at : %v\n", time.Now())
commCh := make(chan *screenInfo)
go checkScreenChange(commCh, lastPixelColor, pixelPointX, pixelPointY)
Expand All @@ -44,14 +46,25 @@ func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) (tickerCh ch
log.Printf("stopping screen change handler")
return
}()
}

return tickerCh
func (s *ScreenChangeHandler) Trigger() {
//doing it the non-blocking sender way
select {
case s.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (s *ScreenChangeHandler) Close() {
close(s.tickerCh)
}

func checkScreenChange(commCh chan *screenInfo, lastPixelColor string, pixelPointX, pixelPointY int) {
currentPixelColor := robotgo.GetPixelColor(pixelPointX, pixelPointY)
// log.Printf("current pixel color: %v\n", currentPixelColor)
// log.Printf("last pixel color: %v\n", lastPixelColor)
//robotgo.MoveMouse(pixelPointX, pixelPointY)
if lastPixelColor != currentPixelColor {
commCh <- &screenInfo{
didScreenChange: true,
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ const (
)

type Instance interface {
Start(chan *activity.Type) chan struct{}
Start(chan *activity.Type)
Trigger()
Close()
}
29 changes: 13 additions & 16 deletions pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{
select {
case <-tickerWorker.C:
log.Printf("tracker worker working at %v\n", time.Now())
//time to ping all registered service handlers
//doing it the non-blocking sender way
for _, serviceHandler := range tracker.serviceHandlers {
select {
case serviceHandler <- struct{}{}:
default:
//service is blocked
}
//time to trigger all registered services
for service := range tracker.services {
service.Trigger()
}
case <-tickerHeartbeat.C:
log.Printf("tracker heartbeat checking at %v\n", time.Now())
Expand Down Expand Up @@ -69,9 +64,9 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{
//log.Printf("activity received: %#v\n", activity)
case <-quit:
log.Printf("stopping activity tracker\n")
//close all service handlers for a clean exit
for _, serviceHandler := range tracker.serviceHandlers {
close(serviceHandler)
//close all services for a clean exit
for service := range tracker.services {
service.Close()
}
return
}
Expand All @@ -88,12 +83,14 @@ func makeActivityMap() map[*activity.Type]time.Time {

func (tracker *Instance) registerHandlers(services ...service.Instance) {

if len(tracker.serviceHandlers) == 0 { //checking for multiple registration attempts
tracker.activityCh = make(chan *activity.Type, len(services)) // number based on types of activities being tracked
tracker.services = make(map[service.Instance]bool)
tracker.activityCh = make(chan *activity.Type, len(services)) // number based on types of activities being tracked

for _, service := range services {
tickerCh := service.Start(tracker.activityCh)
tracker.serviceHandlers = append(tracker.serviceHandlers, tickerCh)
for _, service := range services {
service.Start(tracker.activityCh)
if _, ok := tracker.services[service]; !ok { //duplicate registration prevention
tracker.services[service] = true
}

}
}
7 changes: 4 additions & 3 deletions pkg/tracker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package tracker
import (
"time"

"github.com/prashantgupta24/activity-tracker/internal/pkg/service"
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type Instance struct {
TimeToCheck time.Duration
activityCh chan *activity.Type
serviceHandlers []chan struct{}
TimeToCheck time.Duration
activityCh chan *activity.Type
services map[service.Instance]bool
}

type Heartbeat struct {
Expand Down

0 comments on commit e96d248

Please sign in to comment.