Re: Does anyone understand how to use "shared" types with concurrency send/receive functions?

2017-08-12 Thread crimaniak via Digitalmars-d-learn

On Saturday, 12 August 2017 at 18:57:44 UTC, Arek wrote:

I have the folowing problem:
I like to envelope the class object in struct to control the 
destruction moment and then send this object to another 
thread/fiber (or task, cause I use vibe-d).


I can't find any method to make it working. Any ideas?


I tried it too some time ago. Then I read Alexandrescu book and 
realized that the authors of the language do not want anyone to 
do this. Long story short, just plan your application so that 
each complex object is monitored by only one thread/task, and 
pass not objects, but messages (immutable structs) about what to 
do with them.




Re: html fetcher/parser

2017-08-12 Thread Soulsbane via Digitalmars-d-learn

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:
I would like to get into D again by making a small program 
which fetches a website every X-time and keeps track of all 
changes within specified dom elements.


fetching: should I go for std curl, vibe.d or something else?
parsing: I could only find these dub packages: htmld & 
libdominator.

And they don't seem overly active, any recommendations?

As I haven't been using D for some time I just don't want to 
get off with a bad start :)

thx


I've the requests module nice to work with: 
http://code.dlang.org/packages/requests


Re: html fetcher/parser

2017-08-12 Thread Michael via Digitalmars-d-learn

On Saturday, 12 August 2017 at 20:22:44 UTC, Adam D. Ruppe wrote:

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:

[...]


My dom.d and http2.d combine to make this easy:

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://github.com/adamdruppe/arsd/blob/master/http2.d

[...]


Sometimes it feels like there's the standard D library, Phobos, 
and then for everything else you have already developed a 
suitable library to supplement it haha!


Re: html fetcher/parser

2017-08-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:
I would like to get into D again by making a small program 
which fetches a website every X-time and keeps track of all 
changes within specified dom elements.


My dom.d and http2.d combine to make this easy:

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://github.com/adamdruppe/arsd/blob/master/http2.d

and support file for random encodings:

https://github.com/adamdruppe/arsd/blob/master/characterencodings.d


Or via dub:

http://code.dlang.org/packages/arsd-official

the dom and http subpackages are the ones you want.


Docs: http://dpldocs.info/arsd.dom


Sample program:

---
// compile: $ dmd thisfile.d ~/arsd/{dom,http2,characterencodings}

import std.stdio;
import arsd.dom;

void main() {
auto document = Document.fromUrl("https://dlang.org/;);
writeln(document.optionSelector("p").innerText);
}
---

Output:

D is a general-purpose programming language with
static typing, systems-level access, and C-like syntax.
It combines efficiency, control and modeling power with 
safety

and programmer productivity.




Note that the https support requires OpenSSL available on your 
system. Works best on Linux with it installed as a devel lib (so 
like openssl-devel or whatever, just like you would if using it 
from C).




How it works:


Document.fromUrl uses the http lib to fetch it, then 
automatically parse the contents as a dom document. It will 
correct for common errors in webpage markup, character sets, etc.


Document and Element both have various methods for navigating, 
modifying, and accessing the DOM tree. Here, I used 
`optionSelector`, which works like `querySelector` in Javascript 
(and the same syntax is used for CSS), returning the first 
matching element.


querySelector, however, returns null if there is nothing found. 
optionSelector returns a dummy object instead, so you don't have 
to explicitly test it for null and instead just access its 
methods.


`innerText` returns the text inside, stripped of markup. You 
might also want `innerHTML`, or `toString` to get the whole 
thing, markup and all.




there's a lot more you can do too but just these few functions I 
think will be enough for your task.



Bonus fact: 
http://dpldocs.info/experimental-docs/std.algorithm.comparison.levenshteinDistanceAndPath.1.html that function from the standard library makes doing a diff display of before and after pretty simple


html fetcher/parser

2017-08-12 Thread Faux Amis via Digitalmars-d-learn
I would like to get into D again by making a small program which fetches 
a website every X-time and keeps track of all changes within specified 
dom elements.


fetching: should I go for std curl, vibe.d or something else?
parsing: I could only find these dub packages: htmld & libdominator.
And they don't seem overly active, any recommendations?

As I haven't been using D for some time I just don't want to get off 
with a bad start :)

thx



Does anyone understand how to use "shared" types with concurrency send/receive functions?

2017-08-12 Thread Arek via Digitalmars-d-learn

I have the folowing problem:
I like to envelope the class object in struct to control the 
destruction moment and then send this object to another 
thread/fiber (or task, cause I use vibe-d).


I can't find any method to make it working. Any ideas?

