~ubuntu-branches/ubuntu/wily/julia/wily

« back to all changes in this revision

Viewing changes to extras/julia_web_base.jl

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-02-06 17:54:29 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130206175429-13br5kqpkfjqdmre
Tags: 0.0.0+20130206.git32ff5759-1
* New upstream snapshot.
* debian/copyright: reflect upstream changes
* debian/rules: update get-orig-source to reflect upstream changes
   + Don't ship nginx
   + Adapt for new configure-random target in deps/Makefile
* Enable build of Tk wrapper.
   + debian/control: add build dependency on tk-dev
   + debian/rules: add tk rule to build-arch
* debian/julia.install: install VERSION and COMMIT files
* no-webrepl.patch: new patch
* Refresh other patches
* Add source override for config.status file under deps/random/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Julia Webrepl ##
 
2
const DEBUG = false
 
3
import Base.show
 
4
 
1
5
###########################################
2
6
# protocol
3
7
###########################################
16
20
# [message_type::number, arg0::string, arg1::string, ...]
17
21
 
18
22
# import the message types
19
 
include(find_in_path("webrepl_msgtypes_h"))
 
23
include("webrepl_msgtypes_h.jl")
20
24
 
21
25
###########################################
22
26
# set up the socket connection
23
27
###########################################
24
28
 
25
29
# open a socket on any port
26
 
__ports = [int16(4444)]
27
 
__sockfd = ccall(:open_any_tcp_port, Int32, (Ptr{Int16},), __ports)
28
 
if __sockfd == -1
29
 
    # couldn't open the socket
30
 
    println("could not open server socket on port 4444.")
31
 
    exit()
32
 
end
 
30
(port,sock) = Base.open_any_tcp_port(4444)
33
31
 
34
32
# print the socket number so the server knows what it is
35
 
println(__ports[1])
 
33
println(STDOUT,int16(port))
36
34
 
37
35
# wait for the server to connect to the socket
38
 
__connectfd = ccall(:accept, Int32, (Int32, Ptr{Void}, Ptr{Void}), __sockfd, C_NULL, C_NULL)
39
 
 
40
 
# create an io object from the file descriptor
41
 
__io = fdio(__connectfd)
 
36
__io = Base.wait_accept(sock)
 
37
Base.start_reading(__io)
42
38
 
43
39
###########################################
44
40
# protocol implementation
49
45
    msg_type::Uint8
50
46
    args::Array{Any, 1}
51
47
end
 
48
show(m::__Message) = __print_message(m)
52
49
 
53
50
# read a message
54
51
function __read_message()
58
55
    for i=1:num_args
59
56
        arg_length = read(__io, Uint32)
60
57
        arg = ASCIIString(read(__io, Uint8, arg_length))
61
 
        push(args, arg)
 
58
        push!(args, arg)
62
59
    end
63
60
    return __Message(msg_type, args)
64
61
end
65
62
 
66
63
# send a message
 
64
const __io_out_buf = PipeString()
67
65
function __write_message(msg)
 
66
    if DEBUG show(msg); println() end
68
67
    write(__io, uint8(msg.msg_type))
69
68
    write(__io, uint8(length(msg.args)))
70
69
    for arg=msg.args
71
 
        write(__io, uint32(length(arg)))
72
 
        write(__io, arg)
 
70
        write(__io_out_buf, arg)
 
71
        data = takebuf_array(__io_out_buf)
 
72
        write(__io, uint32(length(data)))
 
73
        write(__io, data)
73
74
    end
74
 
    flush(__io)
 
75
    #flush(__io)
75
76
end
76
77
 
77
78
# print a message (useful for debugging)
91
92
###########################################
92
93
 
93
94
# load the special functions available to the web repl
94
 
include(find_in_path("julia_web"))
 
95
include("julia_web.jl")
95
96
 
96
97
###########################################
97
98
# input event handler
104
105
function __socket_callback(fd)
105
106
    # read the message
106
107
    __msg = __read_message()
 
108
    if DEBUG show(__msg); println() end
107
109
    
108
110
    # MSG_INPUT_EVAL
109
111
    if __msg.msg_type == __MSG_INPUT_EVAL && length(__msg.args) == 3
122
124
 
123
125
        for i=1:length(__lines)
124
126
            # add the next line of input
125
 
            __input_so_far = strcat(__input_so_far, __lines[i], "\n")
 
127
            __input_so_far = string(__input_so_far, __lines[i], "\n")
126
128
 
127
129
            # try to parse it
128
130
            __expr = parse_input_line(__input_so_far)
171
173
end
172
174
 
173
175
# event handler for socket input
174
 
add_fd_handler(__connectfd, __socket_callback)
 
176
enq_work(@task while true __socket_callback(__io) end)
175
177
 
176
178
web_show(user_id, ans) =
177
179
    __Message(__MSG_OUTPUT_EVAL_RESULT, {user_id, sprint(repl_show, ans)})