Java-Gaming.org Hi !
Featured games (88)
games approved by the League of Dukes
Games in Showcase (681)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (744)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2 3 ... 34
1  Game Development / Networking & Multiplayer / [SOLVED] Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-06 23:51:34
Does closing your program after you sent data print any data out? Because it could of been a flush issue. But like I said I haven't popped it into eclipse and actually had a go at it.

While i have not performed that particular test, I would imagine it would not have any effect (unless i close the socket) as the StreamDecoder is still waiting to fill its buffer before continuing. But as i have said, the problem is now understood and I have a solution.

Thanks
2  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-06 22:13:57
Your latest impl interprets empty lines exactly the same as EOF. You might want to change that Pointing

heh thats what comes from just typing into a post with out really testing or thinking too long on the response. cheers for the heads up
3  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-06 06:00:26
Does switching to this style somehow fix your issue. I am unable to troubleshoot your issue really.

I have identified my problem (see earlier post is due the the underlying implementation of classes used by InputStreamReader. )

So unfortunately, your suggested change has no effect as it does not change the use of InputStreamReader.

I have accepted that I will have to roll-my-own Readline to achieve immediate response to messages, it is not difficult or risky to do so for my stated purpose and will most likely be as simple as below:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public String readLine()
{
   StringBuilder sb = new StringBuilder();
   int readByte = in.read();
   while (readByte>-1 && readByte!= '\n')
   {
      sb.append((char) readByte);
      readByte = in.read();
   }
   return sb.length()==0?null:sb.toString();
}

4  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-06 05:26:42
Thanks for the best practises advice. It always helps to get a reminder Smiley

These classes listed here are purely for the purposes of testing my original issue of (in my point of view) unexpected blocking on ReadLine() call and are not reflective of the quality of the final code Smiley.

Ultimately this will be a submission to create a King-of-the-Hill code golf question (similar to that of AlienWar ) where people will submit entries ( bots ) that will play a Tower-Defence game in a tournament to find the strongest bot.

Since submissions can be from any language, all communication will be performed via std in/out using only the lower 7bits of ASCII with integers and foating point numbers sent as text strings. Thus i will not need to be sending any binary (e.g. images etc)

Thanks for all your interest in my issue.
5  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-04 21:15:22
Thanks for spotting that issue.

That is not what caused the original problem (that side of the socket correctly worked Smiley ) but could be an issue in the future
6  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-04 09:55:19
Ok, I have investigated further by looking at the source code of the JRE and have found my problem: The InputStreamReader uses StreamDecoder internally... the implementation of which populates a buffer of either 32 bytes or 8192 bytes from the InputStream before return results back to the InputStreamReader and ultimately to the BufferedReader. It seems that in one direction the 32 byte buffer option is triggered and thus I see the results on that side. For the other side it must trigger the 8192 byte buffer and it blocks until that buffer is full. At which that side outputs the messages. (I found this out after accidentally leaving the test running for a while and coming back to see that it had worked!)

That is annoying for my purposes, but at least I now understand why it was behaving the way it did... I have no idea why it chose the smaller buffer on one side but the larger buffer on the otherside tho. Ah well... I will just have to role my own ReadLine routine.

Thanks for all that pondered on my question!
7  Game Development / Networking & Multiplayer / Re: BufferedReader.ReadLine unexpectedly blocking on: 2016-03-04 09:16:19
System.in
.read()
itself is blocking, not the bufferedReader.

one way to get around it is to use a dedicated thread, as you do already - and utilize
available()
:

only issue
in.read()
if
in.available() > 0
otherwise wait and try again.



I am not sure that is the complete problem... I am happy (want) the blocking to occur until new line character is read from the underlying inputstream used by the BufferedReader. Its just that it seems like it never detects it on the one side. The other side of the socket (see line 83-84 of BlockingInputStreamReaderTest code above) happily reads a line from a BufferedReader encapsulated InputStream.
8  Game Development / Networking & Multiplayer / [SOLVED] BufferedReader.ReadLine unexpectedly blocking on: 2016-03-04 06:02:21
It has been a while since I have had to ask upon the experts here, but I think i must be missing something very simple in understanding in what situation ReadLine() is blocking.