dmd (version 075) gives so stupid results, I belive it's broken 
(I've created the issue 
https://issues.dlang.org/show_bug.cgi?id=17749 )


ldc2 returns some errors:

.../import/std/variant.d(610,27): Error: function 
core.stdc.string.memcpy (void* s1, const(void*) s2, ulong n) is 
not callable using argument types (ubyte[32]*, 
shared(Env!(shared(A)))*, ulong)
.../import/std/conv.d(4186,9): Error: static assert  "Cannot 
emplace a Env!(shared(A)) because Env!(shared(A)).this(this) is 
annotated with @disable."
.../import/std/conv.d(4198,24):instantiated from here: 
emplaceRef!(Env!(shared(A)), Env!(shared(A)), Env!(shared(A)))
.../import/std/variant.d(301,35):instantiated from here: 
emplaceRef!(Env!(shared(A)), Env!(shared(A)))
.../import/std/variant.d(630,21):instantiated from here: 
handler!(shared(Env!(shared(A
.../import/std/variant.d(544,17):... (5 instantiations, 
-v to show) ...

../../../.dub/packages/vibe-core-1.1.1/vibe-core/source/vibe/core/concurrency.d(1223,64):
instantiated from here: send!(shared(Env!(shared(A
app.d(74,7):instantiated from here: 
send!(shared(Env!(shared(A




The code may be complied like this: dub --single --compiler=ldc2 
./app.d   (assuming it's saved in app.d file).



/+
dub.sdl:
name "simple"
dependency "vibe-core" version="~>1.1.0"
+/
import std.stdio;
import std.format;

import vibe.core.core;
import vibe.core.task;
import vibe.core.concurrency;

// simple class with destructor
shared class A
{
int i;
this(int i)
{
this.i = i;
}

~this()
{
writeln("destruct ", i);
}

}

shared struct Env(T) if (is(T == class) && is(T == shared))
{
T obj;

this(T o)
{
obj = obj;
}

this(this)
{
// some magic here
}

auto opAssign(shared(Env!T) other)
{
obj = other.obj;
return this;
}

auto opAssign(ref shared(Env!T) other)
{
obj = other.obj;
return this;
}

~this()
{
if (obj !is null)
destroy(obj);
obj = null;
}
}

void main()
{
auto consumer = runTask({
auto got = receiveOnly!(shared Env!(shared A))();
writeln(typeof(got).stringof);
});

auto producer = runTask({
auto a = new shared A(1);
shared b = shared Env!(shared A)(a);
writeln(typeof(b).stringof);
send(consumer, b);
});
runApplication();
}



Re: How to make http requests to unix socket?

2017-08-12 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Friday, 11 August 2017 at 18:39:54 UTC, dvnguyen wrote:
How to make http requests to unix socket? For example, in 
Docker engine api,


curl --unix-socket /var/run/docker.sock 
http:/v1.24/containers/json


I made a pull request a year ago or so to have vibe-d's 
requestHttp to support unix sockets. Call it like this 
`requestHTTP("https+unix://%2Fvar%2Frun%2Fdocker.sock/containers/create`.
You will need to set some vibe-d tls things to accept the 
connection.


Sönke just upgraded vibe-core/event-core to support it as well if 
I remember it correctly (so you can also use vibe 8.x with 
vibe-core).


As for endpoints which return streams (like 
containers/"~id~"/logs?stdout=1=1=1), Docker 
multiplexes stdout/stderr over http with chunked 
transfer-encoding.
The protocol is a 8 byte Header + Frame. The last four bytes in 
the Header is an uint32 representing the frame size. The first 
byte is either 1 -> stdout or 2 -> stderr, which designates the 
type of the stream in the following frame.


Re: __dtor vs __xdtor

2017-08-12 Thread Marco Leise via Digitalmars-d-learn
Am Fri, 11 Aug 2017 17:10:14 +
schrieb bitwise :

> Ok thanks.
> 
> I don't understand why you would ever want to call __dtor 
> then...is it possible to have only __dtor without also having 
> __xdtor? Like, if I want to call a struct's destructor, do I have 
> to check for both, or can I just always check for, and call 
> __xdtor?

I think it was simply that all the special methods needed a
symbol name, so this() was called __ctor and ~this() was
called __dtor. It was never supposed to cover field
destruction, mixed in destructors or inheritance in classes.
User code was not expected to call these directly anyways.
Not very long ago __xdtor and __xpostblit were introduced
that wrap up the entire finalization and copy operation.
__dtor will remain as the 1:1 representation of the ~this()
method.

-- 
Marco



ddoc and method overriding.

2017-08-12 Thread Alexandru Ermicioi via Digitalmars-d-learn

Hi all,

Having a trivial example such as:

```D
class Foo {
/**
 * A documentation info
**/
void fancy();
}

class Moo {
override void fancy();
}
```

Is there a way to tell ddoc to use Foo.fancy documentation block 
for overriding Moo.fancy method?


Thx.