Merge lp:~sergiusens/nuntium/deactivate into lp:nuntium

Proposed by Sergio Schvezov
Status: Merged
Approved by: Tony Espy
Approved revision: 84
Merged at revision: 77
Proposed branch: lp:~sergiusens/nuntium/deactivate
Merge into: lp:nuntium
Prerequisite: lp:~sergiusens/nuntium/select_that
Diff against target: 260 lines (+111/-37)
2 files modified
mediator.go (+24/-1)
ofono/modem.go (+87/-36)
To merge this branch: bzr merge lp:~sergiusens/nuntium/deactivate
Reviewer Review Type Date Requested Status
Alfonso Sanchez-Beato Approve
Tony Espy Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+236724@code.launchpad.net

Commit message

Syncing upload/download operations with activation/deactivation per request.
Moving all the ofono context property checks and reflection logic to proper functions for easier reuse and readability.

To post a comment you must log in.
lp:~sergiusens/nuntium/deactivate updated
82. By Sergio Schvezov

Log improv

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Tony Espy (awe) wrote :

So this basically looks good, although I had a few comments that I'd like you to review before approving.

review: Needs Fixing
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

address the comments inline, will proceed to fixing now

lp:~sergiusens/nuntium/deactivate updated
83. By Sergio Schvezov

Deactivating context if it was activated on scope exit

84. By Sergio Schvezov

active toggling improvements including new dbus error type checks and log handling

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Tony Espy (awe) wrote :

Looks good!

review: Approve
Revision history for this message
Alfonso Sanchez-Beato (alfonsosanchezbeato) wrote :

LGTM, +1

