Thursday, 4 February 2016

Model View Control (MVC) pattern usage in C++

MVC (Model-View-Controller) pattern is often used in Web based applications, where "View" is your actual HTML content displayed on browser, where as "Controller" is the code that gather dynamic data and generates content within the HTML. Finally, "Model" talks about actual content, probably stored in DataBase or XML, and your business rules transform these contents based on user actions.
As I mentioned, MVC is most widely used in web based applications, but some how I liked this pattern and tried using it in my C++ application.
Scenario is, I have a module in C++ which generates set of System Information, and displays them to user. Since, they are system information, they keep change, and I have to update at view the same. Hope you understood.

Since I cannot get you what I wrote in my module, but I can surely show you a sample code, (the code is simple and self explanatory)

/* * * * * * * * * * * * * * *
* File       : mvc-class.cpp
* Author     : Kiran Puranik
* Copyrights : no copy rights :)
* * * * * * * * * * * * * * */

#include <iostream>

using namespace std;

/* Model class */
class SystemInfoModel {
    string m_InfoName;
    string m_InfoValue;
    string Get_InfoName (void) {
        return m_InfoName;
    string Get_InfoValue (void) {
        return m_InfoValue;
    void Set_InfoName (string info_name) {
        if (!info_name.empty()) {
            m_InfoName = info_name;
    void Set_InfoValue (string info_val) {
        if (!info_val.empty()) {
            m_InfoValue = info_val;

/* View class */
class SystemInfoView {

    void Print_SystemInfo (string name, string val) {
        cout << "|---------------------------------|" << endl;
        cout << "|    System Info    |    Value    |" << endl;
        cout << "|---------------------------------|" << endl;
        cout << "|   " << name << "    |   " << val << "   |" << endl;

/* Controller class*/
class SystemInfoController {

    SystemInfoModel m_SysModel; //\ reference for Model and View
    SystemInfoView  m_SysView;  ///
    /* CTR */
    SystemInfoController (SystemInfoModel si_model, SystemInfoView si_view) {
        m_SysModel = si_model;
        m_SysView  = si_view;
    /* User control APIs */
    void Set_SystemInfo_Name (string si_name) {
        if (!si_name.empty()) {
            m_SysModel.Set_InfoName (si_name);
    void Set_SystemInfo_Value (string si_val) {
        if (!si_val.empty()) {
            m_SysModel.Set_InfoValue (si_val);
    string Get_SystemInfo_Name (void) {
        return m_SysModel.Get_InfoName();
    string Get_SystemInfo_Value (void) {
        return m_SysModel.Get_InfoValue();
    /* Now, bind the Model with View */
    void Update_System_Info (void) {
        m_SysView.Print_SystemInfo (m_SysModel.Get_InfoName(), m_SysModel.Get_InfoValue());

/* Now, usage part of it ----------- */
int main ()
    // system info model instatiation
    SystemInfoModel siModel;
    siModel.Set_InfoName ("Memory Usage"); // looks dirty isn't it. But usually they are
    siModel.Set_InfoValue ("1024 KB");   //
fetched from system units/or other utilities
    // system info view instatiation
    SystemInfoView  siView;
    // now, the controller
    SystemInfoController siControl (siModel, siView);
    // let's display the content now
    siControl.Update_System_Info ();
    // now, the system info has changed, value is different
    siControl.Set_SystemInfo_Value ("2048 KB");
    // and display updated content
    siControl.Update_System_Info ();
    // isn't it a great pattern !!!!
    return 0;

Tuesday, 2 February 2016

Create your own "offline" repository for apt-get

It's a nightmare, if you do not have connected to internet for your Debian/Ubuntu machine. I use to struggle to install dependent packages after first installation of my Debian. Untill I connect to internet I cannot install packages and untill I install relevant packages I am no way on internet.
I have found an alternative, where I get offline debian package collection in one of my directory and I will do #apt-get install from there. Let me list how to do that in 3 steps.

Step 1: Collect all available DEB files:
1. Your Debian/Ubuntu distribution DVD might have come with deb installer distribution. Check your cdrom, for .../pool/main/ directory, and if it has deb files, then 'hola', copy them into your local directory.
2. You will definitely get download deb packages from cyber cafe and get them in thumb-drive and copy them to your local machine.
3. Check "/var/cache/apt/archives" in a Debian/Ubuntu machine, (may be your friend's), which has all the packages pre-installed. If it is not cleared/deleted, you will get them all in one place.

So, by now I have deb files copied in my local machine and I kept them in /opt/deb-packages/ location.

Step 2: Create a list:
Goto /opt/deb-packages/ directory and run below command,

#dpkg-scanpackages .  /dev/null  |  gzip -9c > Packages.gz

This shall take some time, give yourself a break.

Step 3: Now, add sources list:
Open sources.list file from /etc/apt
#gedit  /etc/apt/sources.list

and, add below line at the bottom of file,
deb  file:/opt/deb-packages/  ./

That's it. You are ready to install your packages.

Run, #apt-get  update
and let it update all list.

Enjoy running #apt-get install <your-package-name>
and see your package getting installed offline.