Re: Controlling cocos from web interface (help w/ events, threading, etc.)

27 views
Skip to first unread message

claudio canepa

unread,
Oct 23, 2012, 8:29:41 PM10/23/12
to cocos-...@googlegroups.com


On Tue, Oct 23, 2012 at 8:00 PM, Blaine <fri...@gmail.com> wrote:
Hi guys.

I'd like to make a simple web interface to controlling a cocos app. I'm having a hard time figuring out how to handle events and threading. I'm using the director interface and it's working perfectly on its own!

Let's say that at a basic level I'd like to be able to "emit a signal" that can be captured by my app (next scene, previous scene, etc.). Is there a good way to handle this without hacking the director interface? I've already tried sharing instances between threads (for the purpose of calling methods directly) but this doesn't work. Would I be able to register events and generate them from another thread that would be captured by the cocos app? Ideally I'd like to use Flask for the API but since it has its won blocking .run() call I'd need it to be in its own thread.

What do you guys think? Thanks! 
Blaine
--
 

Have a look to this thread:

It helps ?

 

Blaine

unread,
Oct 23, 2012, 8:47:12 PM10/23/12
to cocos-...@googlegroups.com
Thanks. Getting close! I figured out how to actually talk with the main thread, but the framebuffer refuses to update. I made a new issue. Any ideas?

Maybe I'm doing inter-process communication incorrectly, but at least I am succeeding in getting messages to get passed to the right instance. Now it's a matter of getting pyglet/cocos to update the framebuffer. At least I think that's the case.

claudio canepa

unread,
Oct 23, 2012, 10:05:38 PM10/23/12
to cocos-...@googlegroups.com
On Tue, Oct 23, 2012 at 9:47 PM, Blaine <fri...@gmail.com> wrote:
Thanks. Getting close! I figured out how to actually talk with the main thread, but the framebuffer refuses to update. I made a new issue. Any ideas?

Maybe I'm doing inter-process communication incorrectly, but at least I am succeeding in getting messages to get passed to the right instance. Now it's a matter of getting pyglet/cocos to update the framebuffer. At least I think that's the case.



I'm unclear about your design.
You want a browser (or other app) to communicate trough html with a cocos + flask app ?
 
And your problem is the communication between cocos and flask ?

For the last, lets check some basics:
   The gui thread (the one where the pyglet window is created) should be the main thread, ie the only one that exists when the process starts. This condition is fulfilled in your app ?

   Be sure the communication between the flask thread and the main thread is non blocking.

   The flask operation (facing the client) is non blocking ? I know nothing about flask.

   How many messages per seconds are pumped to cocos ?

   Anything special about self.next_scene ?

Finally, if you can present a small, runnable sample it would help a lot.
 


Blaine

unread,
Oct 24, 2012, 12:42:51 PM10/24/12
to cocos-...@googlegroups.com
Thank you. I actually figured out how to do non-threading with flask and gevent so I'm exploring that route. I do indeed have cocos in the main thread (learned that very quickly). I'll post the full solution here if I can get it working.

The idea is to use HTTP to control cocos (no html needed).  So if I browse to localhost:5000/next/, flask picks up on that, issues a transition to the next scene via the director interface, and all is good. Unfortunately the main problem with my original solution is that the openGL window didn't update.

The main problem here (with the different threads) is that my Cocos window / Pyglet Window also does not update if I move a window in front of it. That may be a problem with my window manager from what I've read? Im on Mint Linux, based on Ubuntu 12.04 (I think). Does anyone else have a problem with the window not redrawing when it gets covered by another window? I'm even manually calling window.on_expose() but it isn't working.

claudio canepa

unread,
Oct 24, 2012, 2:32:39 PM10/24/12
to cocos-...@googlegroups.com
On Wed, Oct 24, 2012 at 1:42 PM, Blaine <fri...@gmail.com> wrote:
Thank you. I actually figured out how to do non-threading with flask and gevent so I'm exploring that route. I do indeed have cocos in the main thread (learned that very quickly). I'll post the full solution here if I can get it working.

The idea is to use HTTP to control cocos (no html needed).  So if I browse to localhost:5000/next/, flask picks up on that, issues a transition to the next scene via the director interface, and all is good. Unfortunately the main problem with my original solution is that the openGL window didn't update.

The main problem here (with the different threads) is that my Cocos window / Pyglet Window also does not update if I move a window in front of it.

Wait a minute. Theres issue 562 in pyglet [0] about this.
Could you try to add self.schedule(lambda dt: None) in the scene on_enter ? 
That should stop the non redraw with overlapping windows.
 
That may be a problem with my window manager from what I've read? Im on Mint Linux, based on Ubuntu 12.04 (I think). Does anyone else have a problem with the window not redrawing when it gets covered by another window? I'm even manually calling window.on_expose() but it isn't working.


Blaine

unread,
Oct 24, 2012, 2:51:08 PM10/24/12
to cocos-...@googlegroups.com
Strange enough, when I removed the call to the external web thread, I no longer have this problem. So note to pyglet devs: creating new threads apparently makes this issue pop up (not re-drawing the window).

Anyway, I've got it working! I used gevent to create a non-blocking WSGI server. Then I call the WSGIServer()._stop_event.wait(timeout) with a very low timeout (0.0001) to activate the event polling. I schedule this callback with pyglet.clock every tick, and now I have only one thread total that controls both the cocos director and the flask-based gevent HTTP API server. Browsing to localhost:5000/next/ dispatches the next_slide event and I'm good to go!

It's a clean and elegant solution, and I'm happy to be able to use gevent for the first time. It's a beautiful little framework.

If anyone is interested I can explain further with more time. I'd like to write up something on how I did it since I don't seem to find any resources online.

Blaine


--
You received this message because you are subscribed to the Google Groups "cocos2d discuss" group.
To post to this group, send email to cocos-...@googlegroups.com.
To unsubscribe from this group, send email to cocos-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocos-discuss?hl=en.

Reply all
Reply to author
Forward
0 new messages