{"id":225,"date":"2017-04-25T15:10:52","date_gmt":"2017-04-25T15:10:52","guid":{"rendered":"http:\/\/blog.qubekwest.com\/?p=225"},"modified":"2017-04-25T15:10:52","modified_gmt":"2017-04-25T15:10:52","slug":"network-round-5","status":"publish","type":"post","link":"https:\/\/blog.qubekwest.com\/?p=225","title":{"rendered":"Network Round 5"},"content":{"rendered":"<p>Networking is still kicking my butt. \u00a0At the end of the previous post, I&#8217;d started what I refer to as &#8220;Network Round 4&#8221; and I&#8217;ve already retired that version. \u00a0It actually temporarily became the &#8220;com.qubekwest.net.old&#8221; package (referred to just as &#8220;old&#8221; later) as a place to constantly refer back to it as I write what I now call &#8220;Network Round 5.&#8221; \u00a0This has obviously taken a bit of work, but it isn&#8217;t really as horrible as it probably sounds, honest.<\/p>\n<p>The cause of this new iteration of the network code is actually that I&#8217;d gotten pretty far with the previous version, and then realized that I sort of had no idea how to actually use it. \u00a0Meaning, it probably worked, but I didn&#8217;t have any clean patterns to follow to use it in something useful. \u00a0After piles of refactors to try to make it useful, I gave up. \u00a0This in turn inspired a collection of interfaces that I wanted to write and that occasionally conflicted with names already in my networking code. \u00a0The best solution I could come up with to make this as easy as possible was to strip the network code back to nothing as described above with the &#8220;old&#8221; package and start making new files in the &#8220;net&#8221; package that was now completely empty.<\/p>\n<p>This creation of interfaces helped me to determine two things at the same time. \u00a0The first was the things that a &#8220;user&#8221; of the network engine (which is actually other pieces of code, not people) would need it to be able to provide, and the second was the things that the network engine does that don&#8217;t need to be exposed to anyone else. \u00a0Those results may be immediately obvious to other people, since that&#8217;s exactly what interfaces are supposed to do, but in this case it was actually supremely useful.<\/p>\n<p>From the first category of &#8220;need to be able to provide&#8221; I knew that I needed a collection of methods that the server had to be able to do to actually be a server. \u00a0For example, it needed to be able to send data to clients. \u00a0What surprised me a little was the number of different ways I had to be able to send data to the client. \u00a0My primary pattern is that I&#8217;m sending data from a specific source to a specific target. \u00a0Other methods build on that concept for the ability to send updates to other users that are near me in the world. \u00a0Maxing out the idea of sending to groups there are also methods for broadcasting a single piece of data from a specific source, but to all connected users. \u00a0This covers things a server needs to be able to do for other pieces of code.<\/p>\n<p>The second category of &#8220;don&#8217;t need to be exposed&#8221; included things like &#8220;accept a connection.&#8221; \u00a0I initially put a method to accept a connection in the interface for a server, but then realized that there is literally no code outside of the network itself that would ever call it. \u00a0If you think about it for a moment, you will probably realize that the method for accepting connections must be there, but that it will be called because of network traffic, not by some other piece of code. \u00a0That means that it can really be a private method on the network server code itself and building the interface helped me to realize that.<\/p>\n<p>There are several other interfaces as well. \u00a0I&#8217;ve defined what it means to be a network worker thread, which is a piece of code that actually does the work of the server. \u00a0There can be any number of workers in a server, and they each get incoming data handed to them if they want it. \u00a0They can&#8217;t affect each other, but they all get a shot at doing something with the data as it comes in. \u00a0There are also handlers that create a sort of call back structure that allows clients to send data to the server where a response is expected.<\/p>\n<p>This whole ball of wax is being implemented with the Java&#8217;s NIO package patterns. \u00a0I still feel it is vastly more complex than other ways of doing things, but I&#8217;m also pretty confident that the result will perform better with more people connected to the server than the standard socket approach would. \u00a0I&#8217;m hoping that makes this all worth it. \u00a0I&#8217;ve actually spent almost six months working on, researching, writing and rewriting, and generally being frustrated by the network code, so I sure hope so.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Networking is still kicking my butt. \u00a0At the end of the previous post, I&#8217;d started what I refer to as &#8220;Network Round 4&#8221; and I&#8217;ve already retired that version. \u00a0It actually temporarily became the &#8220;com.qubekwest.net.old&#8221; package (referred to just as &#8220;old&#8221; later) as a place to constantly refer back to it as I write what &hellip; <a href=\"https:\/\/blog.qubekwest.com\/?p=225\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Network Round 5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":3,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qubekwest.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}