about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-06-01 03:17:49 +0200
committerEmile <hanemile@protonmail.com>2019-06-01 03:17:49 +0200
commit92ef99305664f2c4dd66dd5f3a85d3dffc965bdf (patch)
treeb7395294d0c4c836036bece743116035abcd0332
parent6f2950ac6cbbf689c871f7c783668089c6e3a1c0 (diff)
Saturday morning #GPN19 commit
-rw-r--r--flexerilla_logo.pngbin0 -> 26598 bytes
-rw-r--r--flexerilla_logo_100x82.jpgbin0 -> 10691 bytes
-rw-r--r--go.mod6
-rw-r--r--go.sum6
-rw-r--r--main.go166
5 files changed, 114 insertions, 64 deletions
diff --git a/flexerilla_logo.png b/flexerilla_logo.png
new file mode 100644
index 0000000..0b1acf1
--- /dev/null
+++ b/flexerilla_logo.png
Binary files differdiff --git a/flexerilla_logo_100x82.jpg b/flexerilla_logo_100x82.jpg
new file mode 100644
index 0000000..0533bde
--- /dev/null
+++ b/flexerilla_logo_100x82.jpg
Binary files differdiff --git a/go.mod b/go.mod
index d6ee0fc..7e6255b 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,9 @@
 module git.darknebu.la/pixeltsunami
 
 go 1.12
+
+require (
+	github.com/mattn/go-runewidth v0.0.4 // indirect
+	golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 // indirect
+	gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
+)
diff --git a/go.sum b/go.sum
index 841c93f..9697343 100644
--- a/go.sum
+++ b/go.sum
@@ -5,10 +5,16 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
 github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
 github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
 github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
+github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
 golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
 golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 h1:T5DasATyLQfmbTpfEXx/IOL9vfjzW6up+ZDkmHvIf2s=
+golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 gopkg.in/VividCortex/ewma.v1 v1.1.1 h1:tWHEKkKq802K/JT9RiqGCBU5fW3raAPnJGTE9ostZvg=
 gopkg.in/VividCortex/ewma.v1 v1.1.1/go.mod h1:TekXuFipeiHWiAlO1+wSS23vTcyFau5u3rxXUSXj710=
+gopkg.in/cheggaaa/pb.v1 v1.0.28 h1:n1tBJnnK2r7g9OW2btFH91V92STTUevLXYFb8gy9EMk=
+gopkg.in/cheggaaa/pb.v1 v1.0.28/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
 gopkg.in/cheggaaa/pb.v2 v2.0.6 h1:L2KAo2l2ZQTzxmh8b9RdQpzgLpK2mX3paGCMJSUugBk=
 gopkg.in/cheggaaa/pb.v2 v2.0.6/go.mod h1:0CiZ1p8pvtxBlQpLXkHuUTpdJ1shm3OqCF1QugkjHL4=
 gopkg.in/fatih/color.v1 v1.7.0 h1:bYGjb+HezBM6j/QmgBfgm1adxHpzzrss6bj4r9ROppk=
diff --git a/main.go b/main.go
index 97de251..49efbf1 100644
--- a/main.go
+++ b/main.go
@@ -1,33 +1,39 @@
 package main
 
 import (
+    "gopkg.in/cheggaaa/pb.v1"
 	"bytes"
+	"bufio"
 	"flag"
 	"fmt"
 	"image"
 	"log"
 	"net"
 	"os"
+     _ "image/jpeg"
 )
 
 var (
+
+    // Define command line arguments
 	host = flag.String("host", "127.0.0.1", "Server address (v4)")
 	port = flag.String("port", "1337", "Server port")
 	address string
 
 	imagePath = flag.String("image", "", "Relative image path")
-	imageOffsetX = flag.Int("xoffset", 0, "xoffset")
-	imageOffsetY = flag.Int("yoffset", 0, "yoffset")
+    xoffset = flag.Int("xoffset", 0, "xoffset")
+	yoffset = flag.Int("yoffset", 0, "yoffset")
 
 	canvasWidth = flag.Int("width", 1920, "canvas width")
 	canvasHeight = flag.Int("height", 1080, "canvas height")
 
 	testConn = flag.Bool("t", false, "test the connection before escalating completely")
 
-	fill = flag.Bool("fill", true, "fill the complete canvas")
+	fill = flag.Bool("fill", false, "fill the complete canvas")
 	color = flag.String("col", "000000", "define a color")
 
 	cores = flag.Int("cores", 1, "Amount of cores to use")
+	loops = flag.Int("loops", 500, "Amount os loops to run")
 )
 
 // parse the command line
@@ -37,7 +43,7 @@ func parseFlags() {
 	address = fmt.Sprintf("%s:%s", *host, *port)
 }
 
