File: workerthread.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (132 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "workerthread.h"
#include "network/named_pipe.h"
#include "network/named_pipe_client.h"
#include "network/cl_indexer_reply.h"
#include "network/cl_indexer_request.h"
#include "network/np_connections_server.h"
#include "network/clindexerprotocol.h"
#include "libctags/libctags.h"
#include "utils.h"
#include <stdlib.h>
#include <cstdio>
#include <memory>

WorkerThread::WorkerThread(eQueue<clNamedPipe*> *queue)
		: m_queue(queue)
{
}

WorkerThread::~WorkerThread()
{
}

void WorkerThread::start()
{
	printf("INFO: WorkerThread: Started\n");
	while ( !testDestroy() ) {
		clNamedPipe *conn(NULL);
		if (!m_queue->get(conn, 100)) {
			continue;
		}

		if (conn) {
			std::auto_ptr<clNamedPipe> p( conn );
			// get request from the client
			clIndexerRequest req;
			if ( !clIndexerProtocol::ReadRequest(conn, req) ) {
				continue;
			}

			char *tags(NULL);
			// create fies for the requested files
			for (size_t i=0; i<req.getFiles().size(); i++) {

#ifdef __DEBUG
				printf("------------------------------------------------------------------\n");
				printf("INFO: Source        : %s\n", req.getFiles().at(i).c_str());
				printf("INFO: Command       : %d\n", req.getCmd());
				printf("INFO: CTAGS options : %s\n", req.getCtagOptions().c_str());
				printf("INFO: Database      : %s\n", req.getDatabaseFileName().c_str());
#endif

				char *new_tags = ctags_make_tags(req.getCtagOptions().c_str(), req.getFiles().at(i).c_str());
				if (tags && new_tags) {
					// re-allocate the buffer to containt the new tags + 2 chars: 1 for terminating null and one for the '\n'
					// that will be appended
					char *ptmp = (char*)malloc(strlen(tags) + strlen(new_tags) + 2);
					memset(ptmp, 0, strlen(tags) + strlen(new_tags) + 2);
					strcat(ptmp, tags);
					strcat(ptmp, "\n");
					strcat(ptmp, new_tags);

					ctags_free(new_tags);
					ctags_free(tags);

					tags = ptmp;

				} else if(new_tags) {
					// first time
					tags = new_tags;
					new_tags = NULL;
				}
			}

			// prepare the reply
#ifdef __DEBUG
			std::vector<std::string> lines = string_tokenize(tags, "\n");
			for(size_t i=0; i<lines.size(); i++){
				printf("%s\n", lines.at(i).c_str());
			}
#endif

			clIndexerReply reply;
			if (tags) {
				// prepare reply
				reply.setCompletionCode(1);
				reply.setTags(tags);
			} else {
				reply.setCompletionCode(0);
			}

			ctags_free(tags);

			// send the reply
			if ( !clIndexerProtocol::SendReply(conn, reply) ) {
				fprintf(stderr, "ERROR: Protocol error: failed to send reply for file %s\n", reply.getFileName().c_str());
				break;
			}
		}
	}
	printf("INFO: WorkerThread: Going down\n");
	exit(-1);
}

// ---------------------------------------------
// is alive thread
// ---------------------------------------------

void IsAliveThread::start()
{
	while ( !testDestroy() ) {
#ifdef __WXMSW__
		Sleep(1000);
#else
		sleep(1);
#endif
		if ( !is_process_alive(m_pid) ) {
			fprintf(stderr, "INFO: parent process died, going down\n");
#ifndef __WXMSW__
			// Delete the local socket
			::unlink(m_socket.c_str());
			::remove(m_socket.c_str());
#endif
			exit(0);
		}
	}
	
#ifndef __WXMSW__
	// Delete the local socket
	::unlink(m_socket.c_str());
	::remove(m_socket.c_str());
#endif
}