-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocols.dog
191 lines (178 loc) · 6.85 KB
/
Protocols.dog
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
struct GLOBAL{
const string: slipnode_default_port <- "2006"
}
struct SStreamSpec { // Slipstream Protocols
me string: spec
me string: protocolTag
me string: connectArgs
me string: contentArgs
me string: ipAddr
me string: portNum
me bool: setSpec(me string: fullSpec) <- {spec <- fullSpec; return(toParts())}
me bool: setProtocolTag(me string: spec) <- {protocolTag <- spec; return(toSpec())}
me bool: setConnectArgs(me string: spec) <- {connectArgs <- spec; return(toSpec())}
me bool: setContentArgs(me string: spec) <- {contentArgs <- spec; return(toSpec())}
// TODO: Validate and handle errors
me bool: toSpec() <- {
spec <- ""
if(protocolTag!=""){spec <+- protocolTag+"://"}
if(connectArgs!=""){spec <+- connectArgs}
if(contentArgs!=""){spec <+- "/"+contentArgs}
//TODO: Make sure spec does not have '..' in it.
return(true)
}
me bool: toParts() <- {
trimWS(spec)
me string: temp <- spec
me int: markPos <- temp.find("://")
if(markPos>=0){
protocolTag <- temp.subStr(0, markPos)
temp <- temp.subStr(markPos+3)
} else{protocolTag <- "file"}
markPos <- temp.find("/")
if(markPos>=0){connectArgs <- temp.subStr(0, markPos)}
contentArgs <- temp.subStr(markPos+1)
if(connectArgs!=""){
temp <- connectArgs
markPos <- connectArgs.find("]")
if(markPos>=0){
ipAddr <- connectArgs.subStr(1, markPos-1)
temp <- connectArgs.subStr(markPos+1)
markPos <- temp.find(":")
if(markPos>=0){portNum <- temp.subStr(markPos+1)}
else {portNum <- slipnode_default_port}
} else {
markPos <- connectArgs.find(":")
if(markPos>=0){
ipAddr <- connectArgs.subStr(0, markPos)
portNum <- connectArgs.subStr(markPos+1)
} else {
ipAddr <- connectArgs
portNum <- slipnode_default_port
}
}
if(ipAddr[0]=="["){ipAddr <- ipAddr.subStr(1, ipAddr.size()-2)}
}
return(true)
}
}
struct FetchFile: inherits=QProtocol {
me string: fileName
me FileStream: fileStream
their InfonConnector: source
void: init() <- {}
our QProtocol: createClone() <- {
our FetchFile:: retval
return(retval)
}
void: INIT() <- {
protocolIDPrefix <- "FetchFile" //"slip/fetchFile"
}
void: onNewStream_CB(their QuicStream: qStream) <- {}
me string: serverInit() <- {
print(" INIT FetchFile SERVER: ", protocolArgs,"\n")
fileName <- protocolArgs
fileStream.open(fileName)
if(fileStream.failure()){
print(" ERROR: opening file:'",fileName,"'\n")
return("ERR:"+taskID+":ERROR opening file:'"+fileName+"\n")
}
return("OK:"+taskID+"\n")
}
uint64: onServerRead(their QuicStream: qStream, their string: data, me bool: finished) <- {
me uint64: streamID <- qStream.getStreamID()
print("\n FetchFile SERVER READ STEP:",step," STREAM_ID:",streamID,"\n")
print(" DATA:",data,"\n")
me uint64: bytesUsed <- 0
return(bytesUsed)
}
bool: onServerWrite(their QuicStream: qStream) <- {
me uint64: streamID <- qStream.getStreamID()
print("\n FetchFile SERVER WRITE STEP:",step," fileName:", fileName," STREAM_ID:",streamID,"\n")
me uint64: bytesUsed <- fileStream.read(qStream.buff)
if(fileStream.failure()){print(" ERROR: reading buff, file:'",fileName,"'\n")}
qStream.write(qStream.buff.subStr(0, bytesUsed))
print(qStream.buff.subStr(0, bytesUsed))
if(fileStream.EOF()){
fileStream.close()
qStream.close()
step <+- 1
return(false)
}
return(true)
}
void: clientInit() <- {}
uint64: onClientRead(their QuicStream: qStream, their string: data, me bool: finished)<- {
me uint64: streamID <- qStream.getStreamID()
me uint64: bytesUsed
qStream.readBuffer <- data
bytesUsed <- data.size()
print("\n FetchFile CLIENT READ ",bytesUsed," CHARS FROM STREAM_ID:",streamID,"\n")
source.inputBuf.putStr(data)
if(finished){
print("\n FetchFile CLIENT CLOSE_FILE FROM STREAM_ID:",streamID,"\n")
source.inputBuf.close()
qStream.close()
}
return(bytesUsed)
}
bool: onClientWrite(their QuicStream: qStream) <- {return(false)}
}
/*
// Protocol to facilitate interactions via stdio
struct STDIO: inherits=QProtocol {
me string: fileName
me FileStream: fileStream
their InfonConnector: source
void: init() <- {}
our QProtocol: createClone() <- {
our FetchFile:: retval
return(retval)
}
void: INIT() <- {
protocolIDPrefix <- "slip/STDIO"
}
void: onNewStream_CB(their QuicStream: qStream) <- {}
me string: serverInit() <- {
print(" INIT FetchFile SERVER: ", protocolArgs,"\n")
fileName <- protocolArgs
return("OK:"+taskID+"\n")
}
uint64: onServerRead(their QuicStream: qStream, their string: data, me bool: finished) <- {
me uint64: streamID <- qStream.getStreamID()
print("\n FetchFile SERVER READ STEP:",step," STREAM_ID:",streamID,"\n")
print(" DATA:",data,"\n")
me uint64: bytesUsed <- 0
/// Write data to stdout
return(bytesUsed)
}
bool: onServerWrite(their QuicStream: qStream) <- {
me uint64: streamID <- qStream.getStreamID()
print("\n FetchFile SERVER WRITE STEP:",step," fileName:", fileName," STREAM_ID:",streamID,"\n")
me string: buff bytesUsed <- fileStream.read(qStream.buff)
}
qStream.write(qStream.buff.subStr(0, bytesUsed))
//print(qStream.buff.subStr(0, bytesUsed))
//if(EOF()){return(false)}
return(true)
}
void: clientInit() <- {}
uint64: onClientRead(their QuicStream: qStream, their string: data, me bool: finished)<- {
me uint64: streamID <- qStream.getStreamID()
me uint64: bytesUsed
print("\n FetchFile CLIENT READ STEP:",step," STREAM_ID:",streamID,"\n")
qStream.readBuffer <- data
bytesUsed <- data.size()
source.inputBuf.putStr(data)
if(finished and bytesUsed == data.size()){
source.inputBuf.close()
qStream.close()
}
return(bytesUsed)
}
bool: onClientWrite(their QuicStream: qStream) <- {
// get chars from source.iStreamParent and write them
return(true)
}
}
*/