I have a controller that launches external classes (other jvm instances) and communicate via std in and out.

These external classes (processes) are not part of the project and are provided without source code.

For easy of development, i have implemented an option where, instead of an external processes, threads for each "process" are created and the Main method of each "process" is called. Since the "processes" expect communication via stdin/out I have swapped out the System.in and System.out streams with custom streams that instead uses sockets to pretend to be std in/out for each thread.

My problem is that whilst the controller can and does receive input from the child process, the child process "hangs" on the ReadLine() call indefinitely. If i do not use a BufferedReader, and instead simply use InputStream.read() then there is no blocking and the message is successfully received by the process.

*Note* that the  "threading" of the "external" processes does not have any effect... the same problem exists if the "process" is run in a seperate JVM.

Here is a cut down example to demonstrate:

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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;


public class BlockingInputStreamReaderTest {
   public static void main(String[] args) throws Exception
   {
      // use custom std streams to allow per thread stream redirection
      StandardStreamManager.stdIn=System.in;
      StandardStreamManager.stdOut=System.out;
      System.setIn(StandardStreamManager.getInputStreamRedirector());
      System.setOut(new PrintStream(StandardStreamManager.getOutputStreamRedirector()));
     
     
      final int port = 9876;
      final List<String> command = Arrays.asList("java TestPlayer".split(" "));
         Thread thread = new Thread(new Runnable()
         {
           
         @Override
         public void run() {
            ServerSocket ss=null;
            try
            {
               ss = new ServerSocket(port);
               Socket socket = ss.accept();
               StandardStreamManager.register(socket.getInputStream(),socket.getOutputStream());

               Class playerClass;
               playerClass = getClass().getClassLoader().loadClass(command.get(1));
                  String[] commands = command.subList(2, command.size()).toArray(new String[0]);
                  Method method = playerClass.getMethod("main", new String[commands.length].getClass());
                  Object[] args = new Object[1];
                  args[0] = commands;
                  method.invoke(null,args);
            } catch (Exception e)
            {
               e.printStackTrace();
            }
            finally
            {
               if (ss!=null)
               try {
                  ss.close();
               } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      });
         thread.start();
         
         PrintStream ps = null;
         BufferedReader br = null;
         int retries=0;
         while (retries++<200)
         {
            try
            {
               Socket socket = new Socket("127.0.0.1", port);
               ps = new PrintStream( socket.getOutputStream());
               br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
               break;
            } catch (IOException e)
            {
               try {
               Thread.sleep(10);
            } catch (InterruptedException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
            }
         }
         
         System.out.println("CONTROLLER INPUT RECEIVED: "+br.readLine());
         System.out.println("CONTROLLER INPUT RECEIVED: "+br.readLine());
         
         // write to player...
         while(true)
         {
            ps.println("You can do it!");
            Thread.sleep(500);
         }
   }
}


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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashMap;

public class StandardStreamManager
{
   static public PrintStream stdOut;
   static public InputStream stdIn;
   
   
   
   static private InputStream getInputStream()
   {
      Streams streams = registry.get(Thread.currentThread());
      if (streams == null)
      {
         return stdIn;
      }
      else
      {
         return streams.is;
      }
   }
   
   static private OutputStream getOutputStream()
   {
      Streams streams = registry.get(Thread.currentThread());
      if (streams == null)
      {
         return stdOut;
      }
      else
      {
         return streams.os;
      }
   }
   
   static private class Streams
   {
      public Streams(InputStream inputStreamRedirectWorker,
            OutputStream outputStreamRedirectWorker) {
         this.is=inputStreamRedirectWorker;
         this.os=outputStreamRedirectWorker;
      }
      InputStream is;
      OutputStream os;
   }
   static HashMap<Thread, Streams> registry = new HashMap<>();
   
   static public void register(InputStream is, OutputStream os)
   {
      registry.put(Thread.currentThread(), new Streams(is,os));
   }

   public static InputStream getInputStreamRedirector() {

      return new InputStream() {
         
         @Override
         public int read() throws IOException {
            InputStream is = getInputStream();
            return is.read();
         }
      };
   }

   public static OutputStream getOutputStreamRedirector() {

      return new OutputStream() {
         
         @Override
         public void write(int data) throws IOException {
            getOutputStream().write(data);
         }
      };
   }
   
   
}


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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestPlayer {

   public static void main(String[] args) throws Exception
   {
      Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
         
         @Override
         public void run() {
            System.err.println("TEST PLAYER SHUTDOWN!");
         }
      }));
     
      System.err.println("TEST PLAYER START!");

      InputStream in = System.in;

      // send something to the controller
      System.out.println("12345,E\n45678,F");
     
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      while (true)
      {

         String line = br.readLine();
         System.err.println("TestPlayer INPUT RECEIVED: "+line);
      }

//      String line="";
//      while (true)
//      {
//         line+=(char) in.read();
//         if (line.endsWith("\n"))
//         {
//            System.err.print("TestPlayer INPUT RECEIVED: "+line);
//            line="";
//         }
//      }
   }

}


