aboutsummaryrefslogtreecommitdiff
path: root/advtrains_signals_ks/init_degrotate_nodes.lua
blob: bd213cba7a521cd2484de8bd5ca576849d39b2c2 (plain)
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
-- Ks Signals for advtrains
-- will implement the advtrains signal API

local function place_degrotate(pos, placer, itemstack, pointed_thing)
	local yaw = placer:get_look_horizontal()
	local param = math.floor(yaw * 90 / math.pi + 0.5)
	local n = minetest.get_node(pos)
	n.param2 = param
	minetest.set_node(pos, n)
end

minetest.register_node("advtrains_signals_ks:mast", {
	drawtype = "mesh",
	paramtype="light",
	paramtype2="degrotate",
	walkable = false,
	selection_box = {
		type = "fixed",
		fixed = {-1/4, -1/2, -1/4, 1/4, 1/2, 1/4},
	},
	mesh = "advtrains_signals_ks_mast.obj",
	tiles = {"advtrains_signals_ks_mast.png"},
	description="Ks Signal Mast",
	sunlight_propagates=true,
	groups = {
		cracky=3,
		not_blocking_trains=1,
		--save_in_at_nodedb=2,
	},
	after_place_node = place_degrotate,
})

minetest.register_node("advtrains_signals_ks:head_main", {
	drawtype = "mesh",
	paramtype="light",
	paramtype2="degrotate",
	walkable = false,
	selection_box = {
		type = "fixed",
		fixed = {-1/4, -1/2, -1/4, 1/4, 1/2, 1/4},
	},
	mesh = "advtrains_signals_ks_head_main.obj",
	tiles = {"advtrains_signals_ks_mast.png", "advtrains_signals_ks_head.png"},
	description="Ks Main Signal Screen",
	sunlight_propagates=true,
	groups = {
		cracky=3,
		not_blocking_trains=1,
		--save_in_at_nodedb=2,
	},
	after_place_node = place_degrotate,
})

minetest.register_node("advtrains_signals_ks:zs_top", {
	drawtype = "mesh",
	paramtype="light",
	paramtype2="degrotate",
	walkable = false,
	selection_box = {
		type = "fixed",
		fixed = {-1/4, -1/2, -1/4, 1/4, 1/2, 1/4},
	},
	mesh = "advtrains_signals_ks_zs_top_smr0.obj",
	tiles = {"advtrains_signals_ks_mast.png", "advtrains_signals_ks_head.png"},
	description="Ks Speed Restriction Signal (top)",
	sunlight_propagates=true,
	groups = {
		cracky=3,
		not_blocking_trains=1,
		--save_in_at_nodedb=2,
	},
	after_place_node = place_degrotate,
})

minetest.register_node("advtrains_signals_ks:zs_bottom", {
	drawtype = "mesh",
	paramtype="light",
	paramtype2="degrotate",
	walkable = false,
	selection_box = {
		type = "fixed",
		fixed = {-1/4, -1/2, -1/4, 1/4, 1/2, 1/4},
	},
	mesh = "advtrains_signals_ks_zs_bottom_smr0.obj",
	tiles = {"advtrains_signals_ks_mast.png", "advtrains_signals_ks_head.png"},
	description="Ks Speed Restriction Signal (bottom)",
	sunlight_propagates=true,
	groups = {
		cracky=3,
		not_blocking_trains=1,
		--save_in_at_nodedb=2,
	},
	after_place_node = place_degrotate,
})
#n474'>474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "socket.h" // for select()
#include "porting.h" // for sleep_ms(), get_sysinfo()
#include "httpfetch.h"
#include <iostream>
#include <sstream>
#include <list>
#include <map>
#include <errno.h>
#include "jthread/jevent.h"
#include "config.h"
#include "exceptions.h"
#include "debug.h"
#include "log.h"
#include "util/container.h"
#include "util/thread.h"
#include "version.h"
#include "main.h"
#include "settings.h"

JMutex g_httpfetch_mutex;
std::map<unsigned long, std::list<HTTPFetchResult> > g_httpfetch_results;

HTTPFetchRequest::HTTPFetchRequest()
{
	url = "";
	caller = HTTPFETCH_DISCARD;
	request_id = 0;
	timeout = g_settings->getS32("curl_timeout");
	connect_timeout = timeout * 5;
	
	useragent = std::string("Minetest/") + minetest_version_hash + " (" + porting::get_sysinfo() + ")";
}


static void httpfetch_deliver_result(const HTTPFetchResult &fetchresult)
{
	unsigned long caller = fetchresult.caller;
	if (caller != HTTPFETCH_DISCARD) {
		JMutexAutoLock lock(g_httpfetch_mutex);
		g_httpfetch_results[caller].push_back(fetchresult);
	}
}

static void httpfetch_request_clear(unsigned long caller);

