(被迫参加的hackathon) 在线编译系统开发小记

[ ]

The content is recoverd from Wordpress Blog, for more details please check HERE

November 11, 2015

VOID001

吐槽

因为某种原因我没有参加MSC举办的Hackathon, 不过我的部员们参加了 0.0 然后我本来想去蹭点Pizza之类的东西吃,结果(我感觉他们是给我诱拐过去了) 到了比赛现场之后告诉我, 这里封馆了 Orz 于是乎我就被迫开始写代码(连电脑都没有带只为了去蹭口pizza吃的我TAT[结果最后还没吃到pizza])

实现的是一个简单的在线编译系统 TOC (Toy Online Compiler) 支持多种语言, 下面简单说一下实现的思路和核心的代码

实现思路

简单分析一下这个系统,由两部分组成 ,代码提交模块和代码编译运行模块 ,前者负责将代码交给后者,后者编译运行完毕将结果返回给前者,然后前者将结果展示给用户.

具体实现方式是: 代码提交模块负责将代码文件保存到服务器, 然后通过socket将消息通知到代码编译模块,然后代码编译模块执行完毕的结果再通过通信的方式传递回来, 很显然实现代码提交模块实现难度很容易,主要的问题在后者的编译模块的通信和编译问题, 下面给出编译模块的核心代码

<?php
error\_reporting(E\_ALL);
require\_once('function.php');
require\_once('queue.php');
require\_once('actionclass.php');

$socketQueue = new actionQueue();
$socketQueue->initQueue();
$mainSocket = SocketOpen();
socket\_set\_option($mainSocket, SOL\_SOCKET, SO\_REUSEADDR, 1);

while(TRUE)
{
    if($socketQueue->len > 5)//Maximum count
        continue;               //Do not accept create new socket
    $resSocket = socket\_accept($mainSocket);
    if($resSocket != FALSE)
    {
        $socketQueue->push($resSocket);
    }
    if($socketQueue->isempty() The content is recoverd from Wordpress Blog, for more details please check [HERE](recover-my-blog) FALSE)
    {
        $currentSocket = $socketQueue->pop();
        $rawMsg = SockRead($currentSocket);
        echo "Raw Msg is $rawMsgn";
        if(Auth($rawMsg) The content is recoverd from Wordpress Blog, for more details please check [HERE](recover-my-blog) true)
        {
            $actionObj = ParseMsg($rawMsg);
            $simpleResultObj = $actionObj->Compile();
            if($simpleResultObj->resultno != 0)
            {
                echo "Compile Errorn";
                SockWrite($currentSocket, "ERR");
                SockWrite($currentSocket, $simpleResultObj->resultStr);
            }
            else
            {
                echo "Compile OKn";
                SockWrite($currentSocket, "OK");
                $simpleResultObj = $actionObj->Run();

                //Send Msg back to client
                if($simpleResultObj->resultno The content is recoverd from Wordpress Blog, for more details please check [HERE](recover-my-blog) 0)
                {
                    SockWrite($currentSocket, "OK");
                    SockWrite($currentSocket, $simpleResultObj->resultStr);
                }
                else
                {
                    SockWrite($currentSocket, "ERR");
                    SockWrite($currentSocket, $simpleResultObj->resultStr);
                }
            }
        }
        else
        {
            $getaddr = "";
            $getport = "";
            socket\_getsockname($currentSocket, $getaddr, $getport);
            echo "Invalid request from $getaddr:$getport";
            SockWrite($currentSocket, "FATAL");
        }
    }
}

这段代码就是编译模块的核心逻辑, 我使用了一个队列来维护所有在队列内的通信, 队列最多有5个元素,   代码运行起来会持续检查队列是否为空,如果队列不为空,就会取出队列头的socket,处理socket传来的消息,编译相应的代码,然后产生结果通过封装好的socket_write SockWrite,将运行结果回传给代码提交模块,重复上述操作.

为了保证代码的可读性, 封装了一个编译类 actionClass 来进行编译和运行, 编译和运行的方式是通过exec调用系统命令,下面会具体说明

 

实现细节

If you want to reuse address and port, and get rid of error: unable to bind, address already in use, you have to use socket_setopt (check actual spelling for this function in you PHP verison) before calling bind:

设置上   SO_REUSEADDR

 

 

 

目前实现阶段

待解决问题

 


PHP, Web Develop C. Linux, kernel, Laravel, PHP, Python, Shell, Web, wine


Historical Comments

Post navigation ————— NEXT
setcap wine 遇到的问题解决
PREVIOUS [Laravel] 配置phpstorm支持laravel语法补全 Comments are closed.

统计信息nanodesu~~~

当前在线人数 (。・ω・)ノ゙ 0
修罗酱真诚欢迎更多的朋友来我的WOWO做客哦~ Search for: Search #### Recent Posts

Back