Module: Minep::Request

Defined in:
lib/minep-request/version.rb,
lib/minep-request/requests.rb

Constant Summary collapse

VERSION =
"0.0.12"
DATE =
Time.now.to_s.split[0]

Class Method Summary collapse

Class Method Details

.authenticate(socket) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/minep-request/requests.rb', line 113

def self.authenticate socket
  args = {
    :name => "",
    :pass => ""
  }
  ret = Minep.makeMsg("AUTHENTICATE", args)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "AUTHENTICATE", socket
  return if response.nil?
  response["status"]
end

.backspace(socket, buflist) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/minep-request/requests.rb', line 182

def self.backspace socket, buflist
  args = {
    :buffer => "",
    :number => 0
  }
  ret = Minep.makeMsg("BACKSPACE", args, buflist)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "BACKSPACE", socket
  return if response.nil?
  response["status"]
end

.CommandsInfoObject



33
34
35
# File 'lib/minep-request/requests.rb', line 33

def self.CommandsInfo
  CommandsInfo
end

.delete(socket, buflist) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/minep-request/requests.rb', line 195

def self.delete socket, buflist
  args = {
    :buffer => "",
    :number => 0
  }
  ret = Minep.makeMsg("DELETE", args, buflist)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "DELETE", socket
  return if response.nil?
  response["status"]
end

.exec(socket, buflist) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/minep-request/requests.rb', line 141

def self.exec socket, buflist
  args = {
    :buffer => "",
    :command => "",
    :args => []
  }
  ret = Minep.makeMsg("EXEC", args, buflist)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "EXEC", socket
  return if response.nil?
  response["status"]
end

.insert(socket, buflist) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/minep-request/requests.rb', line 155

def self.insert socket, buflist
  args = {
    :buffer => "",
    :text => ""
  }
  ret = Minep.makeMsg("INSERT", args, buflist)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "INSERT", socket
  return if response.nil?
  response["status"]
end

.load(socket, type, buflist) ⇒ Object



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
# File 'lib/minep-request/requests.rb', line 74

def self.load socket, type, buflist
  args = {
    :path => "",
    :size => 0,
    :line => 0,
    :type => "up",
    :port => 0
  }
  printf "path : "
  args[:path] = Minep.read
  printf "line number : "
  args[:line] = Minep.read.to_i
  printf "port : "
  args[:port] = Minep.read.to_i
  args[:id] = SecureRandom.uuid
  unless File.exist? args[:path]
    return $stderr.puts "File #{args[:path]} doesn't exists"
  end
  f = File.new args[:path]
  args[:size] = f.size
  f.close
  Thread.start args[:port], args[:path], type do |port, path, type|
    sendFile port, path, type
    Thread.stop
  end
  sleep 2
  puts "LOAD=#{JSON.dump args}"
  socket.write "LOAD=#{JSON.dump args}"
  response = readAndParseResponse "LOAD", socket
  return if response.nil?
  unless response["info"]["uuid"]
    return $stderr.puts "Error while parsing response"
  end
  puts "Buffer id : #{response["info"]["uuid"]}"
  buflist[response["info"]["uuid"]] = response["info"]["uuid"]
  buflist[args[:path]] = response["info"]["uuid"]
  response["status"]
end

.move(socket, buflist) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/minep-request/requests.rb', line 168

def self.move socket, buflist
  args = {
    :buffer => "",
    :direction => "",
    :number => 0
  }
  ret = Minep.makeMsg("MOVE", args, buflist)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "MOVE", socket
  return if response.nil?
  response["status"]
end

.readAndParseResponse(request, socket) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/minep-request/requests.rb', line 54

def self.readAndParseResponse request, socket
  responsestr = socket.readpartial 4096
  responsetab = responsestr.split '=', 2
  return $stderr.puts "Error while parsing response"if responsetab[0] != "RESPONSE"
  response = {}
  begin
    response = JSON.parse responsetab[1]
  rescue JSON::ParserError => error
    return $stderr.puts "Error while parsing response"
  end
  if !response["status"] or !response["id"] or !response["info"]
    return $stderr.puts "Error while parsing response"
  end
  status = response["status"]
  if status != 0
    return $stderr.puts "Error : #{request.upcase} : #{Status[status]}"
  end
  response
end

.sendFile(port, path, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/minep-request/requests.rb', line 37

def self.sendFile port, path, type
  file = File.new path
  content = file.read
  file.close
  server = nil
  if type == :tcp
    server = TCPServer.new "0.0.0.0", port
  elsif type == :websocket
    server = WEBSocket::Server.new "0.0.0.0", port
  end
  socket = server.accept
  socket.write content
  socket.shutdown :RDWR
  socket.close
  server.close
end

.shell(socket) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/minep-request/requests.rb', line 208

def self.shell socket
  args = {
    :command => "",
    :args => []
  }
  ret = Minep.makeMsg("SHELL", args)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "SHELL", socket
  return if response.nil?
  if response["info"]["ls"]
    puts "ls : #{response["info"]["ls"]}"
    response["status"]
  end
end

.signup(socket) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/minep-request/requests.rb', line 126

def self. socket
  args = {
    :name => "",
    :pass => "",
    :email => "",
    :website => ""
  }
  ret = Minep.makeMsg("SIGNUP", args)
  return if ret.nil?
  socket.write ret
  response = readAndParseResponse "SIGNUP", socket
  return if response.nil?
  response["status"]
end