Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 3-Dec-2014 22:07:56

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

multithreading with two vm's?

colleagues:

in working on my medioctopus player project, as i think i've posted to the forum, i've encountered greyed out scol windows:

1. during loading of .pkg dependencies,
2. during 3d scene initialization, and
3, when loading new os3d scenes.

would there be some way that i could have my app startup 2 vm's. the first would do the heavy work, and the second would give the user eye  candy and an estimation of the time it will take to accomplish 3d initialization (that could be built up on a particular machine by tracking 3d initialization startup and saving the average to an .xml file, as for giving the user an estimate of loading a 3d scene, i'm open to input, i don't know how i would go about doing that or if it is even possible (perhaps by counting up the size of files and complexity of materials? i'm a bit lost when it comes to getting an estimate of scene loading time, the time between opening a local .xos file and the scene appearing, or the time between completing a .pak downloading and the scene appearing).

when the 3d machine is done its work, it will alert the vm making the eyecandy, which will then shut down or somehow become invisible.  so machine 1 will be the machine presenting os3d content, and machine 2 will keep the user apprised of what is happening.

this would be like managing threads in vb.net, but just using scol.

thx!
-h

Offline

#2 4-Dec-2014 09:38:20

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

Yes.

I've tried a such method when you've posted this problem.
This is enough heavy.

A VM with your player
Another VM with a 'waiting' window. When the first is done, the second is destroyed.

For that, basically :
- on the 'waiting' VM, build and launch a Scol server (_setserver), save the used port number.
- launch the 'player' VM from this VM : _newmachine and add the port number in the main arguments (you should rewrite the launcher script (*.scol) in your package)
- In the 'player' VM, when done, open a channel to the 'waiting' VM (_openchannel), in local (127.0.0.1 by default) and on this famous port number.
- In the 'waiting' VM, when connected, destroy the Scol server and destroy the VM.

In fact, your 'launcher' will be the 'waiting' VM.
So, its code should be light and should be quickly loaded. The 'player' will be loaded after that.

If any, i'll try it with os3d (i don't know when i could).

Offline

#3 4-Dec-2014 13:52:59

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

Thx Iri I will write some code and if you have time you can let me know which direction to go in...I.will start with two comp windows with images....window 1 will launch window and window 2 will have a timer, and when the timer has completed it will tell the window 1 to shut down.

Offline

#4 4-Dec-2014 14:15:45

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

Yes, be simply.

Offline

#5 4-Dec-2014 17:05:31

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

iri wrote:

Yes, be simply.


I agree. So here is the code for two machines. The heavy duty machine starts up first. It creates the eyecandy machine to keep the user occupied:

fun shut_down_other_machine()=
	
	//Iri I await your instructions....this is where I am stuck, from the scol tutorial .pdf I assume I am opening a new channel and creating a vm on that channel or something of the sort...
	0;;


//this is for the heavy duty window
fun main()=

//code here to start up eyecandy machine
psuedocode to start up the eyecandy machine, I assume you call another .scol file?
_showconsole
_fooS "this machine is doing the heavy work, when it is done, it will set off a timer to tell the other machine to shut down, or become invisible for later use";
//right now, we will just start a timer for one and a half seconds. when the time is completed, this machine will shut down the other machine
_rfltimer _starttimer _channel 1500 @shut_down_other_machine; 
	
	0;;

here is the eyecandy machine, it shows pictures, videos (funny cat videos?) other dynamic or static content to user, just pictures and maybe video, no 3d stuff:

//code for eye candy machine to keep user occupied
//the heavy duty machine will call this function when the heavy duty machine is done with its heavy duty work
fun destroy_machine()=
_closemachine; 
	0;;


