SOL9 Sample: IPAddressWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * IPAddressWatcher.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



// SOL++2000
// 2008/07/28
#define _WINSOCK_DEPRECATED_NO_WARNINGS
 
#include <sol\ApplicationView.h>
#include <sol\ListView.h>
#include <sol\Thread.h>
#include <sol\StatusBar.h>

#include <sol\net\NetworkInterfaceInfo.h>
#include <sol\Stdio.h>
#include "resource.h"

#include <process.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")

namespace SOL {

/**
 * IPAddressWatcher thread class. Detect address change by Windows API NotifyAddrChange
 * and show the addresses to ListView.
 */
class IPAddressWatcherThread :public Thread {

private:
  NetworkInterfaceInfo info;
  ListView&            listView;
  StatusBar&           statusBar;
  bool                 looping;

public:
  IPAddressWatcherThread(ListView& list,StatusBar& sbar)
  :listView(list),
   statusBar(sbar) {
    looping = true;
  }

private:
  void listup()
  {
    info.listup();
    int c = info.getUpCount();  
    if (c >0) {
      listView.clear();

      for (int i = 0; i<c; i++) {
        String addr = _T("");
        info.getUpIPAddress(i, addr);
        TCHAR* items[2];
        items[0] =  _T("Up");
        items[1] = (TCHAR*)(const TCHAR*)addr;
        listView.insertLine(i, (const TCHAR**)items, 2);
      }
      if (info.isConnectedToInternet()) {
        statusBar.setText(0, 0, _T("Connected to Internet"));
      } else {
        statusBar.setText(0, 0, _T("Disconnected from Internet"));
      }
    }
  }

public:
  /**
   * Thread procedure
   */
  void run() {
    //printf("WatcherThread,Started\n");
    listup();
    while (looping) {

      //printf("Waiting NotifyAddrChange\n");
      DWORD ret = NotifyAddrChange(NULL, NULL);
      if (ret == NO_ERROR) {
        listup();
      } else {
        statusBar.setText(0, 0, _T("NotifyAddrChange:Error"));
      }  
    }
  }
};
  
/**
 * IPAddressWatcher UI class to watch change of ip addresses by using
 * IPAddressWatcherThread class.
 */
class IPAddressWatcher :public ApplicationView {

private:
  ListView  listView;
  StatusBar  statusBar;

  IPAddressWatcherThread thread;

public:
  /**
   * Constructor
   */
  IPAddressWatcher(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, args),
    thread(listView, statusBar)
  {
    Args ar;
    statusBar.create(this, NULL, ar);
    add(statusBar);

    ar.reset();
    ar.set(XmNstyle, LVS_REPORT);
    listView.create(this, NULL, ar);

    ListViewColumn items[] = {
      {_T("State"),     LVCFMT_LEFT, 100},
      {_T("IPAddress"),   LVCFMT_LEFT, 120},
    };

    listView.setColumn(items, 2);
    add(listView);

    addEventHandler(WM_CLOSE, this, 
        (Handler)&IPAddressWatcher::close, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&IPAddressWatcher::exit, NULL);


    restorePlacement();

    // Start the IPAddressWatcher thread.
    thread.start();
  }

private:
  long  size(Event& event)
  {
    int w, h;
    statusBar.getSize(w, h);
    int cw, ch;
    getClientSize(cw, ch);
    listView.reshape(0, 0, cw, ch-h, 1);

    statusBar.reshape(0, ch-h, cw, h, 1);
    return 1;
  }


private:
  void  exit(Action& action)
  {
    savePlacement();
    destroyWindow();
  }

private:
  long close(Event& event)
  {
    savePlacement();
    return defaultProc(event);
  }

};

}


// IPAddressWatcher Main
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();

  try {
    Application applet(name, argc, argv);
    
    Args args;
    IPAddressWatcher watcher(applet, name, args);
    watcher.realize();

    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}



Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.