-// test the connection to the given server if the -t flag was set
+// testConnection to the given server if the -t flag was set
 func testConnection() {
 	if *testConn == true {
 		log.Printf("[ ] Testing Connection: True")
@@ -50,9 +56,10 @@ func testConnection() {
 	}
 }
 
-// test connecting to the server using the given protocol ("udp" or "tcp")
+// testConnectionProtocol tests the connection to the server using the given protocol ("udp" or "tcp")
 func testConnectionProtocol(protocol string) {
-	connection, netDialErr := net.Dial(protocol, address)
+    log.Printf("[D] address: %s", address)
+    connection, netDialErr := net.Dial(protocol, address)
 	if netDialErr != nil {
 		log.Fatal(netDialErr)
 	}
@@ -62,16 +69,15 @@ func testConnectionProtocol(protocol string) {
 	}
 }
 
+// buildSendString builds a string representing the rectangle defined in fill mode
 func buildSendString(start int, end int, doneChannel chan bool, stripBufferChannel chan bytes.Buffer) {
-	fmt.Printf("start: %d \t end: %d\n", start, end)
-
 	var stripBuffer bytes.Buffer
 	var currentCommand string
 
 	for x := start; x < end; x++ {
 		for y := 0; y < *canvasHeight; y++ {
 			// prepare the command that should be written
-			currentCommand = fmt.Sprintf("PX %d %d %s\n", x, y, *color)
+			currentCommand = fmt.Sprintf("PX %d %d %s\n", y + *yoffset, x + *xoffset, *color)
 			stripBuffer.Write([]byte(currentCommand))
 		}
 	}
@@ -82,37 +88,44 @@ func buildSendString(start int, end int, doneChannel chan bool, stripBufferChann
 
 // openImage opens an image at the given path and returns an image.Image
 func openImage(imagePath string) image.Image {
-	log.Println("AA")
-	reader, openErr := os.Open(imagePath)
+    log.Println("[ ] Opening the image (%s) ...", imagePath)
+	file, openErr := os.Open(imagePath)
+    defer file.Close()
 	if openErr != nil {
+        log.Println("[E] Error while opening the image")
 		log.Fatal(openErr)
 	}
-	log.Println("AB")
-
-	fmt.Println(*reader)
+    log.Println("[ ] Done opening the image")
 
-	img, _, imageDecodeErr := image.Decode(reader)
-	if imageDecodeErr != nil {
-		log.Fatal(openErr)
-	}
-	log.Println("AC")
+    log.Println("[ ] Decoding the image...")
+    img, _, err := image.Decode(bufio.NewReader(file))
+    if err != nil {
+        log.Println("[E] Error while decoding the image")
+		log.Fatal(err)
+    }
+    log.Println("[ ] Done decoding the image")
 
 	return img
 }
 
+// buildSendStringImage builds the string represesenting the given image for the pixelflut server
 func buildSendStringImage(image image.Image, start int, end int, doneChannel chan bool, stripBufferChannel chan bytes.Buffer) {
 
 	var imageHeight = image.Bounds().Max.Y
 	var stripBuffer bytes.Buffer
 
-	fmt.Printf("ImageHeight: %d", imageHeight)
+	log.Printf("ImageHeight: %d", imageHeight)
 
 	for x := start; x < end; x++ {
 		for y := 0; y < imageHeight; y++ {
 			r, g, b, _ := image.At(x, y).RGBA()
-			fmt.Printf("%v %v %v", r, g, b)
-			command := fmt.Sprintf("PX %d %d %.2x%.2x%.2x", x, y, r, g, b)
-			stripBuffer.Write([]byte(command))
+            if r > 10 || g > 10 || b > 10 {
+                command := fmt.Sprintf("PX %d %d 000000", x, y)
+                stripBuffer.Write([]byte(command))
+            } else {
+                command := fmt.Sprintf("PX %d %d ffffff", x, y)
+                stripBuffer.Write([]byte(command))
+            }
 		}
 	}
 
@@ -121,26 +134,32 @@ func buildSendStringImage(image image.Image, start int, end int, doneChannel cha
 }
 
 func main() {
-	log.Printf("[ ] %s -> %s:%s at (%d, %d)", *imagePath, *host, *port, imageOffsetX, imageOffsetY)
+	log.Printf("[ ] %s -> %s:%s at (%d, %d)", *imagePath, *host, *port, xoffset, yoffset)
 	parseFlags()
 	testConnection()
 
 	if *fill == true {
-		// define channels used to bundle the data generated and get information
+        log.Println("[M] FILL MODE")
+		// Define channels used to bundle the data generated and get information
 		doneChannel := make(chan bool)
 		stripBufferChannel := make(chan bytes.Buffer)
 		var completeBuffer bytes.Buffer
 
-		// calculate the width of the individual stripes
+		// Calculate the width of the individual stripes
 		var stripwidth int = *canvasWidth / *cores
 
-		// create a new buildStringBot generating the stripes
+		// Create a new buildStringBot generating the stripes
+        count := *cores
+        bar := pb.StartNew(count)
 		for thread := 0; thread < *cores; thread++ {
-			log.Printf("Starting thread %d", thread)
+            bar.Increment()
 			go buildSendString(thread * stripwidth, (thread + 1) * stripwidth, doneChannel, stripBufferChannel)
 		}
+        bar.FinishPrint("Done Starting all threads")
 
-		// catch all threads
+		// Catch all threads
+        count = *cores
+        bar = pb.StartNew(count)
 		for thread := 0; thread < *cores; thread++ {
 			// get a "Done" message from each worker
 			_ = <- doneChannel
@@ -149,102 +168,121 @@ func main() {
 			stripBufferChannelOutput := <- stripBufferChannel
 			completeBuffer.Write(stripBufferChannelOutput.Bytes())
 
-			log.Printf("Thread %d done!", thread)
+            bar.Increment()
 		}
+        bar.FinishPrint("Done catching all threads")
 
-		// write the command to the server
+		// Connect to the server
 		connection, netDialError := net.Dial("tcp", address)
 		if netDialError != nil {
 			log.Fatal(netDialError)
 		}
 
-
+        // Write the buffer to the connection
 		i := 0
-		for i < 500 {
+        count = *loops
+        bar = pb.StartNew(count)
+		for i < *loops {
 			// actual write
 			_, writeErr := connection.Write(completeBuffer.Bytes())
 			if writeErr != nil {
 				log.Fatal(writeErr)
 			}
 
-			fmt.Printf(".")
+            bar.Increment()
 			i++
 		}
+        bar.FinishPrint("Done writing !")
 
 		fmt.Printf("\n")
 
-		// close the connection
+		// Close the connection
 		connectionCloseError := connection.Close()
 		if connectionCloseError != nil {
 			log.Fatal(connectionCloseError)
 		}
 
-		// cleanup
+		// Cleanup
 		fmt.Printf("cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
 	}
 
 	if *imagePath != "" {
-		log.Println("[ ] Drawing the image!")
-
-		log.Println("A")
+        log.Println("[M] IMAGE MODE")
 
-		// open the image
+		// Open the image
+        log.Println("[ ] Opening the image...")
 		var image = openImage(*imagePath)
-		fmt.Println(image)
-		log.Println("b")
+        log.Println("[ ] Done opening the image")
 
-		// define channels used to bundle the data generated and get informations
+		// Define channels used to bundle the data generated and get informations
+        log.Println("[ ] Creating channels...")
 		doneChannel := make(chan bool)
 		stripBufferChannel := make(chan bytes.Buffer)
 		var completeBuffer bytes.Buffer
-		log.Println("c")
+        log.Println("[ ] Done creating channels")
 
-		// calculate the width of the individual stripes
+		// Calculate the width of the individual stripes
+        log.Println("[ ] Creating stripwidth...")
 		var stripwidth int = image.Bounds().Max.X / *cores
-		log.Println("d")
+        log.Println("[ ] Done calculating stipwith")
 
-		// create new buildSendString workers building the string that should be sent to the pixelflut server
+		// Create new buildSendString workers building the string that should be sent to the pixelflut server
+        log.Println("[ ] Creating buildworkers...")
 		for thread := 0; thread < *cores; thread++ {
 			log.Printf("Staring thread %d", thread)
 			go buildSendStringImage(image, thread * stripwidth, (thread+1) * stripwidth, doneChannel, stripBufferChannel)
 		}
-		log.Println("e")
 
-		// catch all the threads
+		// Catch all the threads
+        count := *cores
+        bar := pb.StartNew(count)
 		for thread := 0; thread < *cores; thread++ {
-			// get a "Done" message from each worker
+			// Get a "Done" message from each worker
 			_ = <- doneChannel
 
-			// get the buffer generated and append it to the complete buffer by writing it there
+			// Get the buffer generated and append it to the complete buffer by writing it there
 			stripBufferChannelOutput := <- stripBufferChannel
 			completeBuffer.Write(stripBufferChannelOutput.Bytes())
 
-			log.Printf("Thread %d done!")
+            bar.Increment()
 		}
-		log.Println("f")
+        bar.FinishPrint("Done catching the threads!")
 
-		// write the command to the server
+
+		// Write the command to the server
+        log.Println("[ ] Connecting to server...")
 		connection, netDialError := net.Dial("tcp", address)
 		if netDialError != nil {
 			log.Fatal(netDialError)
 		}
-		log.Println("g")
+        log.Println("[ ] Done connecting to server")
 
-		// actual write
-		_, writeErr := connection.Write(completeBuffer.Bytes())
-		if writeErr != nil {
-			log.Fatal(writeErr)
+        // Actual write
+        log.Println("[ ] Actual send...")
+		i := 0
+        count = *loops
+        bar = pb.StartNew(count)
+		for i < *loops {
+			_, writeErr := connection.Write(completeBuffer.Bytes())
+			if writeErr != nil {
+				log.Fatal(writeErr)
+			}
+
+            bar.Increment()
+			i++
 		}
-		log.Println("h")
+        bar.FinishPrint("The End!")
+        log.Println("[ ] Done sending")
 
-		// close the connection
+		// Close the connection
+        log.Println("[ ] Closing the connection...")
 		connectionCloseError := connection.Close()
 		if connectionCloseError != nil {
 			log.Fatal(connectionCloseError)
 		}
-		log.Println("i")
+        log.Println("[ ] Done closing the connection")
 
-		// cleanup
-		fmt.Printf("cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
+		// Cleanup
+		log.Printf("[ ] Cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
 	}
 }