//this is for the heavy duty window
fun main()=
_showconsole
_fooS "this machine will keep the user occupied. when the heavy duty machine is done with this work, it will destroy this machine";
_fooS "this machine will show the user pictures, videos maybe, text, (or video?, webnavigator?) lightweight content";
_fooS "in the ultimate project, this machine will show the user pictures, video, downloaded information, whatever the author wants. it will also receive messages from the other machine and let the user know what is going on with that other very busy machine.  eventually this machine won't be destroyed every time the heavy duty machine is done working, but it will be made invisible so it can be used again ";

	
	0;;

Offline

#6 4-Dec-2014 19:35:36

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

I quickly write this example :

I launch the "eyecandy" VM.
The eyecandy launches a new VM : the real player.
When the player is loaded, the eyecandy shuts down.

First, the "eyecandy" code :

typeof ScolServer = Srv;;  // the Scol server object
typeof ScolServerPort = I;;  // the used port number
typeof win = ObjWin;;  // the "waiting" window


// should return 0 if success
// this function launches a new VM with its script
fun launchPlayer ()=
	let "MyPlayer" -> vmName in
	let 5 -> seconds in  // the time to simulate a loading ....
	let strcatn "_load \"directory1/directory2/player.pkg\"\nmain " :: (itoa seconds) :: " \"" :: (itoa ScolServerPort) :: "\"" :: nil -> vmScript in
	_fooId _newmachine vmName vmScript nil nil;;

fun paint (win, text)=
	_PAINTtext text;
	0;;

fun ohWait ()=
	let _GETscreenSize -> [ws hs] in
	let [400 300] -> [w h] in
	(
	set win = _CRwindow _channel nil (ws/2)-(w/2) (hs/2)-(h/2) w h WN_NORMAL "Waiting";
	_CBwinPaint win @paint _CRtext _channel win 5 5 w-10 h-10 ET_BORDER|ET_ALIGN_CENTER "Wait a minute, the player is loading ...";
	_PAINTwindow win;
	0
	);;

fun main ()=
	_showconsole;
	
	_fooS "Eyecandy : Start !";
	
        // i initialize the port number to an arbitrary value
	set ScolServerPort = 3500;
        // First, i create the server. After, i 've able to receive a message from another VM when its loading is complete
	// Set the Scol server. I loop to find the first free port number on this machine.
	while (nil == set ScolServer = _setserver _envchannel _channel ScolServerPort nil) do
		set ScolServerPort = ScolServerPort+1;
	// i display a window to wait the loading	
	ohWait;
	_fooS strcat "Port number : " itoa ScolServerPort;
        // i call the function to launch my "player"
	launchPlayer;
	_fooS "Eyecandy : Done !";
	0;;

// This function is implicit in Scol. Automatically, Scol searches this function name in the current environment for each new connection.
// If found, it is executed. Else, it is ignored.
// here, it closes the server and the window and destroy the VM.	
fun _connected ()=
	_fooS "Eyecandy : player loaded !!";
	_closeserver ScolServer;
	_DSwindow win;
	_closemachine;;

Think to adapt the good path in 'vmScript' in launchPlayer !

The eyecandy launcher :

_load "directory1/directory2/eyecandy.pkg"
main

Adapt the path too !

The player code :

It is very simple : it simulates a loading. When complete, a new channel (a network link if you prefer) is created to the eyecandy VM to tell 'i'm ok, i'm loaded !'

fun main (iSeconds, szPortNumber)=
	_showconsole;
	
	_fooS "Player : Start !";
	
	let iSeconds*1000 -> milliseconds in
	let _tickcount -> start in
	let start -> n in
	(
	while milliseconds >= n-start do
		set n = _tickcount;
	n-start
	);
	
	_fooS "Player : Loaded !";
	
	_openchannel strcat "127.0.0.1:" szPortNumber nil nil;
	
	_fooS "Player : Done !";
	0;;

When the player is loaded, the eyecandy should be destroyed.
Note : there is ONE launcher (*.scol), NOT two. To launch the player, you must launch the eyecandy. That's all.

In the log, you have two files (for the eyecandy and for the player) and you can read something like :

For the eyecandy :

