Examples

Running Multi-System

In this example we want to show how a simple mechanism can be set up to run a program remotely.

The example dispenses with error checking, safety checks etc. because it only serves to show how easy it is to launch an application on a remote system.




(1) The port number is an arbitrary unused port number. Normally you would do this as part of some initialisation of your local program, but as this is all we do we simply quit.

17 Running Multi-System.celsrc - local part
-.-
	Running Multi-System

	On each system that we want (part of) our program to run on, we have to have at least one 
	Virtual Machine Manager running.
	
	This example use two files. The first file (this file) just runs a VM on a remote system
	(or seperate process) and tells it to listen to incoming connections on a port.
-.-
use Windows, Math, Strings, Editor, System

-- The configurator just sets the system up to receive requests
design Configurator is
	-- we tell the system to listen to this port
(1)	system <- Listen("27524")
	
	-- that is all we need to do in this simple case - so we quit !
	destroy self
end
			

Running Multi-System: remote part

The second part of this example is an application that runs on another system - let us call this system B - and that connects to the system above - we call this system A. System B then reads a bytecode file, sends it to system A with the request to load the file. Once the file is loaded, a design from that bytecode can be instantiated by sending the design name and parameters to the creator cell of the system A.


(1)


(2)


(3)

17 Running Multi-System.celsrc - remote part
-.-
	Running Multi-System
	
	This application connects to a remote system, sends a bytecode file and then requests
	the remote system to instantiate one of the cells in that file. The file that is sent
	is the file of the first example of this series.
-.-
use Windows, Math, Strings, Editor, System

design Launcher is
	-- Change the name to the name of your system !
	create RemoteExecution("Apollo", "27524")
end

design RemoteExecution(utf8 HostName, utf8 Port) is

	keep HostName, Port
	ConxClass Conx
	cell Host
	cell HostCreator
	
	-- Create a window for output	
	system <- stdout.Set( create MenuWindow("Running multi-system"))
	
	-- Give some feedback
	system <- println("Connecting to [HostName] on port [Port]")

	-- we connect to the remote host
(1)	system <- TCPIP.Connect(HostName, Port)
	
	destructor is 
		-- Close the connection if any
		Host ? system <- TCPIP.Close( Host )
		
		system <- println("End of RemoteExecution")
	end
	
	-- The message we get when we are connected
	on TCPIP.Connected( ConxClass Conx ) do
	
		-- We talk CellSpeak over the connection - we handle the connection ourself
		sender <- Set.Protocol( Conx, "CLSPK", self )
	
		-- print a message on the display
		system <- println("Connection with [HostName] on port [Port] established")		
	end
	
	-- The following messages is sent by the conxmgr to the handler when the protocol is set up
	on CLSPK.Ready( cell Host ) do
	
		keep Host
		
		-- Now we read the bytecode file
		var Error, ByteCode = File.Read("01 Cells Designs and Messages.cellbyc")
		
		-- Check
		Error ? system <- println("Could not read file ") & yield
		
		-- File was loaded successfuly
		system <- println("File was loaded. Size = [nel ByteCode]")
		
		-- Send the file to the host - also request the creator
(2)		Host <- Creator.Get, ByteCode.Load( ByteCode )	
	end
	
	interface ByteCode.Load is
	
		int RepeatCount=0
		
		on Ok do	
			-- Did i receive the creator already ?
(3)			not HostCreator ? 
				RepeatCount < 10 ? 
					self <- ByteCode.Load.Ok & RepeatCount & RepeatCount += 1 :
					system <- println(m"Load successful, but did not get HostCreator") & yield
			
			-- feedback			
			system <- println("Bytecode successfuly loaded ! - send message to [HostCreator]")
							
			-- we now request to instantiate a cell design 
(4)			HostCreator <- Club.President		
		end
		
		on Failed => system <- println("Byte code load failed")		
	interface end
	
	-- The host will send us the identity of its creator cell
	on Creator( cell HostCreator) do
	
		keep HostCreator
		
		-- Feedback
		system <- println("This is the creator [HostCreator]")		
	end
end