To test that the messages are being sent correctly, uncomment the commented section in TestPlayer and comment the code block above it using the BufferedReader.

Can someone give some suggestions on why BufferedReader would be blocking? Thanks
9  Discussions / Miscellaneous Topics / Re: I'm baaaaack... on: 2015-05-21 03:52:51
Let flame this question, really I want know you’re opinion – what alternative you have? =)

You mentioned you enjoy programming... Then the trick, at least for me, is to find small well defined problems to solve. It can be anything really... Last weekend I programmed up a text viewer. The requirements I had were, had to be able to read and view 10gig log files, perform text searches, relative position searches, and responsive navigation of the text.

Alternatively I have participated in the programming competitions at http://codegolf.stackexchange.com/ I especially like the King-of-the-hill competitions.
10  Games Center / Contests / Re: Java Code Challenge on Stack Overflow on: 2015-03-06 11:33:25
Yeah, i do like to enter these competitions Smiley I have just created an entry.... seems to be a winner... for now Tongue
11  Game Development / Game Mechanics / Re: Represent 3D map on: 2015-01-16 22:25:28
This is beyond my level to help directly, however you might want to consider asking on the ompf2 forums... it is a forum for advanced real time ray tracers but they also have fantastic insight on how to structure data for 3d worlds.

Perhaps they can help you.
12  Game Development / Newbie & Debugging Questions / Re: Issue with changing screen res in windows aero = black screen. on: 2015-01-08 20:44:27
yes both did not work for me
13  Game Development / Newbie & Debugging Questions / Re: Issue with changing screen res in windows aero = black screen. on: 2015-01-08 12:39:05
I can confirm that it does not work for me (win 7) as well.

It is most definitely a product of how you are doing your rendering.... having a look at your code it seems that you are performing unnecessary off screen image updates when you can just use the  Frame's BufferStrategy to do that for you. It will most likely be the cause of the incompatibility with fullscreen mode.

I would also suggest moving the code that updates the image out side of the AlienSwarm Paint method and just update the BufferStrategy's graphics object and then call show() on the bufferstrategy once you have finished rendering a tick.
14  Games Center / nK Game Competition - 2015 / Re: Planning for Competition of 2015 on: 2015-01-02 23:25:11
I agree, applets are the most mature entry point for this competition
15  Games Center / Contests / Re: Java4K Competition 2015 on: 2015-01-02 23:20:03
well i am all for it: the accessibility to provided by signed applets would be a great boon
16  Games Center / nK Game Competition - 2015 / Re: Planning for Competition of 2015 on: 2015-01-02 13:09:45
@richard.pickering

