* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
* @brief
*/
+#include <exception>
#include <map>
#include <poll.h>
#include <client-channel.h>
#include <server-channel.h>
#include <common-types.h>
+#include <connection-exception.h>
std::map<int, int> m_sockets;
using namespace AskUser::Protocol;
struct ServerCallbacks : public IServerCallbacks {
- ServerCallbacks() : m_channel(nullptr) {}
+ ServerCallbacks() : m_channel(nullptr), responseCounter(0) {}
virtual void newConnection(ConnectionFd fd, const Credentials &creds) {
printf("call newConnection fd: %d credentials = { label: %s uid: %s }\n", fd,
virtual void popup(ConnectionFd fd, RequestId id, Privilege &&priv) {
printf("call popup %s \n", priv.c_str());
if (m_channel)
- m_channel->popupResponse(fd, id, rand() % (ASKUSER_ALLOW_FOREVER + 1));
+ m_channel->popupResponse(fd, id, responseCounter++ % (ASKUSER_ALLOW_FOREVER + 1));
}
void setChannel(ServerChannel *ptr) {
private:
ServerChannel *m_channel;
+ int responseCounter;
};
void server(void) {
switch (fd[i].fd) {
case 0:
if (fd[i].revents & POLLIN) {
- UNUSED int ret = scanf("%s", privilege);
+ int ret = scanf("%1024s", privilege);
+ if (ret != 1) {
+ printf("Invalid argument\n");
+ break;
+ }
chan->popupRequest(privilege);
}
break;
int main(){
int com;
printf("0 - server, 1 - send popup\n>");
- UNUSED int ret = scanf("%d", &com);
-
- switch(com) {
- case 0:
- server();
- break;
- case 1:
- client();
- break;
+ int ret = scanf("%d", &com);
+ if (ret != 1) {
+ printf("Invalid argument\n");
+ exit(EXIT_FAILURE);
+ }
+
+ try {
+ switch(com) {
+ case 0:
+ server();
+ break;
+ case 1:
+ client();
+ break;
+ default:
+ printf("Invalid argument\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ catch (const ConnectionException &e) {
+ printf("Connection exception occured: %s\n", e.what());
+ }
+ catch (const std::exception &e) {
+ printf("Standard exception occured: %s\n", e.what());
+ }
+ catch (...) {
+ printf("Unknown exception occured\n");
}
return 0;