B4A Class [B4X] Remote MongoDB Connector (RMDBC)

This is a similar solution to RDC that works with MongoDB databases.
MongoDB is a simple and powerful NoSQL database. It is a bit like KVS (key value store) class. However unlike KVS which is a simple data store, MongoDB is a full database with powerful analytics features.

The B4J server acts as a middleware between the clients and the database.
B4J MongoDB library: https://www.b4x.com/android/forum/threads/mongodb-documents-database.72160/

There is nothing to configure in the server side except of the server port. The server code is simple and
can be extended as needed.

Unlike RDC where the queries are set in the server side, in RMDBC the commands are set in the clients. This means that this solution as-is is more appropriate for trusted environments. You can add validation code to the server to make it more secure.

Client code

1. Add the types declaration to the main module:
B4X:
Type MDBCommand (Collection As String, Maps As List, Method As String, Limit As Int)
Type MDBResult (Success As Boolean, Tag As Object)

2. Add MDBRequestManager class to your project. It depends on OkHttpUtils2 and RandomAccessFile library.
Note that it is compatible with B4A, B4i and B4J.

3. Initialize the manager:
B4X:
manager.Initialize(Me, "mdb", "http://192.168.0.6:51042/rmdbc") 'RMDBC server ip and port

4. Handle the events:
B4X:
Sub JobDone (j As HttpJob)
   If manager.HandleJob(j) Then Return
   'handle other HttpJobs here
End Sub

Public Sub MDB_NonFindResult (Result As MDBResult)
   Log("Command completed. Success =" & Result.Success)
   If Result.Success Then GetResults
End Sub

Public Sub MDB_FindResult (Result As MDBResult, Documents As List)
   If Result.Success Then
     ListView1.Clear
     For Each m As Map In Documents
       ListView1.AddSingleLine(m.Get("_id") & ": " & m.Get("total"))
     Next
   End If
End Sub

See the attached B4A and B4i client examples:


upload_2016-10-23_16-56-28.png
upload_2016-10-23_16-56-35.png
 

Attachments

  • upload_2016-10-23_16-56-17.png
    upload_2016-10-23_16-56-17.png
    35.9 KB · Views: 552
  • RMDBC_Server_B4J.zip
    1.6 KB · Views: 781
  • RMDBC_B4i.zip
    6.5 KB · Views: 576
  • RMDBC_B4A.zip
    9.5 KB · Views: 703

johndb

Active Member
Licensed User
Longtime User
Unlike RDC where the queries are set in the server side, in RMDBC the commands are set in the clients. This means that this solution as-is is more appropriate for trusted environments. You can add validation code to the server to make it more secure.
Is it possible to encrypt the client-server communications using the encryption features of RandomAccessFile?
 

leitor79

Active Member
Licensed User
Longtime User
Thank you very much for sharing this!

I'm getting red messages in the log screen and no window is shown. Is this an exception?

2018-03-31 21:30:04.798:INFO::main: Logging initialized @247ms to org.eclipse.jetty.util.log.StdErrLog
2018-03-31 21:30:05.590:INFO:eek:ejs.Server:main: jetty-9.4.z-SNAPSHOT
2018-03-31 21:30:05.656:INFO:eek:ejs.session:main: DefaultSessionIdManager workerName=node0
2018-03-31 21:30:05.656:INFO:eek:ejs.session:main: No SessionScavenger set, using defaults
2018-03-31 21:30:05.658:INFO:eek:ejs.session:main: Scavenging every 600000ms
2018-03-31 21:30:05.665:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@5e57643e{/,file:///C:/Users/Leo/Desarrollo%20Local/Android/RMDBC/Objects/www,AVAILABLE}
2018-03-31 21:30:05.682:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\Users\Leo\Desarrollo Local\Android\RMDBC\Objects\logs\b4j-2018_04_01.request.log
2018-03-31 21:30:05.705:INFO:eek:ejs.AbstractConnector:main: Started ServerConnector@2d127a61{HTTP/1.1,[http/1.1]}{0.0.0.0:51042}
2018-03-31 21:30:05.706:INFO:eek:ejs.Server:main: Started @1158ms


Regards!
 
Top