-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.cpp
More file actions
171 lines (149 loc) · 3.38 KB
/
session.cpp
File metadata and controls
171 lines (149 loc) · 3.38 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "session.h"
using namespace giac;
MonitorThread::MonitorThread(context *ct) : contextptr(ct) { }
void MonitorThread::run()
{
while (true)
{
if (check_thread(contextptr) == 1)
QThread::msleep(50);
else break;
}
}
StopThread::StopThread(giac::context *ct) : contextptr(ct) { }
void StopThread::run(){
ctrl_c = true;
sleep(2);
if (check_thread(contextptr) == 1)
{
qInfo() << "Dirty try to interrupt thread!!!";
go=false;
emit(startDirtyInterrupt());
while (!go) msleep(20);
kill_thread(true, contextptr);
}
else qInfo() << "Clean interruption";
}
void StopThread::setContinueTrue()
{
go = true;
}
MessageBuffer::MessageBuffer(Session *s, int bsize) : std::streambuf()
{
session = s;
if (bsize)
{
char *ptr = new char[bsize];
setp(ptr, ptr + bsize);
}
else setp(0, 0);
setg(0, 0, 0);
}
int MessageBuffer::overflow(int c)
{
putBuffer();
if (c != EOF)
{
if (pbase() == epptr())
putChar(c);
else
sputc(c);
}
return 0;
}
int MessageBuffer::sync()
{
putBuffer();
return 0;
}
void MessageBuffer::putChar(int chr)
{
QChar c(chr);
session->appendPrintCache(c);
}
void MessageBuffer::putBuffer()
{
if (pbase() != pptr())
{
int len = pptr() - pbase();
char *buffer = new char[len + 1];
strncpy(buffer, pbase(), len);
buffer[len] = 0;
setp(pbase(), epptr());
delete [] buffer;
}
}
MessageStream::MessageStream(Session *s, int bsize) : std::ostream(new MessageBuffer(s, bsize)) { }
gen Session::answer(undef);
Session::Session(QObject *parent) : QObject(parent)
{
ct = new context;
monitor = new MonitorThread(ct);
stopThread = new StopThread(ct);
messageStream = new MessageStream(this);
signal(SIGINT, ctrl_c_signal_handler);
logptr(messageStream, ct);
}
Session::~Session()
{
delete ct;
delete monitor;
delete stopThread;
delete messageStream;
}
void Session::appendPrintCache(const QChar &c)
{
if (c == '"')
printCache.append(""");
else if (c == '&')
printCache.append("&");
else if (c == '<')
printCache.append("<");
else if (c == '>')
printCache.append(">");
else if (c == '\n')
{
messages.append(printCache);
printCache = "";
}
else printCache.append(c);
}
QStringList& Session::getGiacMessages()
{
if (!printCache.isEmpty())
messages.append(printCache);
return messages;
}
void Session::callback(const gen &g,void *newcontextptr)
{
Q_UNUSED(newcontextptr)
answer = g;
}
bool Session::evaluate(const gen &g)
{
if (stopThread->isRunning())
stopThread->wait(2000);
printCache = "";
if (make_thread(g,eval_level(ct), callback, (void*)ct, ct))
{
disconnect(monitor,SIGNAL(finished()),this,SLOT(resultReady()));
monitor->start();
connect(monitor,SIGNAL(finished()),this,SLOT(resultReady()));
processingStarted();
}
else return false;
history_in(ct).push_back(g);
return true;
}
void Session::resultReady()
{
history_out(ct).push_back(answer);
processingFinished(answer, getGiacMessages());
}
void Session::killThread()
{
if (!stopThread->isRunning()) {
emit(killingThread());
stopThread->start();
}
}