Eyecandy : Start !
create Tcp server 4848328 on 3500
NewWindow: 1376484
Port number : 3500
launching C:\Program Files\Scol Voyager\Scol.exe C:\Program Files\Scol Voyager $MyPlayer$%5fload+%22bin%2ftest%2fplayer%2epkg%22%0amain+5+%223500%22 CSDMRWK 8650752
0
Eyecandy : Done !
Connection from 127.0.0.1:42647

> exec: _connected

Eyecandy : player loaded !!
delete servers
SSocketTCP write : Succès
Dead Scol Machine 1 (0)
EndScolMachine 0

For the player :

> exec: main 5 "3500"

Player : Start !
Player : Loaded !
Opening Channel ...
Connect to 127.0.0.1 (127.0.0.1):3500
ChannelIP: 127.0.0.1:3500

Offline

#7 4-Dec-2014 19:40:27

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

You can find some more infos on the wiki : http://redmine.scolring.org/projects/tu … er_in_Scol

Offline

#8 4-Dec-2014 20:28:26

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

Excellent!!!! I will try this tomorrow. Thx man!

Offline

#9 5-Dec-2014 00:38:49

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

I just tried out your code, I'm printing it up to study tonight, it works perfectly.

Offline

#10 5-Dec-2014 16:13:32

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

I've looked at the code....so is this the line in the player.pkg that causes the _connected function in the eyecandy.pkg to be executed?

_openchannel strcat "127.0.0.1:" szPortNumber nil nil;

Offline

#11 5-Dec-2014 16:45:10

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

Yes.

With this channel, i can send and receive messages :
player can send a message to eyecandy
if needed, eyecandy can send a message to player by this same channel.

When a channel is closed, a _closed function is also automatically searched.

Offline

#12 5-Dec-2014 16:49:28

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

_openchannel create a channel between our two VMs.

