Little experiments

I try, I share.

Golo - Little 4

So, last time I told you I started to write some kind of GoloHttpServer.
In that process (which is still WIP) I had several questions.

One of the first was: a server will receive several requests, I need to launch threads.
I knew how to do that with Java, how to do that with Golo.

The solution I found is based on the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module little.experiment4_1

import java.lang

function run = |number| {
  println ("I am thread "+number)
}

function main = |args| {

  for (var thread_number = 0, thread_number < 4, thread_number = thread_number + 1) {
      let process = java.lang.Thread({run(thread_number)}: to(Runnable.class))
      process: start()
  }
}

That’s nice and I’m sure some of you know how to do it in different ways.
I will enjoy suggestions and comments on this.

But remember, I’m explaining how to build a http server.
Having some threading methods is useful, but we need to receive something from a browser.
Let’s start some game with sockets !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module little.experiment4_2

import java.net.InetAddress
import java.net
import java.lang

function run = |connectedClient| {   
  println ("step after step young padawan. Http answer will be in next article ;)")
}

function main = |args| {
  # create a socket
  let addr = InetAddress.getByName("127.0.0.1")
  let Server = ServerSocket (5000, 10, addr)
  println ("TCPServer Waiting for client on port 5000")

  # just loop until a request, then quit
  # this is for sample purpose of course ;)
  var serverRun = true
  while(serverRun) {
      # get the request
      let clientSocket = Server: accept()
      # launch the process of this request
      let process = java.lang.Thread({run(clientSocket)}: to(Runnable.class))
      process: start()
      # for this sample, just tell we want to stop
      serverRun = false
  }
}

I hope everything is clear with the included comments.
If you have questions, use comments here or send me a note to my email if you prefer.

Comments