Archive

Author Archive

using libcurl to download files

July 25th, 2010 thomas No comments

this example shows how to use curl for downloading a file in c++ with a class and a progress callback.

this is pseudocode, means you need some header for this to work, etc :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

class MyClass;

typedef struct job_info_t {
 int jobID;
 MyClass *parent;
} struct job_info_t;

int progress_callback(void *data, double download_total_size, double download_total_size_done, double ultotal, double uldone)
{
    job_info_t *jinfo = (job_info_t *)data;
    if(!jinfo) return 1;
   
    jinfo->parent->reportProgress(jinfo->jobID, download_total_size, download_total_size_done);
    return 0;
}

void MyClass::reportProgress(int jobID, double download_total_size, double download_total_size_done)
{
    printf("DLFile-%04d|progress: %0.3f\n", jobID, download_total_size_done / download_total_size);
}

int MyClass::downloadFile(int jobID, std::string localFile, string url)
{
    job_info jinfo;
    jinfo.parent = this;
    jinfo.jobID = jobID;
   
    CURL *curl = curl_easy_init();
    if(!curl)
    {
        printf("DLFile-%04d|error creating curl: %s\n", jobID, localFile.string().c_str());
        printf("DLFile-%04d|download URL: %s\n", jobID, url.c_str());
        return 1;
    }
    FILE *outFile = fopen(localFile.string().c_str(), "wb");
    if(!outFile)
    {
        perror("error opening file");
        printf("DLFile-%04d|error opening local file: %s.\n", jobID, localFile.string().c_str());
        printf("DLFile-%04d|download URL: %s\n", jobID, url.c_str());
        return 2;
    }

    char *curl_err_str[CURL_ERROR_SIZE];
    memset(curl_err_str, 0, CURL_ERROR_SIZE);

    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_URL,              url.c_str());
   
    // logging stuff
//  curl_easy_setopt(curl, CURLOPT_STDERR,           InstallerLog::getSingleton()->getLogFilePtr());
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,      curl_err_str[0]);

    // progess stuff
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS,       0);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA,     &jinfo);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &progress_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA,        outFile);

    // http related settings
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,   1); // follow redirects
    curl_easy_setopt(curl, CURLOPT_AUTOREFERER,      1); // set the Referer: field in requests where it follows a Location: redirect.
    curl_easy_setopt(curl, CURLOPT_MAXREDIRS,        20);
    curl_easy_setopt(curl, CURLOPT_USERAGENT,        "YourUserAgent");
    curl_easy_setopt(curl, CURLOPT_FILETIME,         1);
   
   
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    fclose(outFile);

    // print curl error if existing
    if(res != CURLE_OK)
    {
        printf("DLFile-%04d| CURL returned: %d\n", jobID, res);
        printf("DLFile-%04d| CURL error: %s\n", jobID, curl_err_str);
        return 3;
    }
    return 0;
}
Categories: coding, cpp Tags:

automatic crash logging on linux using gdb

July 15th, 2010 thomas No comments

The service we wrote for linux has still some segfaults and other problems, so we needed a way of automatically logging and restarting broken servers. When using the crashhandler mode of our init script, the whole service will be wrapped in gdb. Upon crash, it logs the callstack and the last 3 frames to a file and quits. The init script will then restart the service again.

you can view the init-script code here

Categories: coding Tags:

install latest lighttpd 1.5 under debian

July 14th, 2010 thomas No comments

simple and straight forward:

1
2
3
4
5
6
7
8
9
10
apt-get install build-essential libtool autoconf libglib2.0-dev libpcre3-dev zlib1g-dev libbz2-dev automake1.9  libssl-dev libldap2-dev

wget http://cgit.lighttpd.net/lighttpd/lighttpd-1.x/snapshot/lighttpd-1.x-trunk.tar.gz
tar xvfz lighttpd-1.x-trunk.tar.gz
cd lighttpd-1.x-trunk
sh autogen.sh
./configure --prefix=/usr --with-openssl --with-ldap
make -j3
sudo make install
cd ..

