gator: Version 5.20
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / AnnotateListener.cpp
1 /**
2  * Copyright (C) ARM Limited 2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include "AnnotateListener.h"
10
11 #include <unistd.h>
12
13 #include "OlySocket.h"
14
15 struct AnnotateClient {
16         AnnotateClient *next;
17         int fd;
18 };
19
20 AnnotateListener::AnnotateListener() : mClients(NULL), mSock(NULL) {
21 }
22
23 AnnotateListener::~AnnotateListener() {
24         close();
25         delete mSock;
26 }
27
28 void AnnotateListener::setup() {
29         mSock = new OlyServerSocket(8082);
30 }
31
32 int AnnotateListener::getFd() {
33         return mSock->getFd();
34 }
35
36 void AnnotateListener::handle() {
37         AnnotateClient *const client = new AnnotateClient();
38         client->fd = mSock->acceptConnection();
39         client->next = mClients;
40         mClients = client;
41 }
42
43 void AnnotateListener::close() {
44         mSock->closeServerSocket();
45         while (mClients != NULL) {
46                 ::close(mClients->fd);
47                 AnnotateClient *next = mClients->next;
48                 delete mClients;
49                 mClients = next;
50         }
51 }
52
53 void AnnotateListener::signal() {
54         const char ch = 0;
55         AnnotateClient **ptr = &mClients;
56         AnnotateClient *client = mClients;
57         while (client != NULL) {
58                 if (write(client->fd, &ch, sizeof(ch)) != 1) {
59                         ::close(client->fd);
60                         AnnotateClient *next = client->next;
61                         delete client;
62                         *ptr = next;
63                         client = next;
64                         continue;
65                 }
66                 ptr = &client->next;
67                 client = client->next;
68         }
69 }