;stand: 05.07.2017 Author: Candyland
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <ExchangeVariables.au3> ;Im Anhang | von ***Eukalyptus***
#include <array.au3>


Global $Variable_Main, $GotVariable, $WaitForVariable

if $cmdline[0] = '' Then
	Global $ID = 0
ElseIf $cmdline[1] <> '' Then
	Global $ID = Number($cmdline[1])
EndIf



If $ID = 0 Then ; CONTROLLER 0

	_ExchangeVariables_Startup(0, 'MAIN', '_VarMsg')

	For $i = 1 To 4 Step 1
		ShellExecute(@ScriptFullPath, $i, @ScriptDir, '', @SW_SHOW) ;Execute dich selbst 4 mal mit Parameter 1-4
	Next

	ConsoleWrite(@CRLF & "Who's There?" & @CRLF)
	$WaitForVariable = 1
	For $i = 1 To 4 Step 1
		$GotVariable=0
		Do
			Sleep(100)
			_ExchangeVariables_SetVariable($i, "Variable_Main", 'who') ;Sag hallo
		Until $GotVariable ;& warte auf antwort
		ConsoleWrite($Variable_Main & @CRLF)
	Next
	$WaitForVariable = 0
	ConsoleWrite("Everyone is there.. Nice" & @CRLF)

	ConsoleWrite(@CRLF & "Lets Collect a tree of your Files" & @CRLF)
	$ALLDISKFILES = _RecursiveFileListToArray('C:', '', 1) ;baue Datei Stammbaum | Testweise nur C:
	$ROUND = Round((UBound($ALLDISKFILES)-1) / 4) ;durch 4 & abrunden
	ConsoleWrite("Cool Cool you have " & UBound($ALLDISKFILES)-1 & " Files" & @CRLF)

	Local $COUNT=0, $VARTOSEND
	For $i = 1 To 4 Step 1

		For $is = 1 + $COUNT To $ROUND + $COUNT Step 1 ;<= Das muss noch getestet werden
			$COUNT += 1
			if $ALLDISKFILES[$is] <> '' Then
				$VARTOSEND &= $ALLDISKFILES[$is] & '||' ;Wollen uns die arbeit, wie in nem team, teilen. deshalb bekommt jeder was zutun
			EndIf


		Next

		ConsoleWrite('Send it to Worker ' & $i)
		$mayError = _ExchangeVariables_SetVariable($i, "Variable_Main", $VARTOSEND) ; sende variable
		$VARTOSEND = ''
		if $mayError = 1 Then
			ConsoleWrite(' | Sended')
		Elseif $mayError = 0 Then
			ConsoleWrite(' | Failed to Send') ;<= Später | Starte speziefischen Worker neu & gebe Variable erneut
		EndIf

		$WaitForVariable = 1
		$GotVariable = 0
		Do
			Sleep(100)
		Until $GotVariable
		$WaitForVariable = 0
		ConsoleWrite(' | Received' & @CRLF)

		if $Variable_Main <> 'GotIt' & $i Then
			ConsoleWrite('Worker ' & $i & ' Failed' & @CRLF)
		EndIf

	Next










ElseIf $ID <> 0 Then ; WORKER 1-4

	ConsoleWrite('ID: ' & $ID & @CRLF)

	_ExchangeVariables_Startup($ID, 'MAIN', '_VarMsg')
	$WaitForVariable = 1
	$GotVariable = 0
	Do
		Sleep(100)
	Until $GotVariable
	$WaitForVariable = 0
	ConsoleWrite('IM HERE' & @CRLF)
	_ExchangeVariables_SetVariable(0, "Variable_Main", 'Here is Worker ' & $ID) ;Moinsen

	;//WAIT FOR FILELIST//
	$WaitForVariable = 1
	$GotVariable=0 ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!??????????????????
	ConsoleWrite('$GotVariable= ' & $GotVariable & @CRLF)
	;///////////////////////////////////////////
	;Schleife die Probleme macht
	Do
		Sleep(100)
	Until $GotVariable ;FICK DICK ANWENDER ICH BIN ZWAR NE NULL GEHE ABER TROTZDEM WEITER
	;///////////////////////////////////////////
	$WaitForVariable = 0

	$DATA = StringSplit($Variable_Main, "||", 1)
	_ArrayDisplay($DATA) ;TROTZDEM HAB ICH DEINE, NOCH NICHT GESENDETEN, DATEN NICHT. Liegt wohl daran das der Controller diese noch garnicht einmal ausarbeitet hat & damit diese auch noch nicht senden konnte.

	_ExchangeVariables_SetVariable(0, "Variable_Main", 'GotIt' & $ID)

EndIf







Func _VarMsg($hWnd, $iMsg, $wParam, $lparam) ;komme bei mir an & sag per variable das es hier ist
	if $WaitForVariable = 1 Then
		$GotVariable=1
	EndIf
EndFunc

;Autor: Oscar (autoit.de)
Func _RecursiveFileListToArray($sPath, $sPattern, $iFlag = 0, $iFormat = 1, $sDelim = @CRLF)
	Local $hSearch, $sFile, $sReturn = ''
	If StringRight($sPath, 1) <> '\' Then $sPath &= '\'
	$hSearch = FileFindFirstFile($sPath & '*.*')
	If @error Or $hSearch = -1 Then Return SetError(1, 0, $sReturn)
	While True
		$sFile = FileFindNextFile($hSearch)
		If @error Then ExitLoop
		If StringInStr(FileGetAttrib($sPath & $sFile), 'D') Then
			If StringRegExp($sPath & $sFile, $sPattern) And ($iFlag = 0 Or $iFlag = 2) Then $sReturn &= $sPath & $sFile & '\' & $sDelim
			$sReturn &= _RecursiveFileListToArray($sPath & $sFile & '\', $sPattern, $iFlag, 0)
			ContinueLoop
		EndIf
		If StringRegExp($sFile, $sPattern) And ($iFlag = 0 Or $iFlag = 1) Then $sReturn &= $sPath & $sFile & $sDelim
	WEnd
	FileClose($hSearch)
	If $iFormat Then Return StringSplit(StringTrimRight($sReturn, StringLen($sDelim)), $sDelim, $iFormat)
	Return $sReturn
EndFunc   ;==>_RecursiveFileListToArray


