Let’s try more than one connection. Do I need coroutines? Let’s try without. LEARNING: I seem not to be able to have multiple connections within this one program.
I’m supposing that our World server will have a collection of connections, including the server one, which will occasionally need to
accept a new robot connection, and a number of long-lasting robot connections, from which we will occasionally
receive a message and
send a reply.
In the fullness of time, the robot connections will close, at which point we can ditch them.
I’m going to assume that our response time to a message from a robot will be small, so that there is no need to figure out any kind of multi-tasking.
We will, however, need to have everyone’s timeouts set to zero, so that we don’t waste time waiting … but we don’t want to sit in a tight loop trying
receive over and over. There is a function
select that you give a collection of clients and servers, and when any of them has something to provide, the function returns. (I have no idea what goes on while you’re waiting, but using
select seems to be the thing you’re supposed to do.
I might start with the tight loop.
I’m not sure this can be built with anything much like a CodeaUnit test, though I will try. I think I will want to go through a series of steps, separated in time, triggering messages more or less at random. To start, I’ll just make a test like the most recent one, but with more than one client.
I’ll not report every line: I’ll be back when I have some progress or insight or disaster to report.
Well, That Won’t Work
_:test("Multiple clients not round robin", function() local server = getLocalServer(0) local tcp = getTCP(0.01) -- can't be zero local conn1 = tcp:connect(getMyIp(),8888) _:expect(conn1).isnt(nil) local conn2, error = tcp:connect(getMyIp(),8888) _:expect(conn2, "conn2 "..error or "").isnt(nil) end)
I just tried to make two connections, and this is the error:
5: Multiple clients not round robin conn2 already connected -- Actual: nil, Expected: nil
So I guess a single program can’t have multiple connections to the same ip and port. Credible, though I don’t remember any docs saying that.
On StackOverflow, the replies seem to vary between absolutely no and absolutely yes.
Since I don’t really need to do multiple connections from one machine, let’s see if I can approach the situation a different way.
What Do I Need?
Well, right now I don’t really need anything, since I’m not going to run my own version or robot worlds on a network of iPads running Codea. (Might be amusing, though …)
I think I do need a credible ability to build a server that can handle multiple connections. I’m not sure what priority to give that. To test that with real connections … I’m not sure how I can do that, but I can certainly test it with some kind of fakes if need be.
I need to do the work on the JSON junk, boring though it is. If I am to be as careful as Hill, I need to test to be sure that all messages to the server return a valid message even if it is an error message.
And I need to get the robot-world connection to handle at least all the interesting cases of interaction, ideally all of them.
I do not feel it necessary to do everything in the Robot Worlds spec. My purpose here is to show you a bit about how I work with a program like this one, how I make decisions, what mistakes I make, and so on. So where a problem is likely to show up something new, I really want to go after it, and where it is more of the same, I am less interested.
I think I”ll probably proceed on a single robot-to-server hookup, perhaps finessing the whole TCP issue for now. In fact, I’m sure that with one connection I could run two robots. We’ll see.
But, for now, at least, I’m going to table additional work on TCP.
Slightly irritated by that outcome. Maybe I’ll get a good idea later.