2011-08-28 15:45:42 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 Alex Yatskov
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without
|
|
|
|
// restriction, including without limitation the rights to use,
|
|
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following
|
|
|
|
// conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
2011-09-03 16:05:56 +00:00
|
|
|
#include <stdio.h>
|
2011-08-28 15:45:42 +00:00
|
|
|
#include "metacall.hpp"
|
|
|
|
|
|
|
|
using namespace metacall;
|
|
|
|
|
2011-09-03 16:05:56 +00:00
|
|
|
|
2011-09-05 03:26:55 +00:00
|
|
|
//
|
2012-02-19 22:34:52 +00:00
|
|
|
// Defines
|
2011-09-05 03:26:55 +00:00
|
|
|
//
|
|
|
|
|
2012-02-20 02:47:09 +00:00
|
|
|
#define TEST_C_STRING
|
2012-02-19 22:34:52 +00:00
|
|
|
#define TEST_BASIC_STRING
|
2012-04-09 21:01:57 +00:00
|
|
|
#define TEST_VECTOR
|
2012-04-15 02:32:30 +00:00
|
|
|
#define TEST_LIST
|
2012-02-19 22:34:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// C string
|
|
|
|
//
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
#ifdef TEST_C_STRING
|
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
static void testCStringAnsiImp(const char str[]) {
|
|
|
|
printf("[testCStringAnsiImp]: '%s'\n", str);
|
2011-09-05 03:26:55 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
static void testCStringUnicodeImp(const wchar_t str[]) {
|
|
|
|
printf("[testCStringUnicodeImp]: '%S'\n", str);
|
2012-03-04 03:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testCString(Binding* binding, Protocol* protocol) {
|
2012-04-09 21:01:57 +00:00
|
|
|
binding->bind(FPARAM(testCStringAnsiImp));
|
|
|
|
binding->bind(FPARAM(testCStringUnicodeImp));
|
2012-03-04 03:10:36 +00:00
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
const char* stringsAnsi[] = { "Hello world", "", NULL };
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2012-04-09 21:01:57 +00:00
|
|
|
protocol->invoke("testCStringAnsiImp", stringsAnsi[i]);
|
2012-03-04 03:10:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
const wchar_t* stringsUnicode[] = { L"Hello world", L"", NULL };
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2012-04-09 21:01:57 +00:00
|
|
|
protocol->invoke("testCStringUnicodeImp", stringsUnicode[i]);
|
2012-03-04 03:10:36 +00:00
|
|
|
}
|
2012-02-19 22:34:52 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-19 22:34:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// std::basic_string
|
|
|
|
//
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
#ifdef TEST_BASIC_STRING
|
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
static void testBasicStringAnsiImp(const std::string& str) {
|
|
|
|
printf("[testBasicStringAnsiImp]: '%s'\n", str.c_str());
|
2012-02-19 22:34:52 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
static void testBasicStringUnicodeImp(const std::wstring& str) {
|
|
|
|
wprintf(L"[testBasicStringUnicodeImp]: '%S'\n", str.c_str());
|
2012-02-19 22:34:52 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 03:10:36 +00:00
|
|
|
static void testBasicString(Binding* binding, Protocol* protocol) {
|
2012-04-09 21:01:57 +00:00
|
|
|
binding->bind(FPARAM(testBasicStringAnsiImp));
|
|
|
|
binding->bind(FPARAM(testBasicStringUnicodeImp));
|
2012-03-04 03:10:36 +00:00
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
std::string stringsAnsi[] = { std::string("Hello world"), std::string() };
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2012-04-09 21:01:57 +00:00
|
|
|
protocol->invoke("testBasicStringAnsiImp", stringsAnsi[i]);
|
2012-03-04 03:10:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
std::wstring stringsUnicode[] = { std::wstring(L"Hello world"), std::wstring() };
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2012-04-09 21:01:57 +00:00
|
|
|
protocol->invoke("testBasicStringUnicodeImp", stringsUnicode[i]);
|
2012-03-04 03:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-06 13:52:10 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-19 22:34:52 +00:00
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
//
|
|
|
|
// std::vector
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef TEST_VECTOR
|
|
|
|
|
|
|
|
static void testVectorImp(const std::vector<float>& vec) {
|
2012-04-15 02:32:30 +00:00
|
|
|
printf("[testVectorImp]: ");
|
2012-04-09 21:01:57 +00:00
|
|
|
|
|
|
|
for (std::vector<float>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) {
|
|
|
|
printf("%f ", *iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testVector(Binding* binding, Protocol* protocol) {
|
|
|
|
binding->bind(FPARAM(testVectorImp));
|
|
|
|
|
|
|
|
std::vector<float> vec;
|
|
|
|
vec.push_back(3.14159f);
|
|
|
|
vec.push_back(2.71828f);
|
|
|
|
vec.push_back(1.61803f);
|
|
|
|
|
|
|
|
protocol->invoke("testVectorImp", vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-09-05 03:26:55 +00:00
|
|
|
|
2012-04-15 02:32:30 +00:00
|
|
|
//
|
|
|
|
// std::list
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef TEST_LIST
|
|
|
|
|
|
|
|
static void testListImp(const std::list<float>& lst) {
|
|
|
|
printf("[testListImp]: ");
|
|
|
|
|
|
|
|
for (std::list<float>::const_iterator iter = lst.begin(); iter != lst.end(); ++iter) {
|
|
|
|
printf("%f ", *iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testList(Binding* binding, Protocol* protocol) {
|
|
|
|
binding->bind(FPARAM(testListImp));
|
|
|
|
|
|
|
|
std::list<float> lst;
|
|
|
|
lst.push_back(3.14159f);
|
|
|
|
lst.push_back(2.71828f);
|
|
|
|
lst.push_back(1.61803f);
|
|
|
|
|
|
|
|
protocol->invoke("testListImp", lst);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-03 16:05:56 +00:00
|
|
|
//
|
2011-09-04 03:34:30 +00:00
|
|
|
// Program entry
|
2011-09-03 16:05:56 +00:00
|
|
|
//
|
|
|
|
|
2012-02-19 15:02:35 +00:00
|
|
|
int main(int, char *[]) {
|
2011-09-05 03:26:55 +00:00
|
|
|
const int port = 1234;
|
|
|
|
|
|
|
|
Server server;
|
|
|
|
if (!server.start(port)) {
|
|
|
|
perror("Cannot start server\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Client client;
|
|
|
|
if (!client.connect("localhost", port)) {
|
|
|
|
perror("Cannot connect to server\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-20 02:47:09 +00:00
|
|
|
Binding& binding = server.binding();
|
|
|
|
Protocol& protocol = client.protocol();
|
2011-09-03 16:05:56 +00:00
|
|
|
|
2011-09-05 03:26:55 +00:00
|
|
|
do {
|
2012-04-06 13:52:10 +00:00
|
|
|
#ifdef TEST_C_STRING
|
2012-03-04 03:10:36 +00:00
|
|
|
testCString(&binding, &protocol);
|
2012-04-06 13:52:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TEST_BASIC_STRING
|
2012-03-04 03:10:36 +00:00
|
|
|
testBasicString(&binding, &protocol);
|
2012-04-06 13:52:10 +00:00
|
|
|
#endif
|
2012-02-19 22:34:52 +00:00
|
|
|
|
2012-04-09 21:01:57 +00:00
|
|
|
#ifdef TEST_VECTOR
|
|
|
|
testVector(&binding, &protocol);
|
|
|
|
#endif
|
|
|
|
|
2012-04-15 02:32:30 +00:00
|
|
|
#ifdef TEST_LIST
|
|
|
|
testList(&binding, &protocol);
|
|
|
|
#endif
|
|
|
|
|
2011-09-05 03:26:55 +00:00
|
|
|
server.advance();
|
|
|
|
client.advance();
|
|
|
|
}
|
|
|
|
while (server.clientCount() > 0);
|
2011-09-03 16:05:56 +00:00
|
|
|
|
2011-08-28 15:45:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|