[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)
目的:
1. 通过网页读取watchdog的信息
2. 通过网页设置watchdog
准备工作:
1. 选择一个web框架,选用 cherrypy
$ sudo apt-get install python-cherrypy3
2. 熟悉 RESTFUL , 参考 RESR_API(Mark McLoughlin) redhat REST API 指导
步骤:
我们选择了一个cherrypy作为web框架。 cherrypy的部署简单。
这只是个demo,没有实现MVC,大家自己练习。
此外也没有实现模板,距离一个正式的网站还差的很远。
界面实现如下:

代码如下:
代码中,喂狗代码采用了一个线程。需要改进。
发现不是很好,因为每个wsgi的请求都是一个线程。
最好是采用cherrypy提供的background, 或者将该逻辑独立为一个单例模式。
watchdog.py
import cherrypy
import json
import time
import threading index_html = """
<html>
<head>
<style type="text/css">
.watchdog-state {
display: inline-block;
height: 16px;
width: 16px;
border-radius: 8px;
margin-left: 10px;
margin-right: 10px;
}
.up {
background: linear-gradient(to bottom, #BFD255 0%%, #8EB92A 50%%,
#72AA00 51%%, #9ECB2D 100%%) repeat scroll 0 0 transparent;
} .down {
background: linear-gradient(to bottom, #AFAFAF 0%%, #AFAFAF 50%%,
#AFAFAF 51%%, #AFAFAF 100%%) repeat scroll 0 0 transparent;
}
</style>
</head> <body>
<script type="text/javascript">
function requestJSON(url, suc, done, err) {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("dataType", "application/json");
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var responseContext = xmlhttp.responseText;
var jsonobj=eval('('+responseContext+')');
suc(jsonobj);
} else{
// alert("readyState: " + xmlhttp.readyState + "status" + xmlhttp.status);
}
}
xmlhttp.send();
} function operationWatchdog()
{
// alert("operationWatchdog");
var url = "watchdog/deactivate";
var action = document.getElementById("watchdog-action");
if (action.innerHTML == "Activate"){
url = "watchdog/activate";
}
requestJSON(url, function(data){
// alert(data.value);
});
if (action.innerHTML == "Activate"){
action.innerHTML = "Deactivate";
} else {
action.innerHTML = "Activate";
}
var status = document.getElementById("watch-status");
if (status.className.match("up$")){
status.className = "watchdog-state down";
} else{
status.className = "watchdog-state up";
}
}
function FeedWatchod()
{
// alert("FeedWatchod");
var url = "watchdog/feedstop";
var feed = document.getElementById("feed-watchdog");
if (feed.innerHTML == "Start"){
url = "watchdog/feedstart";
}
requestJSON(url, function(data){
//alert(data.value);
});
if (feed.innerHTML == "Start"){
feed.innerHTML = "Stop";
} else {
feed.innerHTML = "Start";
}
}
</script>
<div>
Status of watchdog: <span id="watch-status" class="watchdog-state %s"> </span>
<button id="watchdog-action" type="button" onclick="operationWatchdog()">%s</button>
</div>
<div>
Feed watchdog:
<button id="feed-watchdog" type="button" onclick="FeedWatchod()">Start</button>
</div>
</body>
</html> """ def jsonObj(data):
cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8'
return json.dumps(data, indent=2, separators=(',', ':')) watchdog_hadle = open("/dev/cwd_demo", "w+")
def watchdogStatus():
vals = watchdog_hadle.readlines();
return vals[0] == ['B\x01\n'] def watchdogAction(action):
watchdog_hadle.write(action)
watchdog_hadle.flush() class WelcomePage(object):
def __init__(self):
self.watchdog = Watchdog() def index(self):
watchdog_state = "down"
watchdog_action = "Activate"
if watchdogStatus():
watchdog_state = "up"
watchdog_action = "Deactivate"
a = index_html % (watchdog_state, watchdog_action)
print a
return a
index.exposed = True def default(*args, **kwargs):
data = {"hello": "world"}
return jsonObj(data)
default.exposed = True class Watchdog(object):
exposed = True
feed_running = False
feed_thread = None def __init__(self):
pass @classmethod
def worker(cls):
while cls.feed_running:
print "worker"
watchdogAction('t')
time.sleep(1)
return @cherrypy.expose
def index(self, *args, **kwargs):
data = {"value": False}
data = {"value": watchdogStatus()}
return jsonObj(data) @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def activate(self, *args, **kwargs):
watchdogAction('a')
print "activate"
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def deactivate(self, *args, **kwargs):
watchdogAction('d')
print "deactivate"
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def feedstart(self, *args, **kwargs):
watchdogAction('t')
print "feed start"
if self.__class__.feed_thread == None:
self.__class__.feed_thread = threading.Thread(
target=self.__class__.worker)
if self.__class__.feed_running == False:
self.__class__.feed_running = True
self.__class__.feed_thread.start()
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.expose
def feedstop(self, *args, **kwargs):
print "feed stop"
if self.__class__.feed_thread.isAlive():
self.__class__.feed_running = False
self.__class__.feed_thread.join(1.5)
self.__class__.feed_thread = None
raise cherrypy.InternalRedirect("/watchdog") if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.quickstart(WelcomePage())
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(WelcomePage())
[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)的更多相关文章
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(九)
目的 1. 使用verilog/vhdl设计一个PCI的watchdog设备. 2. 通过systemverilog 写testbench. 很久之前研究过AC97的verilog代码.但是很久没用v ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(四)
通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了. 目的: 1. 了解我们的外设情况. 2. 为在guest中开发我们自己的linux PCI驱动程序做 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(一)
目的: 结合现在比较流行的技术,通过一个demo 展示一个全栈式设计的各种技能. 一个全栈式的工程师,应该能设计通过verilog/VHDL做logical设计.能写内核驱动,能架站. 要熟悉veri ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(六)
目的: 1. 为我们自己的watchdog写一个驱动 步骤: 通过之前的介绍,我们很容易猜想到写我们基于PCI的watchdog驱动,可以分2个步骤. 1. 探测加载PCI设备 这部分代码跟我们的设备 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(二)
这篇文章的理解,需要一些专业知识了. 我们可以创建模拟自己的外设吗? 我们已经知道什么是qemu了,我们可以通过qmeu的提供的外设,DIY一个计算机了. 但是我们可能还不满足,我们可以自己制造一个外 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(五)
目的: 1. 了解PCI的基本知识,为完成watchdog的设备做准备. 准备知识: 简单的说,PCI 设备分3个空间. 配置空间,IO空间,内存地址空间. PCI设备厂家决定了外设是使用IO空间还是 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(七)
目标: 1. 完成最终的设备驱动,增加具体的watchdog设备操作的代码. 测试代码: 代码最终实现见cwd_demo.c 代码只实现了read与write. 没有实现ioctl. 因此,我们可以 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(三)
我们已经设计了一个基于qemu的watchdog了.下一步工作就是创建一个含有我们的watchdog的虚拟计算机器了. 准备工作: 1. 使用virt-manager或者virsh创建一个虚拟机器. ...
- 从零开始的全栈工程师——利用CSS3画一个正方体 ( css3 )
transform属性 CSS3的变形(transform)属性让元素在一个坐标系统中变形.transform属性的基本语法如下: transform:none | <transform-fun ...
随机推荐
- Javascript 链式运动框架——逐行分析代码,让你轻松了解运动的原理
所谓链式运动,就是一环扣一环.我们的很多运动实际上来说指的就是分阶段的,第一个阶段动完,下个阶段开始动. 这个链式运动框架就是用来处理这些问题的. 我们先来看下之前的运动框架,以下是Javascrip ...
- Scrapy入门程序点评
1,引言 在<Scrapy的架构初探>一文,我基于爬虫开发的经验对Scrapy官网文章作了点评和解读,事件驱动的异步处理架构.极强的模块化等是个绝好的框架,接着我细读了官网的<Scr ...
- 00UILabel控件的详解
文本属性 1.text:label显示的文字 2.font:text的字体,值不可以为nil,否则异常 3.textColor:text的颜色 4.textAlignment;text的对其方式 5. ...
- eclipse android sdk javadoc
sdk 的函数不提示帮助信息 查了下是现在adt版本没有doc文件夹,拷贝了早期的版本docs过来 其他具体操作如下:http://blog.csdn.net/lyh7736362/article/d ...
- Example: Develop Web application on Baidu App Engine using CherryPy
In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...
- docker 在esx上的网络配置
- poj 1065 简单的贪心算法
题意大概是:有一组木头需要处理,每块木头有长度,重量两个属性,处理的前一块木头长len,重wei,当下一块木头的len1,与wei1满足:len1>len&&wei1>we ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- 【玩转Ubuntu】01. Ubuntu上配置JDK
一.安装JDK 提示:这里我们使用jdk1.6,因为android开发要求使用1.6.如果不信你可以打开android studio,它会提示你选择JDK6的路径 下载地址: http://www.o ...
- ContextMenuStrip 添加在窗体。点击右键不能显示问题解答
ContextMenuStrip控件,系统默认为点击菜单,使用时.需要与你的窗体绑定.如图所示,,form窗体上包括一个contextMenustrip信息.默认状态是无.所以在使用contextMe ...