Commit 89032270 authored by Gabriel Margiani's avatar Gabriel Margiani
Browse files

first commit

parents
Loading
Loading
Loading
Loading

src/3phone.h

0 → 100644
+36 −0
Original line number Diff line number Diff line
/**
 * This file is part of 3phone
 * Copyright (C) 2015 Gabriel Margiani
 *
 * 3phone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 3phone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 3phone.  If not, see <http://www.gnu.org/licenses/>.
 **/

#ifndef __3P_3PHONE_H__
#define __3P_3PHONE_H__

#include "error.h"

// pjloglevel
#ifndef DEBUG
#define LOGLEVEL 0
#else
#define LOGLEVEL 5
#endif

#define MAX_CALLS 20

// C !!
bool exit;

#endif

src/account.h

0 → 100644
+34 −0
Original line number Diff line number Diff line
/**
 * This file is part of 3phone
 * Copyright (C) 2015 Gabriel Margiani
 *
 * 3phone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 3phone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 3phone.  If not, see <http://www.gnu.org/licenses/>.
 **/


#ifndef __P3_ACCOUNT_H__
#define __P3_ACCOUNT_H__

#include <pjsua2.hpp>

class p3Account : public pj::Account {
	public:
		p3Account() {};
		~p3Account() {};

		virtual void onRegState(OnRegStateParam &prm);
		virtual void onIncomingCall(OnIncomingCallParam &iprm);
}

#endif

src/client.cpp

0 → 100644
+116 −0
Original line number Diff line number Diff line
/**
 * This file is part of 3phone
 * Copyright (C) 2015 Gabriel Margiani
 *
 * 3phone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 3phone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 3phone.  If not, see <http://www.gnu.org/licenses/>.
 **/

#include <iostream>
#include <random>
#include <ctime>

#include <boost/algorithm/string.hpp>

#include "client.h"
#include "error.h"

std::map<std::string, std::string> p3::client::command_shorthands {
	{"c", "call"}
};

p3::client::client(int ac, char *av[]) : argv(av, av+ac) {
	argv.pop_front();

	std::random_device rd;
    std::default_random_engine e(rd());
    std::uniform_int_distribution<int> uniform_dist(200, 900);
    id = std::to_string(uniform_dist(e)) + std::to_string(std::time(NULL));
	connection = nullptr;

	try {
		p3::mQueue ma(p3::mQueue::DefaultId, false);

		connection = new mQueue(id, true);
		ma.send(p3::protocol::HELLO, id);
		p3::mQueueMessage m = ma.receive(30);

		if (m.get_command() != p3::protocol::OK) {
			throw new p3::perror("client.connection", "cannot connect to server " + m.get_value());
		}
	} catch (p3::perror & e) {
		if (connection != nullptr) {
			delete connection;
		}
		throw;
	}
}

p3::client::~client() {
	connection->send(p3::protocol::BYE, "Client Terminating");
	delete connection;
}

std::string p3::client::get_input(std::string q, p3::client::inputType type) {
	std::string in;
	if (!argv.empty()) {
		in = argv.front();
		argv.pop_front();
	} else {
		do {
			std::cout << q << " > " << std::flush; 
			std::cin >> in;
			boost::trim(in);
		} while (in == "");
	}
	boost::to_lower(in);

	if (type == COMMAND && p3::client::command_shorthands.count(in) == 1) {
		return p3::client::command_shorthands[in];
	}
	return in;
}

void p3::client::run() {
	bool r = true;
	connection->send(p3::protocol::COMMAND, get_input("", COMMAND));
	while (r) {
		p3::mQueueMessage m = connection->receive(-1);
		switch (m.get_command()) {
			case p3::protocol::ASK_NUMBER:
				connection->send(p3::protocol::ANSWER, get_input(m.get_value(), NUMBER));
				continue;
			case p3::protocol::OK:
				std::cout << m.get_value() << std::endl;
				break;
			case p3::protocol::ERROR:
				std::cout << "ERROR: " << m.get_value() << std::endl;
				break;
			case p3::protocol::FATAL_ERROR:
				std::cout << "FATAL ERROR: " << m.get_value() << std::endl;
			case p3::protocol::BYE:
				std::cout << "done." << std::endl;
				r = false;
				continue;
			default:
				std::cout << "Unknown server command. " << m.get_value() << std::endl;
		}
		std::string in = get_input("", COMMAND);
		if (in == "exit" || in == "quit") {
			break;
		} else {
			connection->send(p3::protocol::COMMAND, in);
		}
	}
}

src/client.h

0 → 100644
+53 −0
Original line number Diff line number Diff line
/**
 * This file is part of 3phone
 * Copyright (C) 2015 Gabriel Margiani
 *
 * 3phone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 3phone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 3phone.  If not, see <http://www.gnu.org/licenses/>.
 **/

#ifndef __P3_CLIENT_H__
#define __P3_CLIENT_H__

#include <string>
#include <list>
#include <map>

#include "msgq.h"

namespace p3 {

	class client {

		mQueue * connection;
		std::string id;

		std::list<std::string> argv;

		static std::map<std::string, std::string> command_shorthands;

		enum inputType {
			COMMAND,
			NUMBER
		};

		std::string get_input(std::string question, inputType is_command);

		public:
			client(int argc, char *argv[]);
			~client();

			void run();
	};
}
#endif

src/error.h

0 → 100644
+42 −0
Original line number Diff line number Diff line
/**
 * This file is part of 3phone
 * Copyright (C) 2015 Gabriel Margiani
 *
 * 3phone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 3phone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 3phone.  If not, see <http://www.gnu.org/licenses/>.
 **/

#ifndef __P3_ERROR_H__
#define __P3_ERROR_H__

#include <string>

namespace p3 {
	class perror : std::exception {
		std::string context;
		std::string error;

		public:
			perror(std::string c, std::string e) : context(c), error(e) {};
			~perror() throw () {};

			const char * getContext() { return context.c_str(); }
			const char * getError() { return error.c_str(); }

			const char* what() const throw() {
				return (context + ": " + error).c_str();
			}
	};
}

#endif