git-svn-id: svn://elaine/murooma/trunk@122 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-07-30 19:41:56 +00:00
parent b0fe090d21
commit ce1c5fb3ac
5 changed files with 73 additions and 40 deletions

View file

@ -1,6 +1,6 @@
VERSION = 0.01
CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -g -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\"
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -g -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\"
LDFLAGS = -lrt -lzmq -lpthread -lportaudio
OBJ_SERVER = server.o

View file

@ -8,7 +8,6 @@
#include <zmq.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <sys/time.h>
#include <unistd.h>
#include <deque>
@ -16,9 +15,9 @@
#include <algorithm>
#include <thread>
#include <portaudio.h>
#include <iterator>
#include "chunk.h"
#include "utils.h"
#include "stream.h"
#include "zhelpers.hpp"
@ -49,17 +48,6 @@ void player()
std::string getMacAddress()
{
std::ifstream t("/sys/class/net/eth0/address");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
return str;
}
void control(Stream* stream)
{
zmq::context_t context(1);
@ -74,11 +62,7 @@ void control(Stream* stream)
s_send (worker, "ready");
while (1) {
std::string cmd = s_recv (worker);
istringstream iss(cmd);
vector<std::string> splitCmd;
copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(splitCmd));
for (size_t n=0; n<splitCmd.size(); ++n)
std::cout << "cmd: " << splitCmd[n] << "\n";
vector<std::string> splitCmd = split(cmd);
if (splitCmd.size() > 1)
{
if (splitCmd[0] == "buffer")

View file

@ -13,20 +13,11 @@
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "utils.h"
using namespace std;
std::string getMacAddress()
{
std::ifstream t("/sys/class/net/eth0/address");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
return str;
}
void control()
{
@ -36,15 +27,13 @@ void control()
// We use a string identity for ease here
string macAddress = getMacAddress();
worker.setsockopt(ZMQ_IDENTITY, macAddress.c_str(), macAddress.length());
worker.connect("tcp://127.0.0.1:10000");
worker.connect("tcp://127.0.0.1:123459");
// Tell the router we're ready for work
s_send (worker, "ready");
while (1) {
std::string cmd = s_recv (worker);
istringstream iss(cmd);
vector<std::string> splitCmd;
copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(splitCmd));
vector<std::string> splitCmd = split(cmd);
for (size_t n=0; n<splitCmd.size(); ++n)
std::cout << "cmd: " << splitCmd[n] << "\n";
s_send(worker, "ACK " + cmd);

View file

@ -6,8 +6,9 @@
#include <thread>
#include <istream>
#include "zhelpers.hpp"
#include "utils.h"
zmq::socket_t* client;
void receiver(zmq::socket_t* client)
{
@ -24,13 +25,20 @@ void receiver(zmq::socket_t* client)
}
void send(const std::string& address, const std::string& cmd)
{
s_sendmore (*client, address);
s_sendmore (*client, "");
s_send (*client, cmd);
}
int main () {
zmq::context_t context(2);
zmq::socket_t client (context, ZMQ_ROUTER);
client.bind("tcp://0.0.0.0:123459");
client = new zmq::socket_t(context, ZMQ_ROUTER);
client->bind("tcp://0.0.0.0:123459");
std::thread receiveThread(receiver, &client);
std::thread receiveThread(receiver, client);
while (true)
{
@ -39,9 +47,7 @@ int main () {
std::getline(std::cin, address);
std::getline(std::cin, cmd);
s_sendmore (client, address);
s_sendmore (client, "");
s_send (client, cmd);
send(trim(address), trim(cmd));
}
return 0;
}

54
utils.h Normal file
View file

@ -0,0 +1,54 @@
#ifndef UTILS_H
#define UTILS_H
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
static std::string getMacAddress()
{
std::ifstream t("/sys/class/net/eth0/address");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return trim(str);
}
std::vector<std::string> split(const std::string& str)
{
std::istringstream iss(str);
std::vector<std::string> splitStr;
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(splitStr));
return splitStr;
}
#endif