connection gwan with aerospike db in C - c

Hello.
First I'm sorry for my ita-english.
I want use gwan with aerospike but when run the servlet...problem.
I start with this example.c of aerospike. In file example.c I put gwan.h and this is the output ./gwan:
loading
hello.cs: to use .cs scripts, install C#..
hello.lua: to use .lua scripts, install Lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linking example.c: undefined symbol: g_namespace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To run G-WAN, you must fix the error(s) or remove this Servlet.
Inside example.c:
#include "gwan.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <aerospike/aerospike.h>
#include <aerospike/aerospike_key.h>
#include <aerospike/aerospike_query.h>
#include <aerospike/as_error.h>
#include <aerospike/as_key.h>
#include <aerospike/as_query.h>
#include <aerospike/as_record.h>
#include <aerospike/as_status.h>
#include <aerospike/as_val.h>
#include "example_utils.h"
const char TEST_INDEX_NAME[] = "test-bin-index";
bool query_cb(const as_val* p_val, void* udata);
void cleanup(aerospike* p_as);
bool insert_records(aerospike* p_as);
int
main(int argc, char* argv[])
{
if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {
exit(-1);
}
aerospike as;
example_connect_to_aerospike(&as);
example_remove_test_records(&as);
example_remove_index(&as, TEST_INDEX_NAME);
if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME))
{
cleanup(&as);
exit(-1);
}
if (! insert_records(&as)) {
cleanup(&as);
exit(-1);
}
if (! example_read_test_records(&as)) {
cleanup(&as);
exit(-1);
}
as_error err;
as_query query;
as_query_init(&query, g_namespace, g_set);
as_query_where_inita(&query, 1);
as_query_where(&query, "test-bin", as_integer_equals(7));
LOG("executing query: where test-bin = 7");
if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL)
!= AEROSPIKE_OK) {
LOG("aerospike_query_foreach() returned %d - %s", err.code,
err.message);
as_query_destroy(&query);
cleanup(&as);
exit(-1);
}
LOG("query executed");
as_query_destroy(&query);
cleanup(&as);
LOG("simple query example successfully completed");
return 0;
}
bool
query_cb(const as_val* p_val, void* udata)
{
if (! p_val) {
LOG("query callback returned null - query is complete");
return true;
}
as_record* p_rec = as_record_fromval(p_val);
if (! p_rec) {
LOG("query callback returned non-as_record object");
return true;
}
LOG("query callback returned record:");
example_dump_record(p_rec);
return true;
}
void
cleanup(aerospike* p_as)
{
example_remove_test_records(p_as);
example_remove_index(p_as, TEST_INDEX_NAME);
example_cleanup(p_as);
}
bool
insert_records(aerospike* p_as)
{
set
as_record rec;
as_record_inita(&rec, 1);
for (uint32_t i = 0; i < g_n_keys; i++) {
as_error err;
as_key key;
as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);
as_record_set_int64(&rec, "test-bin", (int64_t)i);
if (aerospike_key_put(p_as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {
LOG("aerospike_key_put() returned %d - %s", err.code, err.message);
return false;
}
}
LOG("insert succeeded");
return true;
}
how can connect aerospike with gwan?
Thank you

You need to #pragma link your aerospike library, and make sure all your required header files are in the right place. See G-WAN FAQ or read example code in the G-WAN tarball.
Also, in G-WAN the return code of the main function will be used as HTTP response code, so avoid return -1;.

undefined symbol: g_namespace
the error message is clear. As long as this variable is undefined your C servlet won't compile.
I don't know your library but this variable is probably defined in a library include file - or must be defined by the end user (you). Check the library documentation.

Detailed steps to run Aerospike C-client example with G-WAN,
Download and extract G-WAN server tar on your system
You can start the G-WAN server using ./gwan script present in extracted folder, e.g. ./gwan_linux64-bit/
Get Aerospike C-client from https://github.com/aerospike/aerospike-client-c, and install on your system
Copy example.c to ./gwan_linux64-bit/0.0.0.0_8080/#0.0.0.0/csp/
Make following changes to example.c,
Add following #pragma directive,
#pragma include "/home/user/aerospike-client-c/examples/utils/src/include/"
This will help search example_utils.h, which is necessary for all the example scripts in C-client.
Add following #pragma directive,
#pragma link "/home/user/aerospike-client-c/examples/utils/src/main/example_utils.c"
We shall have to link example_utils.c, as it has definitions of all util functions used in example scripts.
Make changes to the return values. Retun proper HTTP error codes.
Now, you are good to go. Run ./gwan server and access your webservice through browser, http://127.0.0.1:8080/?example.c

Related

GCC (C) - error: 'x' redeclared as different kind of symbol

I'm writing a package manager for the Termux terminal emulator on android using the APK format. The program is written in C and uses various arguments like 'sync', and 'remove'. However, the function I have written doesn't recognize the argument I have written for the name of the package to 'sync'. 'sync' is meant to download an apk from the fdroid repositories and open it using xdg-open (not yet implemented) using the name of the apk given in the arguments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void syncapk(char * apkname);
int main(int argc, char **argv)
{
if(argc==1) {
printf("Displaying help screen.\n");
} else if(argc>=2) {
if(strncmp(argv[1], "sync\n", 4) == 0) {
syncapk(argv[2]);
} else if(strncmp(argv[1], "upgrade", 7) == 0) {
printf("Upgrading all packages!\n");
} else if(strncmp(argv[1], "remove", 6) == 0) {
printf("Removing package!\n");
}
}
return 0;
}
void syncapk(char * apkname) {
printf("Syncing package: %s!\n", apkname);
char * synccmd = "fdroidcl download %s", apkname;
system(synccmd);
}
GCC (my compiler) says that the argument (the name of the apk I wish to download from the repositories) is 'redeclared as a different symbol'. I am fairly new to writing programs in C so feel free to critique other things, not just the problem itself and whether I could take a different approach completely.
This doesn't do what you think it does:
char * synccmd = "fdroidcl download %s", apkname;
This is defining a variable of type char * named synccmd and a variable of type char named apkname. The latter conflicts with the parameter of the same name, hence the error.
If you want to build a formatted string, you need to use sprintf to do that:
char synccmd[100];
sprintf(synccmd, "fdroidcl download %s", apkname);

error function declared but never defined

I have added a library to my c project in codeVision AVR. when I want to use it's functions receive this error:
function 'function name' is declared but never defined.
here is my code:
#include "pid.h"
#include <mega32.h>
PidType _pid;
void main(void)
{
//some uC hardware initializing codes which are removed here to simplify code
PID_Compute(&_pid);
while (1)
{
// Place your code here
}
}
in pid.h:
.
.
bool PID_Compute(PidType* pid);
.
.
and pid.c:
#include "pid.h"
.
.
bool PID_Compute(PidType* pid) {
if (!pid->inAuto) {
return false;
}
FloatType input = pid->myInput;
FloatType error = pid->mySetpoint - input;
pid->ITerm += (pid->ki * error);
if (pid->ITerm > pid->outMax)
pid->ITerm = pid->outMax;
else if (pid->ITerm < pid->outMin)
pid->ITerm = pid->outMin;
FloatType dInput = (input - pid->lastInput);
FloatType output = pid->kp * error + pid->ITerm - pid->kd * dInput;
if (output > pid->outMax)
output = pid->outMax;
else if (output < pid->outMin)
output = pid->outMin;
pid->myOutput = output;
pid->lastInput = input;
return true;
}
the ERROR:
function 'PID_Compute' declared, but never defined.
Where is the problem?
EDIT:
to add the library to my project I placed the .c and .h library files in the same folder that my main project file is:
and then #include "pid.h" in my main file:
#include "pid.h"
#include <mega32.h>
// Declare your global variables here
PidType _pid;
void main(void)
{
.
.
my error and warnings:
EDIT2:
I simplified the code and now can show you the entire code:
main code:
#include "pid.h"
PidType _pid;
void main(void)
{
PID_Compute(&_pid);
while (1)
{
}
}
pid.h:
#ifndef PID_H
#define PID_H
#include <stdbool.h>
typedef struct {
int i;
} PidType;
bool PID_Compute(PidType* pid);
#endif
pid.c:
#include "pid.h"
bool PID_Compute(PidType* pid) {
pid->i = 2;
return true;
}
thank you every body.
As you said, the pid.c was not added to the project.
for those who may face the same problem:
in codeVision AVR we have to add .c file to project from project->configure->files->input files->add
I addec .c file to project and the error went away.
From the screenshot with the tree view of your files it is clear that the file "pid.c" is not part of the project.
Move it into your project. Then it should build without that linker error.
This does not mean the location in the file system. I reference the "virtual" view of the IDE on your project.

libharu memory allocation failed while loading image

I have some C code trying to use libharu. Although I can use every function of this library (even UTF8) I can hardly draw images. Here is some very basic code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include "hpdf.h"
jmp_buf env;
#ifdef HPDF_DLL
void __stdcall
#else
void
#endif
error_handler (HPDF_STATUS error_no,
HPDF_STATUS detail_no,
void *user_data)
{
printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
(HPDF_UINT)detail_no);
longjmp(env, 1);
}
int main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[256];
HPDF_Image image;
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
pdf = HPDF_New (error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
/* error-handler */
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
font = HPDF_GetFont (pdf, "Helvetica", NULL);
page = HPDF_AddPage (pdf);
HPDF_Page_SetWidth (page, 550);
HPDF_Page_SetHeight (page, 500);
image = HPDF_LoadPngImageFromFile (pdf, "img.png");
HPDF_SaveToFile (pdf, fname);
HPDF_Free (pdf);
return 0;
}
When I compile this I have ERROR: error_no=1015, detail_no=0. I have found a similar post in stackoverflow: this. However although original poster said the problem is solved it hardly helped mine. I moved img.png to a folder and recompiled the file. Changed the code that says /home/name/path/to/img.png which is the direct path to image. Nothing works. I "always" have the same error, but when I change the name of file I have ERROR: error_no=1017, detail_no=2 which basicly means program cannot find image (according to reference of libharu) So I deduce that program finds img.png; but, it's strange but, cannot allocate the necessary memory. Which is weird because I cannot see any reason for this program not to allocate memory. I have every kind of permission.
I am using GCC 4.7.2 under Ubuntu Quantal Quetzal and libharu 2.3.0 RC2. Thank you for your help.
Hello Equalities of polynomials .
I also encountered the same problem when i integrated the haru sdk in my macOS environment.
The error_handler returned ERROR: error_no=1017, detail_no=2,and then i checked the official document for haru at http://libharu.sourceforge.net/error_handling.html query 0x1017 indicates that the file failed to open, so i suspect that the second parameter of the HPDF_LoadPngImageFromFile method needs to pass an exact png image file path, so after I modified it, the problem was solved, and I hope to help you.
code ad follow:
char filename1[255];
strcpy(filename1, "/Users/xx/Downloads/lusaceg.com.png");
image = HPDF_LoadPngImageFromFile (pdf, filename1);
Faithfully yours.

Qt: Downloading a file doesn't work

For three days now I try to come up with a workable code to simply download a file from an Url, but I had my fair share of problems so far and now I am at a point where I can't see the error.
First of all, my code:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.downloadArchive(QString("https://bitbucket.org/BattleClinic/evemon/downloads/EVEMon-binaries-1.8.1.4016.zip"), QCoreApplication::applicationDirPath(), QString("evemon.zip"));
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool downloadArchive(QString archiveUrl, QString saveToPath, QString archiveName);
private slots:
void downloadReadyRead();
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void downloadFinished();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
QNetworkAccessManager manager;
QFile *file;
QNetworkReply *reply;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->downloadLabel->hide();
ui->downloadProgressBar->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::downloadReadyRead()
{
if(file)
{
file->write(reply->readAll());
}
}
void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->downloadProgressBar->setMaximum(bytesTotal);
ui->downloadProgressBar->setValue(bytesReceived);
}
void MainWindow::downloadFinished()
{
downloadReadyRead();
file->flush();
file->close();
if(reply->error())
{
QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
}
reply->deleteLater();
reply = NULL;
delete file;
file = NULL;
ui->downloadLabel->hide();
ui->downloadProgressBar->hide();
}
bool MainWindow::downloadArchive(QString archiveUrl, QString saveToPath, QString archiveName)
{
QUrl url(archiveUrl);
if(archiveName.count() <= 0)
{
return false;
}
if(QFile::exists(saveToPath + "/" + archiveName))
{
QFile::remove(saveToPath + "/" + archiveName);
}
file = new QFile(saveToPath + "/" + archiveName);
if(!file->open(QIODevice::WriteOnly))
{
delete file;
file = NULL;
return false;
}
reply = manager.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
ui->downloadLabel->setText(QString("Downloading %1...").arg(archiveName));
ui->downloadLabel->show();
ui->downloadProgressBar->show();
return true;
}
Downloader.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Downloader
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
In mainwindow.cpp I'm checking for an error and print it to a MessageBox, in case there is one. The first error I had with the current code in the reply was "Unable to init SSL Context", because I'm requesting data from a https-site. I googled a while and found the solution to be two files called "libeay32.dll" and "ssleay32.dll" I had to copy to the executable's directory.
There are now no more errors within the reply (at least the MessageBox doesn't show up)....however, after the downloadFinished Function was executed, the downloaded file has a size of 0 kb, which makes me think, their wasn't at all a download happening.
I skipped the "if(reply->error())" statement and showed the MessageBox no matter what....I got an: "Unknown Error". Should an "Unknown Error" not set the "if(reply->error())" statement to true?
There is no problem with permissions either....I tried it as Admin....so no problem at creating the file itself.
Can anyone help me to get that code working?
Do I miss any dlls or an #include?
Thanks
Download with SSL:
connect(&http, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedHttp(QNetworkReply*)));
QNetworkRequest *req = new QNetworkRequest();
req->setUrl( QUrl("https://...") );
QSslConfiguration configSsl = QSslConfiguration::defaultConfiguration();
configSsl.setProtocol(QSsl::AnyProtocol);
req->setSslConfiguration(configSsl);
http.get(*req);
You need: libeay32.dll, libssl32.dll, ssleay32.dll (from OpenSSL) and Visual c++ 2008 SP1 redist package.
Make sure that it works for a non-ssl file. Then to add ssl support, try following the directions in the answers to this question:
Qt SSL support missing
Also try using qDebug in your code instead of the QMessageBox most of the time. It makes adding debugging lines easier, and it won't interrupt your code or fill your screen with QMessageBoxes.

How to use rolling log using log4c?

Can someone tell me how I can use the rolling log feature of log4c API library?
There is only documentation on the functions it provides and there are so many.
If anyone has used rolling log with log4c, would be great to see how to configure it and use it.
Add something like this to your .log4crc file:
<rollingpolicy name="myrollingpolicy"
type="sizewin"
maxsize="1024"
maxnum="10"
/>
<appender name="myrollingfileappender"
type="rollingfile"
logdir="."
prefix="myprefix"
layout="dated"
rollingpolicy="myrollingpolicy"
/>
Then you do logging like normal with:
#include <stdio.h>
#include "log4c.h"
int main(int argc, char** argv) {
int rc = 0;
log4c_category_t* mycat = NULL;
if (log4c_init()) {
printf("log4c_init() failed");
rc = 1;
}
else{
mycat = log4c_category_get("log4c.examples.helloworld");
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Hello World!");
/* Explicitly call the log4c cleanup routine */
if ( log4c_fini()){
printf("log4c_fini() failed");
}
}
return 0;
}
This is all available in the examples from the log4c source code
Since this is a 3 month old question, just wondering if the Wikipedia page was tried -- http://en.wikipedia.org/wiki/Log4c#Development_with_Log4C.

Resources