Will there be a defined entry point? i.e. Applet or executable JAR? Webstart?
17  Games Center / nK Game Competition - 2015 / Re: Planning for Competition of 2015 on: 2015-01-02 13:06:52
Sure, I think I might be a little naive... but so far so good. And all the obstacles I have currently come across I have found solutions for.... At worst it will improve my understanding of java script.... and the jvm.

As you are probably aware, Understanding comes from solving problems. I did think about using existing libraries, however I think I will gain the knowledge to actually achieve this project by performing the parser work myself.

However that does not mean that once I have that knowledge I will not change to using libary such as ASM as you have suggested Smiley I am sure that their implementation is much more mature and tested than my parser could ever be!
18  Games Center / nK Game Competition - 2015 / Re: Planning for Competition of 2015 on: 2015-01-02 01:44:46
While it is not going to be ready any time soon... for prosperity's sake I am writing a java applet to java script converter.

This converter will be targeting applets as that is what most of the past J4K entries have been and will limit the scope of the project somewhat.

I believe that this is achievable due to the following:

1. Java 4k entries usually only use a limited subset of Swing
2. Java 4k entries do not use native methods.
3. Java 4k entries are usually single threaded and or limited threading
4. Java 4k entries by their nature usually have only limited dependencies to the Java API.
5. Once the core common Java4k entries components are implemented/functional, I will be able to expand the converter as necessary, piecemeal.
6. There is a lot of commonality between the core java and java script langages.
7. I have currently finished the first pass of a ClassFile parser and am now in development of creating a java byte code parser.
8. Modern Javascript does have the concept of WebWorkers that can manipulate referenced image data. This should allow me to render swing components to an image on a separate thread.
9. There are java script modules out there that can emulate Long integers in javascript (one of the main differences between java and javascript)

I have no idea how performant the resulting game will be but one can hope as the java script runtime machines get better the games will be playable in the future.


Note that my javascript programming experience and knowledge is not extensive. I believe that I am able to produce this converter... however and javascript performance tricks are beyond my knowledge.
19  Games Center / Contests / Re: Java4K Competition 2015 on: 2015-01-01 03:39:53
I am just being diplomatic Smiley I do not want to scare off the latest champion of the 4k cause.

I also support Riven's offering of help, I just do not want to Richard (who has been very keen and been keeping the debate going) from being slighted. Mainly for selfish reasons, i might add, as I would like to see him take the reigns: allowing him to get skills required to host the 4k comp and thus for him to get personally attached to it and reduce the risk of it fizzling out again Tongue
20  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-31 21:27:29
That sounds awesome Riven Smiley thanks for the offer. Richard is the defacto (most motivated Tongue ) organiser for this latest incarnation of the comp so I would wait to see if he is agreement first.
21  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-31 00:34:47
I suggest just something simple for this first time. i.e. use the "MY FILES" of this forum to allow people to submit entries.

Similarly host an automatic tool chain script + tools for use by people to use to perform the compression aspect.

Later on, a new website can be created and then the entries transferred.
22  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-15 01:07:10
Also, if we are using a common tool chain then the only way to enforce this is to have the submissions in source files + resources format. I would suggest that an automated submit, compile, compress, return output script be made available.
23  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-15 01:04:53
I would suggest expanding the byte code compiler rule such that other languages that target the JVM can be used. However that, similarly for Java language, only one defined version can be used.

Note that it is not important to me as I will be using java, only that there has been a couple of entries that used different languages that run on the JVM
24  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-09 05:43:12
Executable JARs do not really provide any more benefit over the Java4kLauncher it terms of accessibility for non-developers.