Revision history for this message
Alfonso Sanchez-Beato (alfonsosanchezbeato) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'mediator.go'
--- mediator.go 2014-10-02 01:16:17 +0000
+++ mediator.go 2014-10-02 01:16:17 +0000
@@ -25,6 +25,7 @@
25 "io/ioutil"25 "io/ioutil"
26 "log"26 "log"
27 "os"27 "os"
28 "sync"
2829
29 "launchpad.net/nuntium/mms"30 "launchpad.net/nuntium/mms"
30 "launchpad.net/nuntium/ofono"31 "launchpad.net/nuntium/ofono"
@@ -44,6 +45,7 @@
44 NewMSendReqFile chan struct{ filePath, uuid string }45 NewMSendReqFile chan struct{ filePath, uuid string }
45 outMessage chan *telepathy.OutgoingMessage46 outMessage chan *telepathy.OutgoingMessage
46 terminate chan bool47 terminate chan bool
48 contextLock sync.Mutex
47}49}
4850
49//TODO these vars need a configuration location managed by system settings or51//TODO these vars need a configuration location managed by system settings or
@@ -164,12 +166,21 @@
164}166}
165167
166func (mediator *Mediator) getMRetrieveConf(mNotificationInd *mms.MNotificationInd) {168func (mediator *Mediator) getMRetrieveConf(mNotificationInd *mms.MNotificationInd) {
169 mediator.contextLock.Lock()
170 defer mediator.contextLock.Unlock()
171
167 preferredContext, _ := mediator.telepathyService.GetPreferredContext()172 preferredContext, _ := mediator.telepathyService.GetPreferredContext()
168 mmsContext, err := mediator.modem.ActivateMMSContext(preferredContext)173 mmsContext, err := mediator.modem.ActivateMMSContext(preferredContext)
169 if err != nil {174 if err != nil {
170 log.Print("Cannot activate ofono context: ", err)175 log.Print("Cannot activate ofono context: ", err)
171 return176 return
172 }177 }
178 defer func() {
179 if err := mediator.modem.DeactivateMMSContext(mmsContext); err != nil {
180 log.Println("Issues while deactivating context:", err)
181 }
182 }()
183
173 if err := mediator.telepathyService.SetPreferredContext(mmsContext.ObjectPath); err != nil {184 if err := mediator.telepathyService.SetPreferredContext(mmsContext.ObjectPath); err != nil {
174 log.Println("Unable to store the preferred context for MMS:", err)185 log.Println("Unable to store the preferred context for MMS:", err)
175 }186 }
@@ -185,6 +196,7 @@
185 } else {196 } else {
186 storage.UpdateDownloaded(mNotificationInd.UUID, filePath)197 storage.UpdateDownloaded(mNotificationInd.UUID, filePath)
187 }198 }
199
188 mediator.NewMRetrieveConfFile <- mNotificationInd.UUID200 mediator.NewMRetrieveConfFile <- mNotificationInd.UUID
189}201}
190202
@@ -360,6 +372,9 @@
360}372}
361373
362func (mediator *Mediator) uploadFile(filePath string) (string, error) {374func (mediator *Mediator) uploadFile(filePath string) (string, error) {
375 mediator.contextLock.Lock()
376 defer mediator.contextLock.Unlock()
377
363 preferredContext, _ := mediator.telepathyService.GetPreferredContext()378 preferredContext, _ := mediator.telepathyService.GetPreferredContext()
364 mmsContext, err := mediator.modem.ActivateMMSContext(preferredContext)379 mmsContext, err := mediator.modem.ActivateMMSContext(preferredContext)
365 if err != nil {380 if err != nil {
@@ -368,6 +383,12 @@
368 if err := mediator.telepathyService.SetPreferredContext(mmsContext.ObjectPath); err != nil {383 if err := mediator.telepathyService.SetPreferredContext(mmsContext.ObjectPath); err != nil {
369 log.Println("Unable to store the preferred context for MMS:", err)384 log.Println("Unable to store the preferred context for MMS:", err)
370 }385 }
386 defer func() {
387 if err := mediator.modem.DeactivateMMSContext(mmsContext); err != nil {
388 log.Println("Issues while deactivating context:", err)
389 }
390 }()
391
371 proxy, err := mmsContext.GetProxy()392 proxy, err := mmsContext.GetProxy()
372 if err != nil {393 if err != nil {
373 return "", err394 return "", err
@@ -376,5 +397,7 @@
376 if err != nil {397 if err != nil {
377 return "", err398 return "", err
378 }399 }
379 return mms.Upload(filePath, msc, proxy.Host, int32(proxy.Port))400 mSendRespFile, uploadErr := mms.Upload(filePath, msc, proxy.Host, int32(proxy.Port))
401
402 return mSendRespFile, uploadErr
380}403}
381404
=== modified file 'ofono/modem.go'
--- ofono/modem.go 2014-10-02 01:16:17 +0000
+++ ofono/modem.go 2014-10-02 01:16:17 +0000
@@ -29,6 +29,7 @@
29 "reflect"29 "reflect"
30 "strconv"30 "strconv"
31 "strings"31 "strings"
32 "time"
3233
33 "launchpad.net/go-dbus/v1"34 "launchpad.net/go-dbus/v1"
34)35)
@@ -38,6 +39,11 @@
38 contextTypeMMS = "mms"39 contextTypeMMS = "mms"
39)40)
4041
42const (
43 ofonoAttachInProgressError = "org.ofono.AttachInProgress"
44 ofonoInProgressError = "org.ofono.InProgress"
45)
46
41type OfonoContext struct {47type OfonoContext struct {
42 ObjectPath dbus.ObjectPath48 ObjectPath dbus.ObjectPath
43 Properties PropertiesType49 Properties PropertiesType
@@ -243,32 +249,101 @@
243 if context.isActive() {249 if context.isActive() {
244 return context, nil250 return context, nil
245 }251 }
246 log.Println("Trying to activate context on", context.ObjectPath)252 if err := context.toggleActive(true, modem.conn); err == nil {
247 obj := modem.conn.Object("org.ofono", context.ObjectPath)253 return context, nil
248 _, err = obj.Call(CONNECTION_CONTEXT_INTERFACE, "SetProperty", "Active", dbus.Variant{true})254 } else {
255 log.Println("Failed to activate for", context.ObjectPath, ":", err)
256 }
257 }
258 return OfonoContext{}, errors.New("no context available to activate")
259}
260
261//DeactivateMMSContext deactivates the context if it is of type mms
262func (modem *Modem) DeactivateMMSContext(context OfonoContext) error {
263 if context.isTypeInternet() {
264 return nil
265 }
266
267 return context.toggleActive(false, modem.conn)
268}
269
270func activationErrorNeedsWait(err error) bool {
271 if dbusErr, ok := err.(*dbus.Error); ok {
272 return dbusErr.Name == ofonoInProgressError || dbusErr.Name == ofonoAttachInProgressError
273 }
274 return false
275}
276
277func (context OfonoContext) toggleActive(state bool, conn *dbus.Connection) error {
278 log.Println("Trying to set Active property to", state, "for context on", state, context.ObjectPath)
279 obj := conn.Object("org.ofono", context.ObjectPath)
280 for i := 0; i < 3; i++ {
281 _, err := obj.Call(CONNECTION_CONTEXT_INTERFACE, "SetProperty", "Active", dbus.Variant{state})
249 if err != nil {282 if err != nil {
250 log.Printf("Cannot Activate interface on %s: %s", context.ObjectPath, err)283 log.Printf("Cannot set Activate to %t (try %d/3) interface on %s: %s", state, i+1, context.ObjectPath, err)
284 if activationErrorNeedsWait(err) {
285 time.Sleep(2 * time.Second)
286 }
251 } else {287 } else {
252 return context, nil288 return nil
253 }289 }
254 }290 }
255 return OfonoContext{}, errors.New("no context available to activate")291 return errors.New("failed to activate context")
292}
293
294func (oContext OfonoContext) isTypeInternet() bool {
295 if v, ok := oContext.Properties["Type"]; ok {
296 return reflect.ValueOf(v.Value).String() == contextTypeInternet
297 }
298 return false
299}
300
301func (oContext OfonoContext) isTypeMMS() bool {
302 if v, ok := oContext.Properties["Type"]; ok {
303 return reflect.ValueOf(v.Value).String() == contextTypeMMS
304 }
305 return false
256}306}
257307
258func (oContext OfonoContext) isActive() bool {308func (oContext OfonoContext) isActive() bool {
259 return reflect.ValueOf(oContext.Properties["Active"].Value).Bool()309 return reflect.ValueOf(oContext.Properties["Active"].Value).Bool()
260}310}
261311
312func (oContext OfonoContext) hasMessageCenter() bool {
313 return oContext.messageCenter() != ""
314}
315
316func (oContext OfonoContext) messageCenter() string {
317 if v, ok := oContext.Properties["MessageCenter"]; ok {
318 return reflect.ValueOf(v.Value).String()
319 }
320 return ""
321}
322
323func (oContext OfonoContext) messageProxy() string {
324 if v, ok := oContext.Properties["MessageProxy"]; ok {
325 return reflect.ValueOf(v.Value).String()
326 }
327 return ""
328}
329
330func (oContext OfonoContext) name() string {
331 if v, ok := oContext.Properties["Name"]; ok {
332 return reflect.ValueOf(v.Value).String()
333 }
334 return ""
335}
336
262func (oContext OfonoContext) GetMessageCenter() (string, error) {337func (oContext OfonoContext) GetMessageCenter() (string, error) {
263 if msc := reflect.ValueOf(oContext.Properties["MessageCenter"].Value).String(); msc != "" {338 if oContext.hasMessageCenter() {
264 return msc, nil339 return oContext.messageCenter(), nil
265 } else {340 } else {
266 return "", errors.New("context setting for the Message Center value is empty")341 return "", errors.New("context setting for the Message Center value is empty")
267 }342 }
268}343}
269344
270func (oContext OfonoContext) GetProxy() (proxyInfo ProxyInfo, err error) {345func (oContext OfonoContext) GetProxy() (proxyInfo ProxyInfo, err error) {
271 proxy := reflect.ValueOf(oContext.Properties["MessageProxy"].Value).String()346 proxy := oContext.messageProxy()
272 // we need to support empty proxies347 // we need to support empty proxies
273 if proxy == "" {348 if proxy == "" {
274 return proxyInfo, nil349 return proxyInfo, nil
@@ -309,33 +384,8 @@
309 }384 }
310385
311 for _, context := range contexts {386 for _, context := range contexts {
312 var name, contextType, msgCenter, msgProxy string387 if (context.isTypeInternet() && context.isActive() && context.hasMessageCenter()) || context.isTypeMMS() {
313 var active bool388 if context.ObjectPath == preferredContext || context.isActive() {
314 for k, v := range context.Properties {
315 if reflect.ValueOf(k).Kind() != reflect.String {
316 continue
317 }
318 k = reflect.ValueOf(k).String()
319 switch k {
320 case "Name":
321 name = reflect.ValueOf(v.Value).String()
322 case "Type":
323 contextType = reflect.ValueOf(v.Value).String()
324 case "MessageCenter":
325 msgCenter = reflect.ValueOf(v.Value).String()
326 case "MessageProxy":
327 msgProxy = reflect.ValueOf(v.Value).String()
328 case "Active":
329 active = reflect.ValueOf(v.Value).Bool()
330 }
331 }
332 log.Println("Check context valid for - Name", name,
333 "| Context type:", contextType,
334 "| MessageCenter:", msgCenter,
335 "| MessageProxy:", msgProxy,
336 "| Active:", active)
337 if (contextType == contextTypeInternet && active && msgCenter != "") || contextType == contextTypeMMS {
338 if context.ObjectPath == preferredContext || active {
339 mmsContexts = append([]OfonoContext{context}, mmsContexts...)389 mmsContexts = append([]OfonoContext{context}, mmsContexts...)
340 } else {390 } else {
341 mmsContexts = append(mmsContexts, context)391 mmsContexts = append(mmsContexts, context)
@@ -343,6 +393,7 @@
343 }393 }
344 }394 }
345 if len(mmsContexts) == 0 {395 if len(mmsContexts) == 0 {
396 log.Printf("non matching contexts:\n %+v", contexts)
346 return mmsContexts, errors.New("No mms contexts found")397 return mmsContexts, errors.New("No mms contexts found")
347 }398 }
348 return mmsContexts, nil399 return mmsContexts, nil

Subscribers

People subscribed via source and target branches