you will also want spawn-fcgi:

1
2
3
4
5
6
7
8
wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.3.tar.gz
tar xvfz spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi*
sh autogen.sh
./configure --prefix=/usr
make
sudo make install
cd ..

and php:

1
apt-get install php5-xmlrpc php5-sqlite php5-snmp php5-pspell php5-recode php5-mysql php5-mhash php5-mcrypt php5-ldap  php5-interbase php5-imap php5-gmp php5-gd php5-dev php5-curl php5-common php5-cli php5-cgi php-pear php5-xcache

and some init scripts:

1
2
3
4
5
6
7
wget http://thomasfischer.biz/wp-content/2010/07/lighttpd.txt
mv lighttpd.txt /etc/init.d/lighttpd
chmod +x /etc/init.d/lighttpd

wget http://thomasfischer.biz/wp-content/2010/07/spawn-fcgi.txt
mv spawn-fcgi.txt /etc/init.d/spawn-fcgi
chmod +x /etc/init.d/spawn-fcgi
Categories: server Tags:

extended XEN wiki a bit

July 13th, 2010 thomas No comments

so the XEN Networking page is really nice, but misses some actual usage examples, so i started with that and added our setup there:

http://wiki.xensource.com/xenwiki/XenNetworking#head-c68ea7b3a6b235b59401445549210309cdac6a82

Categories: xen Tags:

working a bit on litesql

June 29th, 2010 thomas No comments

created two tickets for litesql:
- MSVS compile problem: 27 (fixed already)
- CMake buildsystem improvements: 28

Categories: coding Tags:

working with litesql

June 27th, 2010 thomas No comments

reported a bug that was quickly fixed by gulliver: http://sourceforge.net/apps/trac/litesql/ticket/27

Categories: coding Tags:

finding out the linux distribution and version in CMake

June 23rd, 2010 thomas No comments

actually its quite hacky, but works well here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   # use the LSB stuff if possible :)
   EXECUTE_PROCESS(
      COMMAND cat /etc/lsb-release
      COMMAND grep DISTRIB_ID
      COMMAND awk -F= "{ print $2 }"
      COMMAND tr "\n" " "
      COMMAND sed "s/ //"
      OUTPUT_VARIABLE LSB_ID
      RESULT_VARIABLE LSB_ID_RESULT
   )
   EXECUTE_PROCESS(
      COMMAND cat /etc/lsb-release
      COMMAND grep DISTRIB_RELEASE
      COMMAND awk -F= "{ print $2 }"
      COMMAND tr "\n" " "
      COMMAND sed "s/ //"
      OUTPUT_VARIABLE LSB_VER
      RESULT_VARIABLE LSB_VER_RESULT
   )
   #message("LSB output: ${LSB_ID_RESULT}:${LSB_ID} ${LSB_VER_RESULT}:${LSB_VER}")
   if(NOT ${LSB_ID} STREQUAL "")
      # found some, use it :D
      set(INSTALLER_PLATFORM "${LSB_ID}-${LSB_VER}" CACHE PATH "Installer chosen platform")
   else(NOT ${LSB_ID} STREQUAL "")
      set(INSTALLER_PLATFORM "linux-generic" CACHE PATH "Installer chosen platform")
   endif(NOT ${LSB_ID} STREQUAL "")
Categories: coding Tags:

tuning the eclipse look (and feel)

June 10th, 2010 thomas No comments

well, to say it right off, the default eclipse GUI for me is:

  • bloated with features
  • far too big
  • unusable without tuning

following are some hints and tips how to tune eclipse to your likings:

a) tune fontsize in gtk
get the gtk-chtheme program. Under gentoo:

1
2
emerge -av x11-themes/gtk-engines x11-themes/gtk-chtheme
gtk-chtheme

select a theme you like and use the font button on the lower left side of the dialog. (for example i use Verdana regular, size 6)

