Skip to content

Commit

Permalink
Adding GeoLookup message service and handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Feb 21, 2015
1 parent 1351854 commit 1170403
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
72 changes: 43 additions & 29 deletions src/github.com/getlantern/flashlight/geolookup/geolookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,83 @@ import (
)

const (
sleepTime = time.Second * 10
messageType = `GeoLookup`

endpoint = "/geolookup"
sleepTime = time.Second * 10
)

var (
log = golog.LoggerFor("geolookup-flashlight")

startMutex sync.Mutex
uichannel *ui.UIChannel
clientCity = make(chan *geolookup.City)
service *ui.Service
lookupMutex sync.Mutex
lookupData *geolookup.City
)

func watchForUserIP() {
func getUserGeolocationData() *geolookup.City {
lookupMutex.Lock()
defer lookupMutex.Unlock()

var err error

if lookupData != nil {
// We already looked up IP's information.
return lookupData
}

for {
if !geolookup.UsesDefaultHTTPClient() {
// Will look up only if we're using a proxy.
geodata, err := geolookup.LookupCity("")
lookupData, err = geolookup.LookupCity("")
if err == nil {
clientCity <- geodata
// We got what we wanted, no need to query for it again, let's exit.
return
return lookupData
}
}
// Sleep if the proxy is not ready yet of any error happened.
time.Sleep(sleepTime)
}

// We should not be able to reach this point.
panic("unreachable position")
}

// StartService initializes the geolocation websocket service.
func StartService() error {
// Looking up client's information.
go watchForUserIP()
return start()
}

startMutex.Lock()
func start() (err error) {

if uichannel == nil {
start()
} else {
helloFn := func(write func([]byte) error) error {
var b []byte
var err error

// Waiting for the city to be discovered.
city := <-clientCity
city := getUserGeolocationData()

message := ui.Envelope{
Type: messageType,
Message: city,
}

if b, err = json.Marshal(city); err != nil {
if b, err = json.Marshal(message); err != nil {
return fmt.Errorf("Unable to marshal geolocation information: %q", err)
}

// Writing data to channel.
uichannel.Out <- b
return write(b)
}

startMutex.Unlock()
if service, err = ui.Register(messageType, helloFn); err != nil {
return fmt.Errorf("Unable to register channel: %q", err)
}

go read()

return nil
}

func start() {
uichannel = ui.NewChannel(endpoint, func(func([]byte) error) error {
// We don't really need to to anything here yet but we could add code for
// allowing looking up other IPs.
return nil
})

log.Debugf("Serving geodata information at %v", uichannel.URL)
func read() {
for _ = range service.In {
// Discard message, just in case any message is sent to this service.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/getlantern/flashlight/ui"
)

const (
messageType = `ProxiedSites`
)

// deltaMessage is the struct of the message we're expecting from the client.
type deltaMessage struct {
Delta proxiedsites.Delta `json:"Message"`
Expand All @@ -38,7 +42,7 @@ func Configure(cfg *proxiedsites.Config) {
} else if delta != nil {
// Sending delta.
message := ui.Envelope{
Type: ui.MessageTypeProxiedSites,
Type: messageType,
Message: delta,
}
b, err := json.Marshal(message)
Expand All @@ -60,7 +64,7 @@ func start() (err error) {

// Hello message.
message := ui.Envelope{
Type: ui.MessageTypeProxiedSites,
Type: messageType,
Message: proxiedsites.ActiveDelta(),
}

Expand All @@ -73,7 +77,7 @@ func start() (err error) {
return write(b)
}

if service, err = ui.Register(ui.MessageTypeProxiedSites, helloFn); err != nil {
if service, err = ui.Register(messageType, helloFn); err != nil {
return fmt.Errorf("Unable to register channel: %q", err)
}

Expand Down
5 changes: 0 additions & 5 deletions src/github.com/getlantern/flashlight/ui/envelope.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package ui

// Values for Envelope.Type
const (
MessageTypeProxiedSites = `ProxiedSites`
)

// Envelope is a struct that wraps messages and associates them with a type.
type Envelope struct {
Type string
Expand Down
10 changes: 5 additions & 5 deletions src/github.com/getlantern/ui/app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var app = angular.module('app', [
if (typeof Messages[envelope.Type] != 'undefined') {
Messages[envelope.Type].call(this, envelope.Message);
} else {
console.log('Got unknown message type: ' + message.Type);
console.log('Got unknown message type: ' + envelope.Type);
};
});

Expand Down Expand Up @@ -119,9 +119,9 @@ var app = angular.module('app', [
});

var methods = {
'send': function(data) {
'send': function(messageType, data) {
console.log('request to send.');
ds.send(JSON.stringify(data))
ds.send(JSON.stringify({'Type': messageType, 'Message': data}))
}
};

Expand All @@ -134,12 +134,12 @@ var app = angular.module('app', [
update: function() {
console.log('UPDATE');
// dataStream.send(JSON.stringify($rootScope.updates));
DataStream.send($rootScope.updates)
DataStream.send('ProxiedSites', $rootScope.updates)
},
get: function() {
console.log('GET');
// dataStream.send(JSON.stringify({ action: 'get' }));
DataStream.send({'action': 'get'});
DataStream.send('ProxiedSites', {'action': 'get'});
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/github.com/getlantern/ui/app/js/services.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

angular.module('app.services', [])
// Messages service will return a map of callbacks that handle websocket
// messages sent from the flashlight process.
.service('Messages', function($rootScope) {

var fnList = {
'GeoLookup': function(data) {
console.log('Got GeoLookup information: ', data);
},
'ProxiedSites': function(data) {
if (!$rootScope.entries) {
console.log("Initializing proxied sites entries", data.Additions);
Expand Down

0 comments on commit 1170403

Please sign in to comment.