<commit_before>
----------------------------------------------------------------------------
--  xmlrpc_post.lua - Concurrent execution of XML-RPC calls
--
--  Created: Wed Jul 13 14:43:00 2011 (at SRI International, Menlo Park, CA)
--  License: BSD, cf. LICENSE file of roslua
--  Copyright  2011  <NAME> [www.niemueller.de]
--             2011  SRI International
--             2011  Carnegie Mellon University
----------------------------------------------------------------------------

--- Concurrent XML-RPC client.
-- This module contains the XmlRpcPost class which can execute XML-RPC calls
-- concurrently. A method call is split up into multiple stages. A request
-- is started and on spinning required actions are taken (sending call,
-- waiting, reading and parsing the reply) with a minimum time overhead.
-- <br /><br />
-- The user should not have to directly interact with this class. It is used
-- by the SlaveProxy class to provide a way to concurrently call
-- requestTopic, an operation that can harm the overall performance (and
-- actually lead to deadlocks) when called blocking.
-- @copyright <NAME>, Carnegie Mellon University, Intel Labs Pittsburgh,
-- SRI International
-- @release Released under BSD license
module("roslua.xmlrpc_post", package.seeall)

require("socket")
require("socket.url")
require("xmlrpc")
assert(xmlrpc._VERSION_MAJOR and (xmlrpc._VERSION_MAJOR > 1 or xmlrpc._VERSION_MAJOR == 1 and xmlrpc._VERSION_MINOR >= 2),
       "You must use version 1.2 or newer of lua-xmlrpc")

require("roslua.utils")
local socket_send = roslua.utils.socket_send
local socket_recv = roslua.utils.socket_recv
local socket_recvline = roslua.utils.socket_recvline

local STATE_IDLE      = 1
local STATE_INITIATED = 2
local STATE_CONNECTED = 3
local STATE_POSTED    = 4
local STATE_RECEIVED  = 5
local STATE_FAILED    = 6

local num_to_state = { "IDLE", "INITIATED", "CONNECTED",
		       "POSTED", "RECEIVED", "FAILED"}

XmlRpcPost = {}

--- Constructor.
-- Setup a new concurrent XML-RPC call.
-- @param uri URI to use for method calls
-- @param method method name to pass. This parameter is optional, if passed
-- the call is started in the contructor.
-- @param ... any parameter required for the method call (only used if
-- method is passed)
function XmlRpcPost:new(uri, method, ...)
   assert(uri, "XmlRpcPost: uri must not be nil")

   local o = { uri=uri }
   setmetatable(o, self)
   self.__index = self

   o.state = STATE_IDLE

   if method then
      o:start_call(method, ...)
      self.args = { ... }
   end

   return o
end


--- Setup the request.
-- @param method name of method to call
-- @param ... any parameter required for the method call
function XmlRpcPost:setup_request(method, ...)
   local request_body = xmlrpc.clEncode(method, ...)
   self.args = { ... }

   local u = socket.url.parse(self.uri, {path="/"})

   self.request = {
      url = u,
      uri=socket.url.build({path=u.path}),
      method = method,
      http_method = "POST",
      request_body = request_body,
      headers = {
	 ["user-agent"] = xmlrpc._PKGNAME .. " " .. xmlrpc._VERSION,
	 ["host"] = u.host,
	 ["connection"] = "close",
	 ["content-type"] = "text/xml",
	 ["content-length"] = tostring(string.len(request_body)),
      },
   }

   for k,v in pairs(u) do
      --printf("U: %s: %s", k, tostring(v))
      self.request[k] = v
   end

   --for k,v in pairs(self.request) do
   --  printf("R: %s: %s", k, tostring(v))
   --end
end

--- Start a method call.
-- @param method name of method to call
-- @param ... any parameter required for the method call
function XmlRpcPost:start_call(method, ...)
   assert(self.state == STATE_IDLE, "A request is already running")

   self:setup_request(method, ...)

   self.c = socket.tcp()
   self.c:settimeout(0)
   local ok, err = self.c:connect(self.request.host, self.request.port)
   if not ok then
      if err == "timeout" then
	 -- it might still complete at some point
	 self.state = STATE_INITIATED
      else
	 self.state = STATE_FAILED<commit_msg>XmlRpcPost: catch socket init fail, reset conn on fail<commit_after>