unsigned long httpfetch_caller_alloc()
{
	JMutexAutoLock lock(g_httpfetch_mutex);

	// Check each caller ID except HTTPFETCH_DISCARD
	const unsigned long discard = HTTPFETCH_DISCARD;
	for (unsigned long caller = discard + 1; caller != discard; ++caller) {
		std::map<unsigned long, std::list<HTTPFetchResult> >::iterator
			it = g_httpfetch_results.find(caller);
		if (it == g_httpfetch_results.end()) {
			verbosestream<<"httpfetch_caller_alloc: allocating "
					<<caller<<std::endl;
			// Access element to create it
			g_httpfetch_results[caller];
			return caller;
		}
	}

	assert("httpfetch_caller_alloc: ran out of caller IDs" == 0);
	return discard;
}

void httpfetch_caller_free(unsigned long caller)
{
	verbosestream<<"httpfetch_caller_free: freeing "
			<<caller<<std::endl;

	httpfetch_request_clear(caller);
	if (caller != HTTPFETCH_DISCARD) {
		JMutexAutoLock lock(g_httpfetch_mutex);
		g_httpfetch_results.erase(caller);
	}
}

bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetchresult)
{
	JMutexAutoLock lock(g_httpfetch_mutex);

	// Check that caller exists
	std::map<unsigned long, std::list<HTTPFetchResult> >::iterator
		it = g_httpfetch_results.find(caller);
	if (it == g_httpfetch_results.end())
		return false;

	// Check that result queue is nonempty
	std::list<HTTPFetchResult> &callerresults = it->second;
	if (callerresults.empty())
		return false;

	// Pop first result
	fetchresult = callerresults.front();
	callerresults.pop_front();
	return true;
}

#if USE_CURL
#include <curl/curl.h>

/*
	USE_CURL is on: use cURL based httpfetch implementation
*/

static size_t httpfetch_writefunction(
		char *ptr, size_t size, size_t nmemb, void *userdata)
{
	std::ostringstream *stream = (std::ostringstream*)userdata;
	size_t count = size * nmemb;
	stream->write(ptr, count);
	return count;
}

static size_t httpfetch_discardfunction(
		char *ptr, size_t size, size_t nmemb, void *userdata)
{
	return size * nmemb;
}

class CurlHandlePool
{
	std::list<CURL*> handles;

public:
	CurlHandlePool() {}
	~CurlHandlePool()
	{
		for (std::list<CURL*>::iterator it = handles.begin();
				it != handles.end(); ++it) {
			curl_easy_cleanup(*it);
		}
	}
	CURL * alloc()
	{
		CURL *curl;
		if (handles.empty()) {
			curl = curl_easy_init();
			if (curl == NULL) {
				errorstream<<"curl_easy_init returned NULL"<<std::endl;
			}
		}
		else {
			curl = handles.front();
			handles.pop_front();
		}
		return curl;
	}
	void free(CURL *handle)
	{
		if (handle)
			handles.push_back(handle);
	}
};

struct HTTPFetchOngoing
{
	CurlHandlePool *pool;
	CURL *curl;
	CURLM *multi;
	HTTPFetchRequest request;
	HTTPFetchResult result;
	std::ostringstream oss;
	char *post_fields;
	struct curl_slist *httpheader;

	HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *pool_):
		pool(pool_),
		curl(NULL),
		multi(NULL),
		request(request_),
		result(request_),
		oss(std::ios::binary),
		httpheader(NULL)
	{
		curl = pool->alloc();
		if (curl != NULL) {
			// Set static cURL options
			curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
			curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
			curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
			curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);

#if LIBCURL_VERSION_NUM >= 0x071304
			// Restrict protocols so that curl vulnerabilities in
			// other protocols don't affect us.
			// These settings were introduced in curl 7.19.4.
			long protocols =
				CURLPROTO_HTTP |
				CURLPROTO_HTTPS |
				CURLPROTO_FTP |
				CURLPROTO_FTPS;
			curl_easy_setopt(curl, CURLOPT_PROTOCOLS, protocols);
			curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, protocols);
#endif

			// Set cURL options based on HTTPFetchRequest
			curl_easy_setopt(curl, CURLOPT_URL,
					request.url.c_str());
			curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
					request.timeout);
			curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
					request.connect_timeout);

			if (request.useragent != "")
				curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());

			// Set up a write callback that writes to the
			// ostringstream ongoing->oss, unless the data
			// is to be discarded
			if (request.caller == HTTPFETCH_DISCARD) {
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
						httpfetch_discardfunction);
				curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
			}
			else {
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
						httpfetch_writefunction);
				curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
			}
			// Set POST (or GET) data
			if (request.post_fields.empty()) {
				curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
			}
			else {
				curl_easy_setopt(curl, CURLOPT_POST, 1);
				curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
						request.post_fields.size());
				curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
						request.post_fields.c_str());
				// request.post_fields must now *never* be
				// modified until CURLOPT_POSTFIELDS is cleared
			}
			// Set additional HTTP headers
			for (size_t i = 0; i < request.extra_headers.size(); ++i) {
				httpheader = curl_slist_append(
					httpheader,
					request.extra_headers[i].c_str());
			}
			curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpheader);
		}
	}