It would, however be easier to submit entries in the absence of the java4k.com website.
25  Game Development / Newbie & Debugging Questions / Re: Creating shapes n stuff out of the BufferedImage.. on: 2014-12-06 21:58:39
I am not entirely sure what you are asking...

If you are wanting to use standard shapes e.g. circle, rect etc then using the Graphics2D object from the BufferedImage is the easiest approach:
1  
2  
3  
4  
Graphics2D g2 = (Graphics2D) img.getGraphics();
g2.setColor(new Colour(pack(r,g,b)));
g2.fillOval(20,20,20,20);
g2.dispose();


if you are wanting custom shapes and effects then it will be per pixel manipulation:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      int imgWidth = img.getWidth();
      int imgHeight = img.getHeight();
      for (int y = 0; y < imgHeight; y++)
      {
         for (int x = 0; x < imgWidth; x++)
         {
            int temp = (x + y) % 255;
            pixels[y * imgWidth + x] = pack(255 - temp, temp, temp ^ 255);
         }
      }
      ImageIO.write(img, "png", new File("out.png"));
26  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-06 21:21:17
I am warming to the idea of a "code-golfed" competition where only the java source file is submitted.

my reasons:
1. It is in same spirit of the java4k competition, but different that might give freshness to the competition.
2. Does not require complex compression chains. However, it remains to be seen whether the effort to golf code is more than using the compression chains
3. Easy to store entries for prosperity. Java syntax and idioms are quite unchanging. i.e. future proof
4. If people are concerned about malicious code, they can analyse the submission before running it
27  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-06 02:49:31
Maybe I am not the best person to ask for that as I actually would applaud the use of tricks like that as long as they are made public for others to use.
28  Games Center / Contests / Re: Ludum Dare 31: "Entire Game on One Screen" on: 2014-12-06 02:47:14
be a little bit liberal with the interpretation.

e.g. perhaps a top down vertical shooter that takes place on an asian-style reed screen. So you can still scroll as the game is taking place "on One Screen" Tongue
29  Games Center / Contests / Re: Java4K Competition 2015 on: 2014-12-06 02:41:13
My Thoughts Smiley

1. I am a proponent of the current rules. But I enjoy the compression aspect... hunting for those extra bytes! No matter what is decided, there is a delivery problem: will have to be run outside of a browser... so why don't we leave the rules the same and use the Java4kLauncher ?

1. larger than 4k JARs start to remove the equalizer effect. That is, the difference between a 2 year java programmer and a 10+ year java programmer's entry is smaller when the allowed bytes are smaller. This means that more entries are able to compete effectively. 4096 bytes seems to be the sweet spot.

2. Accessibility. Moving from non-standard java libraries restricts the number of potential entrants as they will be required to learn a new library. see underwhelming response to the LWJGL16k competition.

3. One large source file means that you have to embed graphics into the source file meaning that you cannot lever off of standard and byte-cheap image reading methods. Not a big issue I guess. Out of all the options presented thus far this is one i would most likely support. It does have an added benefit of being somewhat future proof.

30  Games Center / Contests / Re: End of 4K. Thanks all! on: 2014-12-05 07:33:32
Well it could be just an informal one. Just create a thread and see if there is any interest.... I did plan on "finally" finishing my java 4k project of the last couple of years in this years competition... so I may be a competitor for you Tongue
Pages: [1] 2 3 ... 34
 
CopyableCougar4 (24 views)
2016-06-25 16:56:52

Hydroque (58 views)
2016-06-22 02:17:53

SwampChicken (61 views)
2016-06-20 13:22:57

SwampChicken (61 views)
2016-06-20 13:22:49

SwampChicken (57 views)
2016-06-20 13:22:26

Hydroque (100 views)
2016-06-15 08:22:50

Hydroque (93 views)
2016-06-13 06:40:55

DarkCart (216 views)
2016-05-29 02:30:33

Hydroque (175 views)
2016-05-26 14:45:46

Mac70 (176 views)
2016-05-24 21:16:33
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!