b) tune eclipse fontsize:
open via the menu in eclipse:
Window -> Preferences : type “font” in the search bar on the top left side.

select “General -> Appearance -> Colors and Fonts”
type “font” in the new search bar on the right side.

Change the fonts as you like.

Categories: coding Tags:

new personal record

June 9th, 2010 thomas No comments

working day from 8:45 am to 12:57pm …

Categories: website Tags:

embedding Ogre 1.7 into wxWidgets 2.9 cross platform

May 17th, 2010 thomas No comments

well, its harder than you might think, but i finally found this to be working quite well:

wxutils.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * File:   wxutils.h
 * Author: thomas
 *
 * Created on May 12, 2010, 9:23 AM
 */


#ifndef _WXUTILS_H
#define _WXUTILS_H

#include <wx/wx.h>
#include <string.h>

std::string getWindowHandle(wxWindow *window);

#endif  /* _WXUTILS_H */

wxutils.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "wxutils.h"

#include <iostream>
#include <sstream>

// this is in a separate file to reduce the clutter in the global namespace and their conflicts

#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
# ifdef __WXGTK__
#include <gdk/gdk.h>
#include <gtk/gtk.h> // just this should suffice as it should include gdk.h itself
/* Seems to be needed under Linux */
//#include <wx/gtk/win_gtk.h>
#include "wx/gtk/private/win_gtk.h"
#include <gdk/gdkx.h>
#include <GL/glx.h>

#if  wxCHECK_VERSION(2, 9, 0)
#define piz(wxwin) WX_PIZZA((wxwin)->m_wxwindow)
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(((GtkWidget*)piz(wxwin))->window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#endif

#ifdef __WXX11__
#include "wx/x11/privx.h"
#define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
#endif

# endif //__WXGTK__
#endif //OGRE_PLATFORM_LINUX

std::string getWindowHandle(wxWindow *window)
{
#if WIN32
    std::stringstream sstr;
    size_t hWnd = (size_t)window->GetHandle();
    sstr << hWnd;
    std::string result = sstr.str();
    return result;
#else

    // TODO: Someone test this. you might to use "parentWindowHandle" if this
    // does not work.  Ogre 1.2 + Linux + GLX platform wants a string of the
    // format display:screen:window, which has variable types ulong:uint:ulong.
    GtkWidget* widget = window->GetHandle();
    //gtk_widget_set_double_buffered (widget, FALSE);
    gtk_widget_realize( widget );   // Mandatory. Otherwise, a segfault happens.
    //XSync(GDK_WINDOW_XDISPLAY(widget->window), False);
   
    Display* display = GDK_WINDOW_XDISPLAY( widget->window );

   
    //Window wid = GetXWindow(window);
    Window wid = GDK_WINDOW_XWINDOW( widget->window );

    std::stringstream sstr;
    /* Get the right display (DisplayString() returns ":display.screen") */
    std::string displayStr = DisplayString( display );
    displayStr = displayStr.substr( 1, ( displayStr.find( ".", 0 ) - 1 ) );

    int screenNum = DefaultScreen( display );

    /* Put all together */
    printf("using display: %s\n", displayStr.c_str());
    printf("using screen: %d\n", screenNum);
    printf("using window: 0x%x\n", wid);

    // old format:
    sstr << displayStr << ':' << screenNum << ':' << wid;
    std::string result = sstr.str();

    printf("getWindowHandle() = %s\n", result.c_str());
    return result;
#endif
}

how to use (panel_viewport is the widget that should show the 3d content):

1
2
3
4
5
6
...
mWindow = mRoot->initialise(false);
Ogre::NameValuePairList params;
params["externalWindowHandle"] = getWindowHandle(panel_viewport);
mWindow = Ogre::Root::getSingleton().createRenderWindow("OgreRenderWindow", 640, 480, false, &params);
...

works great so far under windows 7 and ubuntu 9.10 with Ogre 1.7 and wxWidgets 2.9

Categories: coding, cpp Tags: