O O D B Design And Implementation - O D B Worker -

Joe

Thành viên VIP
21/1/13
2,690
1,244
113
Hi

(cont. of ODBConnect)

The ancient Chinese scholar Confucius said "If you explain it to me I'll forget it later. If you show it to me I'll remember it later. If you let me do it I'll understand it". So, have you guys tried to "implement" an UserList or ODBConnection ? Well, "Learning by Doing" as we used to say in the West. It's your own decision to forget or to remember or ... to understand this dry theme.

Let's start with the Relationship between ODBConnect and ODBWorker -its counterpart. The following image gives you an impression how they work together and how their relationship is built.



  1. Client App starts with an ODBConnect which is a connection to the ODB Server
  2. ODB Server receives the connection request and spawns an ODBWorker and hands out the Connection request to the new ODBWorker
  3. ODBWorker verifies the PW and UserID and either rejects the connection request (invalid PW or UserID) or accepts the connection
  4. ODBConnect is always the first who starts the dialog (e.g. read, update, getKeys, etc.)
  5. ODBWorker is always passive and waits for its partner's requests and always sends back a result which could be an Exception (e.g. wrong dbName, invalid key, etc.)
  6. etc.
  7. till ODBConnect sends a disconnect or is shutdown by whatever case
  8. ODBWorker closes the connection and terminates.

The Design of ODBConnect and ODBWorker bases on the requirements of DB activities. They are usually as following:

  • connect (or open) to a DB
  • read
  • update
  • delete
  • getKey or getKeys
  • lock/unlock
  • commit/rollback
  • close (and save)
  • save

All the activities happen upon a network (LAN/WAN/WEB) and the net is usually independent, unreliable. The implementation task is to build a solid connection on a reliable communication/Networking base. In the past it was hard to accomplish such requirements because it was slow and technology was obsolete (e.g. leased line with 64 kbps). Today, even the Web, the communication network is quite reliable and very fast (fiber optic, Mbps).

Our task, or to be more precise: YOUR task, is to design a Communication/Networking framework and from the framework you implement the ODBConnect and ODBWorker and both base on a hypothetical ODB Server which will be explained in the next section. You have two choices:
  • Socket: the traditional way
  • SocketChannel: the new way
As mentioned in previous sections the OO data could be big, very-very big. And over a network such as the WEB where "billions" of users put their heavy stuffs on the net, too (images, musics, etc.) Therefore the OO Data should be compressed before they are put on the net. It reduces the (net) last and shortens the transferring time (latency) and that means a better response time for the users. The compression technique is available in JAVA and it's the GZIP technology.

  • GZIPInputStream (uncompressed the OO Data)
  • GZIPOutputStream (compressed the OO Data)

The following coding snippet gives you an idea how to design and how to implement the Communication/Networking in JAVA:
PHP:
    soc = SocketChannel.open(new InetSocketAddress(dbHost, port));
    soc.socket().setReceiveBufferSize(32768); // 32KB
    soc.socket().setSendBufferSize(32768);
    // Authentication...........................
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    bao.write(EnDecrypt.encrypt(pw+":"+uID, true)).getBytes()); // encrypted the PW/ID
    send(soc, bao);
    ODBResult res = getResult(soc);
    if (res.bool) {
      uID = res.ID.substring(1);
      priv = res.ID.charAt(0) & 0x0F;
      panicShutdown();
      con = true;
    } else {
      try {
        soc.shutdownInput();
        soc.shutdownOutput();
        soc.close();
      } catch (Exception ex) { }
      throw new Exception("Unable to connect to:"+dbHost+":"+port);
    }
  }
  ...
  private void send(SocketChannel soc, ByteArrayOutputStream bao) throws Exception {
    byte[] buf = bao.toByteArray(); // get the content
    bao.reset();
    // GZIP the OO Data before send
    GZIPOutputStream go = new GZIPOutputStream(bao, true);
    go.write(buf);
    go.flush();
    go.close();
    //
    soc.write(ByteBuffer.wrap(bao.toByteArray()));
    soc.socket().getOutputStream().flush();
  }
  //
  private ODBResult getResult(SocketChannel soc)  throws Exception {
    ByteBuffer bbuf =  ByteBuffer.allocate(32768);
    ODBOutputStream bao = new ODBOutputStream( );
    while (true) {
      bbuf.clear();
      int le = soc.read(bbuf);
      bao.write(bbuf.array(), 0, le);
      if (le < 32768) break;
    }
    // NOW unGZIP the OO data
    byte[] buf = new byte[32768];
    GZIPInputStream gi = new GZIPInputStream(new ODBInputStream(bao.toByteArray()));
    bao.reset(); // inflate the content of ODBOutputStream............
    for (int p = gi.read(buf); p > 0; p = gi.read(buf)) bao.write(buf, 0, p);
    gi.close(); // EOF gzip
    //
    ObjectInputStream ois = new ObjectInputStream(new ODBInputStream(bao.toByteArray()));
    return (ODBResult) ois.readObject(); // retrieve the result.
  }
It's similar with ODBWorker and the "tranfer vehicle" ODBResult. A serialized POJO:
PHP:
public class ODBResult implements java.io.Serializable {
  private static final long serialVersionUID = 1L;
  public java.util.ArrayList<String> list;
  public String ID, err = "";
  public boolean bool = false;
  public byte[] obj;
}
....back to the ancient Chinese scholar Confucius: "If I explain it to YOU YOU'll forget it later. If I show it to YOU YOU'll remember it later. If I let YOU do it YOU'll understand it" ;)

The source of ODBWorker API will be bundled and released when this series terminates
(Next: OODB Design and Implementation: ODMS)
 
Sửa lần cuối: