OSDN Git Service

Complete DNServer implementation. hasn't tested.
[dennco/dennco.git] / Source / DNEngine.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on 1/28/2012.
18 //
19
20
21 #include "TKLock.h"
22 #include "TKContainer.h"
23 #include "TKUICell.h"
24 #include "DNTimeKeeper.h"
25 #include "DNHTTPServer.h"
26 #include "DNEngine.h"
27
28 DNEngine::DNEngine(TKContainer *container) :
29 mContainer(container),mIsRunning(false),mPortNumber(9080),mHTTPServer(NULL)
30 {
31     mTimeKeeper = new DNTimeKeeper();
32
33 }
34
35
36 bool DNEngine::startHTTPServer(int portNumber)
37 {
38     if (mHTTPServer && mHTTPServer->isRunning())
39     {
40         mHTTPServer->stop();
41         delete mHTTPServer;
42         mHTTPServer = NULL;
43     }
44     mHTTPServer = new DNHTTPServer(this);
45     if (mHTTPServer)
46     {
47         mHTTPServer->setPortNumber(portNumber);
48         mHTTPServer->start();
49     }
50     return true;
51 }
52
53 void DNEngine::stopServer()
54 {
55     if (mHTTPServer && mHTTPServer->isRunning())
56     {
57         mHTTPServer->stop();
58         delete mHTTPServer;
59         mHTTPServer = NULL;
60     }    
61 }
62
63 void DNEngine::setTickIntervalSec(float interval)
64 {
65     if (mTimeKeeper)
66         mTimeKeeper->setIntevalSec(interval);
67 }
68
69 void DNEngine::doTickThread()
70 {
71     mTickThreadLock.lock();
72     if (mIsRunning)
73     {
74         //doTickThread is already running.
75         mTickThreadLock.unlock();
76         return;
77     }
78     mIsRunning = true;
79     mTickThreadLock.unlock();
80     
81     
82     DNTimeKeeper *timeKeeper = new DNTimeKeeper();
83     
84     while(mIsRunning)
85     {
86         mContainer->doTick(timeKeeper->getTickTime());
87         timeKeeper->sleepUntilNextInterval();
88     }
89     
90     delete timeKeeper;
91 }
92
93 float DNEngine::doClientGetRequest(const char* path)
94 {
95     TKUICell *cell = (TKUICell*)mContainer->getInterfaceCell(path);
96     float result = 0;
97     if (cell)
98     {
99         result = cell->getValue();
100     }
101     return result;
102 }
103
104 bool DNEngine::doClientSetRequest(const char* path, const char* value)
105 {
106     TKUICell *cell = (TKUICell*)mContainer->getInterfaceCell(path);
107     bool result = false;
108     if (cell)
109     {
110         cell->setValue(atof(value));
111         result = true;
112     }
113     return result;
114 }
115