----------------------------------------------------------------------------
--  xmlrpc_post.lua - Concurrent execution of XML-RPC calls
--
--  Created: Wed Jul 13 14:43:00 2011 (at SRI International, Menlo Park, CA)
--  License: BSD, cf. LICENSE file of roslua
--  Copyright  2011  <NAME> [www.niemueller.de]
--             2011  SRI International
--             2011  Carnegie Mellon University
----------------------------------------------------------------------------

--- Concurrent XML-RPC client.
-- This module contains the XmlRpcPost class which can execute XML-RPC calls
-- concurrently. A method call is split up into multiple stages. A request
-- is started and on spinning required actions are taken (sending call,
-- waiting, reading and parsing the reply) with a minimum time overhead.
-- <br /><br />
-- The user should not have to directly interact with this class. It is used
-- by the SlaveProxy class to provide a way to concurrently call
-- requestTopic, an operation that can harm the overall performance (and
-- actually lead to deadlocks) when called blocking.
-- @copyright <NAME>, Carnegie Mellon University, Intel Labs Pittsburgh,
-- SRI International
-- @release Released under BSD license
module("roslua.xmlrpc_post", package.seeall)

require("socket")
require("socket.url")
require("xmlrpc")
assert(xmlrpc._VERSION_MAJOR and (xmlrpc._VERSION_MAJOR > 1 or xmlrpc._VERSION_MAJOR == 1 and xmlrpc._VERSION_MINOR >= 2),
       "You must use version 1.2 or newer of lua-xmlrpc")

require("roslua.utils")
local socket_send = roslua.utils.socket_send
local socket_recv = roslua.utils.socket_recv
local socket_recvline = roslua.utils.socket_recvline

local STATE_IDLE      = 1
local STATE_INITIATED = 2
local STATE_CONNECTED = 3
local STATE_POSTED    = 4
local STATE_RECEIVED  = 5
local STATE_FAILED    = 6

local num_to_state = { "IDLE", "INITIATED", "CONNECTED",
		       "POSTED", "RECEIVED", "FAILED"}

XmlRpcPost = {}

--- Constructor.
-- Setup a new concurrent XML-RPC call.
-- @param uri URI to use for method calls
-- @param method method name to pass. This parameter is optional, if passed
-- the call is started in the contructor.
-- @param ... any parameter required for the method call (only used if
-- method is passed)
function XmlRpcPost:new(uri, method, ...)
   assert(uri, "XmlRpcPost: uri must not be nil")

   local o = { uri=uri }
   setmetatable(o, self)
   self.__index = self

   o.state = STATE_IDLE

   if method then
      o:start_call(method, ...)
      self.args = { ... }
   end

   return o
end


--- Setup the request.
-- @param method name of method to call
-- @param ... any parameter required for the method call
function XmlRpcPost:setup_request(method, ...)
   local request_body = xmlrpc.clEncode(method, ...)
   self.args = { ... }

   local u = socket.url.parse(self.uri, {path="/"})

   self.request = {
      url = u,
      uri=socket.url.build({path=u.path}),
      method = method,
      http_method = "POST",
      request_body = request_body,
      headers = {
	 ["user-agent"] = xmlrpc._PKGNAME .. " " .. xmlrpc._VERSION,
	 ["host"] = u.host,
	 ["connection"] = "close",
	 ["content-type"] = "text/xml",
	 ["content-length"] = tostring(string.len(request_body)),
      },
   }

   for k,v in pairs(u) do
      --printf("U: %s: %s", k, tostring(v))
      self.request[k] = v
   end

   --for k,v in pairs(self.request) do
   --  printf("R: %s: %s", k, tostring(v))
   --end
end

--- Start a method call.
-- @param method name of method to call
-- @param ... any parameter required for the method call
function XmlRpcPost:start_call(method, ...)
   assert(self.state == STATE_IDLE, "A request is already running")

   self:setup_request(method, ...)

   self.c = socket.tcp()
   assert(self.c,
          string.format("XmlRpcPost: Failed to create TCP socket "..
                        "(host: %s, port %d, method: %s)",
                     self.request.host, self.request.port, method))
   self.c:settimeout(0)
   local ok, err = self.c:connect(self.request.host, self.request.port)
   if not ok then
      if err == "timeout" then
	 -- it might still complete at some point
	 self.state = STATE_INITIATED
      else
         self.c = nil
	 self.state = STATE_FAILED
{'fraction_non_alphanumeric': 0.1548061922476899, 'fraction_numerical': 0.008640345613824553, 'mean_word_length': 2.5968925334484245, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7527595', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2475, 'n_words': 851}