A channel is :
- a network connection (even if it is on the same machine)
- an environment (all Scol APIs + other functions if needed.

So, the network link allow you to send / receive messages
The environment allows you to "understand" these messages.

Offline

#13 5-Dec-2014 17:27:00

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

thx iri..two questions
1. does this mean that once any other vm is started on my computer (aside from the two we have created), will the eyecandy vm shut down?
and
2. can the player.pkg send a message to the eyecandy.pkg. let's say i define this function in eyecandy.pkg:

fun saysomething(string)=
_fooS string;
0;;

....how would i get the player.pkg to trigger saysomething "hello from player pkg" in the eyecandy pkg? I think you see where I am going here, since I will be creating an eyecandy vm that will be needed to send messages to the user over the lifetime of an app session.

Offline

#14 5-Dec-2014 17:43:15

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,161
Website

Re: multithreading with two vm's?

ho you are starting serious scol handling smile

what does Iri is creating a server (eyecandy) and a client with TCP connection.
and killing the server application once the client were able to connect (so it respond)

you can change the behavior to connect the client on start, and close the connection at the end.
so destroy the server application on client connection lost (but be careful of timeout)

then once you're connected to the server you can send and receive messages on the connection channel.

Offline

#15 5-Dec-2014 17:58:29

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

arkeon wrote:

ho you are starting serious scol handling smile

what does Iri is creating a server (eyecandy) and a client with TCP connection.
and killing the server application once the client were able to connect (so it respond)

you can change the behavior to connect the client on start, and close the connection at the end.
so destroy the server application on client connection lost (but be careful of timeout)

then once you're connected to the server you can send and receive messages on the connection channel.

thx arkeon, so how would i send a message from the player.pkg to the eyecandy.pkg using the simple function definition above. (this is a new area for me, so I am a bit lost)

looks like no need for visual studio or vb.net  in order to keep users from being bothered by greyed out screens.  smile

Last edited by hebdemnobad (5-Dec-2014 18:16:40)

Offline

#16 5-Dec-2014 18:26:17

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

For that, you must define a Comm object.

From the player :

You define a such object by the keyword defcom (defcomvar exists too).
Once defined, you use the Scol function _on to send a message on a given channel.

So, look at this example :

defcom FromPlayerToEyecandy = ReceiveFromPlayer I S;;

This object is defined like another Scol object (typeof, struct, etc)
I define a Comm object named FromPlayerToEyecandy
This object will be "linked" on the eyecandy VM with a function named __ReceiveFromPlayer. There are always two added underscores in the receiver code.
This object send two values : first, an integer (I), next a string (S).

When you want send a message :

fun sendMessage ()=
    _on ConnectedChannel FromPlayerToEyecandy [10 "Hello !"];  // i send an integer next a string, as i defined in my Comm object
    0;;

ConnectedChannel is the channel previously opened by _openchannel.
The sent datas are always between [ ].

_on + the channel + the Comm object + datas to send

To the eyecandy :

You write a function __ReceiveFromPlayer (don't forget the underscores !) :

fun __ReceiveFromPlayer (anInteger, aString)=
    _fooId anInteger;
    _fooId aString;
    0;;

Warning : if the receiver function is incorrect (bad type, not defined or other errors), the message will be quietly ignored and lost. There is not possible for the sender to know this error.
Often, we do an "intialializing" message to the receiver and the receiver replies to the sender. Then we assume that the connection is ok.
You can manage a reply for each send, if you want.

You can find more informations on the wiki : http://redmine.scolring.org/projects/tu … er_in_Scol
I also wrote an example in the "tutorials" repository (here to exchange between two independant VMs) : http://redmine.scolring.org/projects/tu … ent_server

Offline

#17 5-Dec-2014 18:30:34

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

hebdemnobad wrote:

does this mean that once any other vm is started on my computer (aside from the two we have created), will the eyecandy vm shut down?

Only the connected VMs on the defined channel will receive the message.
If any, other VMs are ignored.

Offline

#18 5-Dec-2014 18:32:39

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

thx iri and arkeon  i am beginning to see how this works.  i will try this out this weekend.

i guess i bought all those vb books for nothing. i guess i will keep them around,
perhaps at some point i will find something that scol cannot do.

but so far, that hasn't happened. smile

Offline

#19 5-Dec-2014 19:13:19

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

I think you should understand my first code before read this update.

I don't write comments, so ask if needed.

Now, the player send a message to eyecandy for each done step.
For ease, i create a Timer to simulate a loading. For each top, a step is done.

In the eyecandy, i added a progress bar and a message "Step N is done".
When all steps are completes, the eyecandy is destroyed.

The eyecandy :

typeof ScolServer = Srv;;
typeof ScolServerPort = I;;

typeof win = ObjWin;;
typeof winchild = ObjWin;;
typeof font = ObjFont;;

typeof iTotalStep = I;;
typeof szCurrentStep = S;;
typeof iCurrentStep = I;;
var iCurrentStep = 0;;

var bgColor = 0xdddddd;;
var foColor = 0;;


// should return 0 if success
fun launchPlayer ()=
	let "MyPlayer" -> vmName in
	let 5 -> seconds in
	let _fooS strcatn "_load \"directory_1/directory_2/player.pkg\"\nmain " :: " \"" :: (itoa ScolServerPort) :: "\"" :: nil -> vmScript in
	_fooId _newmachine vmName vmScript nil nil;;

fun paint (win, text)=
	let _GETwindowSizePosition winchild -> [wc hc _ _] in
	(
	_PAINTtext text;
	_PAINTrectangle winchild 0 0 (wc/iTotalStep)*iCurrentStep hc DRAW_SOLID 1 bgColor DRAW_SOLID bgColor;
	_TXTout winchild font wc/2 (hc/4)*3 TD_BASELINE|TD_CENTER foColor szCurrentStep;
	0
	);;

fun ohWait ()=
	let _GETscreenSize -> [ws hs] in
	let [400 300] -> [w h] in
	(
	set win = _CRwindow _channel nil (ws/2)-(w/2) (hs/2)-(h/2) w h WN_NORMAL "Waiting";
	set winchild = _CRwindow _channel win 5 30 w-10 20 WN_CHILDINSIDE "";
	set font = _CRfont _channel 14 0 0 "Arial";
	_CBwinPaint win @paint _CRtext _channel win 5 5 w-10 20 ET_BORDER|ET_ALIGN_CENTER "Wait a minute, the player is loading ...";
	_PAINTwindow win;
	0
	);;

fun main ()=
	_showconsole;
	
	_fooS "Eyecandy : Start !";
	
	set ScolServerPort = 3500;
	while (nil == set ScolServer = _setserver _envchannel _channel ScolServerPort nil) do
		set ScolServerPort = ScolServerPort+1;
		
	ohWait;
	_fooS strcat "Port number : " itoa ScolServerPort;
	launchPlayer;
	_fooS "Eyecandy : Done !";
	0;;
	
	
fun end ()=
	_fooS "Eyecandy : player loaded !!";
	_closeserver ScolServer;
	_DSwindow win;
        _DSfont font;
	_closemachine;;

/* The receiver functions */
fun __fromPlayerInit (iSeconds)=
	set iTotalStep = iSeconds;
	0;;
	
fun __fromPlayer (szStep)=
	if szStep == nil then
		end
	else
	(
		set szCurrentStep = szStep;
		set iCurrentStep = iCurrentStep+1;
		_PAINTwindow win;
		0
	);;

The player :

defcom CtoEyeCandyInit = fromPlayerInit I;;
defcom CtoEyeCandy = fromPlayer S;;
typeof ChannelToEyeCandy = Chn;;

fun cbLoading (o, u)=
	let u -> [n] in
	(
	mutate u <- [n+1];
	let _fooS strcatn "Step " :: (itoa n) :: " done !" :: nil -> message in
	_fooS strcat "Send a message to eyecandy ? " itoa _on ChannelToEyeCandy CtoEyeCandy [message];
	
	if n == 6 then
	(
		_fooS "Player : Loaded !";
		_fooS strcat "Send an ending message to eyecandy ? " itoa _on ChannelToEyeCandy CtoEyeCandy [nil];
		_fooS "Player : Done !";
		_deltimer o;
		0
	)
	else
		0
	);;

fun main (szPortNumber)=
	_showconsole;
	
	_fooS "Player : Start !";
	if nil == set ChannelToEyeCandy = _openchannel strcat "127.0.0.1:" szPortNumber nil nil then
	(
		_fooS "Unable to connect the 'EyeCandy', the program exits";
		1
	)
	else
	(
		_fooS strcat "Send an init message to eyecandy ? " itoa _on ChannelToEyeCandy CtoEyeCandyInit [5];
		_rfltimer _starttimer _channel 3000 @cbLoading [1];
		0
	);;

Now, i don't need to use the implicit _connected function, thus i don't write it.
But i write two receiver functions : first to know the total step number, the second to know each new finished step.

PS : don't forget to adapt the package path before try this !

Offline

#20 5-Dec-2014 19:33:52

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: multithreading with two vm's?

If you wish send a message from the eyecandy to the player, you should save the channel (because it is NOT the current channel '_channel' !).

In the player code, it is ChannelToEyeCandy, already defined.
In the eyecandy, you could update like this :

typeof ChannelToPlayer = Chn;;

fun __fromPlayerInit (iSeconds)=
	set iTotalStep = iSeconds;
	set ChannelToPlayer = _channel;
	0;;

And send a message on this channel by _on after.

Offline

#21 5-Dec-2014 19:40:07

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

thx iri i will print up your code and study it over the weekend.

Offline

#22 9-Oct-2024 22:35:37

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,543
Website

Re: multithreading with two vm's?

hello everyone...i just tried running iri's code and it no longer workers...has the channel/environment/server api been changed since 2014?

-Dan

Offline

Board footer

Powered by FluxBB