/**
 * Stores information on sip devices currently registered in the server.
 * This implementation won't scale to thousands of devices.
 *
 * @author Pedro Sanders
 * @since v1
 */
function LocationService() {
    const HashMap = Packages.java.util.HashMap
    const db = new HashMap()

    this.put = (addressOfRecord, contactAddress) => { db.put(addressOfRecord.toString(), contactAddress) }

    // It would be nice if we could enforce the use of 'tel' scheme for DIDs requests
    this.get = addressOfRecord => {
        if (addressOfRecord instanceof Packages.javax.sip.address.URI ||
            addressOfRecord instanceof Packages.javax.sip.address.SipURI) {

            const sipAddress = addressOfRecord.getScheme() + ":" + addressOfRecord.getUser() + '@' + addressOfRecord.getHost()

            return db.get(sipAddress) || db.get('tel:' + addressOfRecord.getUser())
        } if (k instanceof Packages.javax.sip.address.TelURL) {
            return db.get('tel:' + addressOfRecord.getPhoneNumber())
        }

        return db.get(addressOfRecord)
    }

    this.remove = k => { db.remove(k) }

    this.listAllAsJSON = () => {
        let s = []
        let aors = db.keySet().iterator()

        while(aors.hasNext()) {
            let key = aors.next()
            let tmp = {'addressOfRecord': key, 'contactAddress': db.get(key).toString()}
            s.push(tmp)
        }

        return JSON.stringify(s)
    }
}



@@ -289,63 +289,185 @@
     
-this.put = (addressOfRecord, contactAddress) =%3E %7B
+function getKey(route) %7B return route.sentByAddress + route.sentByPort + route.received + route.rport%7D%0A%0A    this.put = (addressOfRecord, route) =%3E %7B%0A        const routes =
  db.
-pu
+ge
 t(ad
@@ -483,36 +483,145 @@
 cord
-.toString(), contactAddress)
+) %7C%7C new HashMap()%0A        const routeKey = getKey(route)%0A        routes.put(routeKey, route)%0A        db.put(addressOfRecord, routes)%0A   
  %7D%0A%0A
@@ -718,16 +718,27 @@
 this.get
+AORContacts
  = addre
@@ -765,72 +765,19 @@
     
-if (addressOfRecord instanceof Packages.javax.sip.address.URI %7C%7C
+let routes%0A
 %0A   
@@ -781,20 +781,20 @@
         
-    
+if (
 addressO
@@ -849,17 +849,16 @@
 pURI) %7B%0A
-%0A
         
@@ -867,26 +867,25 @@
   const 
-sipAddress
+recordKey
  = addre
@@ -975,17 +975,16 @@
 tHost()%0A
-%0A
         
@@ -992,31 +992,33 @@
    r
-eturn db.get(sipAddress
+outes =  db.get(recordKey
 ) %7C%7C
@@ -1064,24 +1064,60 @@
 r())
-%0A        %7D if (k
+ %7C%7C new HashMap()%0A        %7D else if (addressOfRecord
  ins
@@ -1166,37 +1166,39 @@
  %7B%0A            r
-eturn
+outes =
  db.get('tel:' +
@@ -1221,72 +1221,182 @@
 .get
-PhoneNumber())%0A        %7D%0A%0A        return db.get(addressOfRecord)
+User()) %7C%7C new HashMap()%0A        %7D else %7B%0A            routes = db.get(addressOfRecord)%0A        %7D%0A%0A        const iterator = routes.values().iterator()%0A%0A        return iterator
 %0A   
@@ -1417,17 +1417,31 @@
 emove = 
-k
+addressOfRecord
  =%3E %7B db
@@ -1448,17 +1448,31 @@
 .remove(
-k
+addressOfRecord
 ) %7D%0A%0A   
@@ -1625,24 +1625,394 @@
 aors.next()%0A
+%0A            let i = db.get(key).values().iterator()%0A            let contactInfo = ''%0A%0A            while(i.hasNext()) %7B%0A                const rObj = i.next()%0A                const r = '%3C' + rObj.contactURI + '%3E;nat=' + rObj.nat + ';timestamp=' + rObj.registeredOn%0A                if (i.hasNext()) r = r + ','%0A                contactInfo = contactInfo + r%0A            %7D%0A%0A
             
@@ -2022,16 +2022,33 @@
  tmp = %7B
+%0A                
 'address
@@ -2066,49 +2066,64 @@
 key,
- 'contactAddress': db.get(key).toString()
+%0A                'contactInfo': contactInfo%0A            
 %7D%0A  

Improve nat traversal