Re: [nox-dev] Receiving JSON messages

2012-01-29 Thread kk yap
Hi,

Reading the documentation might help.
http://noxrepo.org/~yapkke/doc/classvigil_1_1jsonmessenger.html#_details

Try have a type field.

Regards
KK

On 29 January 2012 07:34, Giorgio Mazza giorgio.mazza...@gmail.com wrote:
 A little update and a further question :)

 With destiny's messenger my component works, even adding jsonmessenger as a
 dependency. Also Jsonmsg_events are raised and handled without errors by my
 callback and I solved the dummy timer problem with the client disconnection.

 Now, I have a strange behaviour with my callback that seems to handle only
 messages that have the default syntax described in jsonmessenger.hh ( just
 above the definition of the jsonmessenger class).
 If I send a 'connect' or a 'disconnect' message it is handled by my
 callback, while, if I send a different message (that's what I want to do),
 jsonmessenger posts a JSONMsg_event, that it is simply ignored by my
 callback.

 That 's a snippet of my install function:

 from nox.coreapps.messenger.pyjsonmsgevent import JSONMsg_event
 JSONMsg_event.register_event_converter(self.ctxt)
 self.register_handler(JSONMsg_event.static_get_name(),
 self.json_message_callback)


 That's my (temporary) callback:

 def json_message_callback(self, e):
         import json
         global cache_server_table
         message = json.loads(e.jsonstring)
         cache_server_table.update(message)
         print message
         print cache_server_table
         e.reply(json.dumps({MSG:Welcome! I am the Controller}))
         if cache_server_table.has_key(MSG):
             print cache_server_table[MSG]
             if cache_server_table[MSG] == Connection setup:
                 cache_server_MAC = cache_server_table[MAC]
                 print cache_server_MAC
         return CONTINUE


 And that's what I get as output:

 00046|nox|INFO:nox bootstrap complete
 00047|messenger_core|DBG:Starting connection with idleInterval 0
 00048|jsonmessenger|DBG:JSON message of length 18 (connect)
 00049|jsonmessenger|DBG:JSON message of length 18
 00050|jsonmessenger|DBG:JSON: {type:connect}
 {u'type': u'connect'}
 --- the two print instruction
 {u'type': u'connect'}
 --- in the callback
 00051|messenger_core|DBG:Sent string of length 39 socket 0x8e536c0
 00052|messenger_core|DBG:TCP socket connection accepted
 00053|messenger_core|DBG:Copy 74 bytes to message
 00054|messenger_core|DBG:Received packet of length 74
 00055|jsonmessenger|DBG:JSON message of length 74
 00056|jsonmessenger|DBG:Message posted as JSONMsg_event
 - after that I would expect the
 output of my callback (the two print instruction)
 00057|jsonmessenger|DBG:JSON:
 {MAC:08:00:27:cc:77:1c,IP:10.0.10.2,MSG:Connection setup}
 00058|messenger_core|DBG:Copy 22 bytes to message
 00059|messenger_core|DBG:Received packet of length 22
 00060|jsonmessenger|DBG:JSON message of length 22
 00061|jsonmessenger|DBG:Message posted as JSONMsg_event

 00062|jsonmessenger|DBG:JSON: {type:disconnect}
 00063|jsonmessenger|DBG:Clear connection state for 0x8e536c0
 {u'type': u'disconnect'}
 {u'type': u'disconnect'}


 So, the question is: Why my callback does not handle events posted by
 jsonmessenger? How can I fix that? Is there any particular syntax I have to
 follow so that it is a json-related error?

 Thanks in advance.

 Regards,
 Giorgio



 On 27/01/2012 16:42, kk yap wrote:

 Hi Giorgio,

 Your client is disconnecting before the reply is sent.  If you look at
 nox-console.py, it should be a good example to follow.

 Regards
 KK

 On 27 January 2012 05:12, Kyriakos Zarifis kyr.zari...@gmail.com wrote:

 A JSONMsg_event is just another NOX event and us such it will either passed
 on to all components down the event handler chain or stopped by one of them.
 Your handler needs to return a valid NOX event disposition (
 http://noxrepo.org/noxwiki/index.php/Disposition  )
 So in your case you just need to add a return STOP and the error will
 disappear.


 As for the other comment, I'm not sure how messenger_core cleans the
 connections state/closes socket. It might very well be a timing issue, maybe
 the connection state hasn't been cleaned when the event is processed. I
 don't know if the log messages represent the reality 100%. In any case the
 last message is either never really sent or it's sent to the void. Either
 way I'd just ignore it, I doubt it will affect anything


 On Fri, Jan 27, 2012 at 3:02 AM, Giorgio Mazza giorgio.mazza...@gmail.com
 wrote:

 I tried this way:  I replaced zaku's default messenger folder with
 destiny's messenger one (nox/src/nox/coreapps/messenger), that I had
 previously downloaded and installed.
 Then I recompiled zaku and the import error disappeared, so that when I
 run
 ./nox_core -v -i ptcp:6633 jsonmessenger=tcpport=3334 my_component
 I do not get errors anymore and components are installed successfully.
 However, when I try to send a json message from my 

Re: [nox-dev] Receiving JSON messages

2012-01-29 Thread Giorgio Mazza

It was the type field, thanks!

On 29/01/2012 16:45, kk yap wrote:

Hi,

Reading the documentation might help.
http://noxrepo.org/~yapkke/doc/classvigil_1_1jsonmessenger.html#_details

Try have a type field.

Regards
KK

On 29 January 2012 07:34, Giorgio Mazzagiorgio.mazza...@gmail.com  wrote:

A little update and a further question :)

With destiny's messenger my component works, even adding jsonmessenger as a
dependency. Also Jsonmsg_events are raised and handled without errors by my
callback and I solved the dummy timer problem with the client disconnection.

Now, I have a strange behaviour with my callback that seems to handle only
messages that have the default syntax described in jsonmessenger.hh ( just
above the definition of the jsonmessenger class).
If I send a 'connect' or a 'disconnect' message it is handled by my
callback, while, if I send a different message (that's what I want to do),
jsonmessenger posts a JSONMsg_event, that it is simply ignored by my
callback.

That 's a snippet of my install function:

from nox.coreapps.messenger.pyjsonmsgevent import JSONMsg_event
JSONMsg_event.register_event_converter(self.ctxt)
self.register_handler(JSONMsg_event.static_get_name(),
self.json_message_callback)


That's my (temporary) callback:

def json_message_callback(self, e):
 import json
 global cache_server_table
 message = json.loads(e.jsonstring)
 cache_server_table.update(message)
 print message
 print cache_server_table
 e.reply(json.dumps({MSG:Welcome! I am the Controller}))
 if cache_server_table.has_key(MSG):
 print cache_server_table[MSG]
 if cache_server_table[MSG] == Connection setup:
 cache_server_MAC = cache_server_table[MAC]
 print cache_server_MAC
 return CONTINUE


And that's what I get as output:

00046|nox|INFO:nox bootstrap complete
00047|messenger_core|DBG:Starting connection with idleInterval 0
00048|jsonmessenger|DBG:JSON message of length 18 (connect)
00049|jsonmessenger|DBG:JSON message of length 18
00050|jsonmessenger|DBG:JSON: {type:connect}
{u'type': u'connect'}
--- the two print instruction
{u'type': u'connect'}
--- in the callback
00051|messenger_core|DBG:Sent string of length 39 socket 0x8e536c0
00052|messenger_core|DBG:TCP socket connection accepted
00053|messenger_core|DBG:Copy 74 bytes to message
00054|messenger_core|DBG:Received packet of length 74
00055|jsonmessenger|DBG:JSON message of length 74
00056|jsonmessenger|DBG:Message posted as JSONMsg_event
- after that I would expect the
output of my callback (the two print instruction)
00057|jsonmessenger|DBG:JSON:
{MAC:08:00:27:cc:77:1c,IP:10.0.10.2,MSG:Connection setup}
00058|messenger_core|DBG:Copy 22 bytes to message
00059|messenger_core|DBG:Received packet of length 22
00060|jsonmessenger|DBG:JSON message of length 22
00061|jsonmessenger|DBG:Message posted as JSONMsg_event

00062|jsonmessenger|DBG:JSON: {type:disconnect}
00063|jsonmessenger|DBG:Clear connection state for 0x8e536c0
{u'type': u'disconnect'}
{u'type': u'disconnect'}


So, the question is: Why my callback does not handle events posted by
jsonmessenger? How can I fix that? Is there any particular syntax I have to
follow so that it is a json-related error?

Thanks in advance.

Regards,
Giorgio



On 27/01/2012 16:42, kk yap wrote:

Hi Giorgio,

Your client is disconnecting before the reply is sent.  If you look at
nox-console.py, it should be a good example to follow.

Regards
KK

On 27 January 2012 05:12, Kyriakos Zarifiskyr.zari...@gmail.com  wrote:

A JSONMsg_event is just another NOX event and us such it will either passed
on to all components down the event handler chain or stopped by one of them.
Your handler needs to return a valid NOX event disposition (
http://noxrepo.org/noxwiki/index.php/Disposition  )
So in your case you just need to add a return STOP and the error will
disappear.


As for the other comment, I'm not sure how messenger_core cleans the
connections state/closes socket. It might very well be a timing issue, maybe
the connection state hasn't been cleaned when the event is processed. I
don't know if the log messages represent the reality 100%. In any case the
last message is either never really sent or it's sent to the void. Either
way I'd just ignore it, I doubt it will affect anything


On Fri, Jan 27, 2012 at 3:02 AM, Giorgio Mazzagiorgio.mazza...@gmail.com
wrote:

I tried this way:  I replaced zaku's default messenger folder with
destiny's messenger one (nox/src/nox/coreapps/messenger), that I had
previously downloaded and installed.
Then I recompiled zaku and the import error disappeared, so that when I
run
./nox_core -v -i ptcp:6633 jsonmessenger=tcpport=3334 my_component
I do not get errors anymore and components are installed successfully.
However, when I try to send a json message from my external 

Re: [nox-dev] Receiving JSON messages

2012-01-27 Thread kk yap
Hi Giorgio,

Your client is disconnecting before the reply is sent.  If you look at
nox-console.py, it should be a good example to follow.

Regards
KK

On 27 January 2012 05:12, Kyriakos Zarifis kyr.zari...@gmail.com wrote:
 A JSONMsg_event is just another NOX event and us such it will either passed
 on to all components down the event handler chain or stopped by one of them.
 Your handler needs to return a valid NOX event disposition (
 http://noxrepo.org/noxwiki/index.php/Disposition  )
 So in your case you just need to add a return STOP and the error will
 disappear.


 As for the other comment, I'm not sure how messenger_core cleans the
 connections state/closes socket. It might very well be a timing issue, maybe
 the connection state hasn't been cleaned when the event is processed. I
 don't know if the log messages represent the reality 100%. In any case the
 last message is either never really sent or it's sent to the void. Either
 way I'd just ignore it, I doubt it will affect anything


 On Fri, Jan 27, 2012 at 3:02 AM, Giorgio Mazza giorgio.mazza...@gmail.com
 wrote:

 I tried this way:  I replaced zaku's default messenger folder with
 destiny's messenger one (nox/src/nox/coreapps/messenger), that I had
 previously downloaded and installed.
 Then I recompiled zaku and the import error disappeared, so that when I
 run
 ./nox_core -v -i ptcp:6633 jsonmessenger=tcpport=3334 my_component
 I do not get errors anymore and components are installed successfully.
 However, when I try to send a json message from my external application I
 get a strange behaviour. I don't understand very well what is happening and
 why I get this error, so I do not know if it is my fault in doing something
 or if I need to hack something because the simple replacement of messenger
 folder is not enough.

 My callback is fairly simple for the moment and the only thing it does is
 to store the value of the received json message and to answer with a Hello
 world message, like that:
     def json_message_callback(self, e):
         import json
         global cache_server_table    #the global dict where I
 want to store received json information
         cache_server_table = json.loads(e.jsonstring)
         print cache_server_table

         e.reply(json.dumps({msg:Hello world}))


 This is my simple external application:

 import json
 import socket

 HOST = '10.0.10.1'
 PORT = 3334
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect((HOST, PORT))
 print Connected to server:  + str(s.getpeername())
 message={msg:CS-setup,mac:x,ip:10.0.10.2}
 json_message=json.dumps(message)
 s.send(json_message)
 print Sent JSON message
 data = s.recv(1024)
 print 'Received', repr(data)


 And this is what I get in the controller prompt, after executing the above
 code:

 00045|openflow|DBG:Passive tcp interface bound to port 6633
 00046|nox|INFO:nox bootstrap complete
 00047|messenger_core|DBG:Starting connection with idleInterval 0
 00048|jsonmessenger|DBG:JSON message of length 18 (connect)
 00049|jsonmessenger|DBG:JSON message of length 18
 00050|jsonmessenger|DBG:JSON: {type:connect}
 {u'type': u'connect'}
 00051|messenger_core|DBG:Sent string of length 22 socket 0x8b9ff48
 00052|pyrt|ERR:Python handler returned invalid Disposition.
                          No idea
 00053|messenger_core|DBG:TCP socket connection accepted
 00054|messenger_core|DBG:Copy 54 bytes to message
 00055|messenger_core|DBG:Received packet of length 54
 00056|jsonmessenger|DBG:JSON message of length 54
 00057|jsonmessenger|DBG:Message posted as JSONMsg_event
 00058|jsonmessenger|DBG:JSON:
 {mac:x,msg:CS-setup,ip:10.0.10.2}
 00059|jsonmessenger|DBG:JSON message of length 21 (disconnect)
 00060|jsonmessenger|DBG:JSON message of length 21
 00061|messenger_core|DBG:socket closed
 00062|jsonmessenger|DBG:JSON: {type:disconnect}
 00063|jsonmessenger|DBG:Clear connection state for 0x8b9ff48
 {u'type': u'disconnect'}
 00064|messenger_core|DBG:Sent string of length 22 socket
 0x8b9ff48   -- It seems that my
 callback tries to answer when the socket is already closed. Why?
 00065|pyrt|ERR:Python handler returned invalid Disposition.


 Any idea or suggestion will be appreciated.

 Regards,
 Giorgio



 On 26/01/2012 23:39, Kyriakos Zarifis wrote:

 That's the reason; the jsonmsg_event isn't exposed in python on zaku.
 I'd really encourage you to try to migrate to destiny. Not only because
 this will work, but more importantly because destiny is a far more developed
 branch by now, with many fixes and added features.

 If you really don't want to switch to destiny, maybe you could just grab
 jsonmsg_event.i from destiny and stick it in your zaku tree. (I can't
 remember if this is going to just work or you'll need to hack something, but
 give it a try and see what breaks?)

 On Thu, Jan 26, 2012 at 1:37 PM, Giorgio Mazza
 giorgio.mazza...@gmail.com wrote:

 Yes, I'm using 

Re: [nox-dev] Receiving JSON messages

2012-01-25 Thread Giorgio Mazza

A question about the socket opened when invoking jsonmessenger.
What are the IP address, the tcp port and the interface that this socket 
refers to? Is there any way to set them?
I undersotood the mechanism, but I don't know where to send my messages 
from the external application.

Thank you.
Regards,

Giorgio

On 24/01/2012 13:49, Murphy McCauley wrote:

The minimum to get up and going should be something like this:

1) In your component's install function:
from nox.coreapps.messenger.pymsgevent import JSONMsg_event
JSONMsg_event.register_event_converter(self.ctxt)
self.register_handler(JSONMsg_event.static_get_name(), myHandler)

2) Implement a handler:
def myHandler (e):
  import json
  print json.loads(e.jsonstring)
  e.reply(json.dumps({msg:Hello world}))

3) Include jsonmessenger on the commandline or as a dependency


That may not be exactly correct -- it's adapted from a quick writeup I 
did in December about using the new Python support for the regular 
messenger (as opposed to the JSON messenger), which has not yet been 
pushed to the repository.  For reference, that post was:

http://noxrepo.org/pipermail/nox-dev/2011-December/008382.html

(If using the new version of messenger that I linked to in that post, 
you remove the register_event_converter() call from step 1 and include 
pyjsonmessenger instead of jsonmessenger in step 3.)


Invoking the jsonmessenger component (on the commandline or by 
including it as a dependency in your app's meta.json) will create the 
server socket for you.


You absolutely do not have to use the messenger.py class.  I'm 
removing it from that directory, because all it ever does is confuse 
people -- it really doesn't belong there.  messenger.py is a library 
for writing JSON messenger *clients* (external programs) in Python. 
 That may be useful to you, but you don't need it for the NOX side of 
things.


Hope that helps.

-- Murphy

On Jan 24, 2012, at 4:12 AM, Giorgio Mazza wrote:


Thank you.
I try to sum up the operations I need to perform, to see if I 
understood correctly.
Basically in my external application I have to set up a socket that 
sends json messages and this would be quite simple.
In my nox component, instead, I have to import the JSONMsg_event 
and, within the install() instruction, to handle it with my 
specific method, that, in my case, would only save these json 
messages into a dictionary, for using them later, according to some 
conditions.

Is that correct?

A couple of things that I didn't understand:
- I assume I also have to set up a server socket in my nox component, 
in order to receive json messages and handle  JSONMsg_events. So, I 
think this socket has to be already up and running when I handle the 
event. So, when do I have to create it and how? Do I have to use 
messenger.py channel class?
- Second question, probably related to the first. I think to be 
pretty confused about jsonmessenger: what are the jsonmessenger files 
I could look into in order to understand fields and methods that I 
would need to use? Are the jsonmessenger.cc and jsonmessenger.hh in 
nox/src/nox/coreapps/messenger? And, if it is the case, how can I 
integrate them into a python component?


Thanks again,

Giorgio

On 24/01/2012 12:28, Kyriakos Zarifis wrote:

Hi Giorgio,

yes, I think using jsonmessenger would be the best approach for this.

you need to implement a send/receive interface on the external 
application and in your nox component. For the external application, 
it's pretty straightforward - Connect to the jsonmessenger socket 
and send json strings. In your nox application you need to register 
for JSON messages, and handle them appropriately.


The wiki explains the communication in a few steps (specifically for 
the GUI-NOX, but it will be similar and simpler for any external 
app) here 
http://noxrepo.org/noxwiki/index.php/NOX_GUI#Connecting_a_subview_to_a_NOX_component:


If you want to see a full example, the GUI 
http://tinyurl.com/6p2yl5o and the monitoring 
http://tinyurl.com/6nv83a3 component in destiny could be a place 
to look. I'm afraid it's much more complex than what you need, but 
the bits you need are in there if you dig in the code a bit.



On Tue, Jan 24, 2012 at 2:16 AM, Giorgio Mazza 
giorgio.mazza...@gmail.com mailto:giorgio.mazza...@gmail.com wrote:


Hi all!
I have written a simple component in python that works fine.
Now I would to improve it, making it to install flow entries
depending on parameters received from an external application.
In particular I want to pass those parameters via json messages
to my component, which, in my thougths, has to open a
permanent socket listening for them, save those parameters in
a dictionary and, as a consequence, decide the desired switch
behaviour (whether install or not a flow entry for the received
parameters).
In previous threads I found that I have to use jsonmessenger
(even in python?) or to have a look to discovery.py, but 

Re: [nox-dev] Receiving JSON messages

2012-01-25 Thread Murphy McCauley
I believe it defaults to port 2703.  You should be able to set the port number 
by specifying it on the commandline...
./nox_core -i ptcp: jsonmessenger=tcpport=4096 your_app_here

It listens on all IP addresses; there is currently no way to specify just one.

-- Murphy

On Jan 25, 2012, at 1:11 PM, Giorgio Mazza wrote:

 A question about the socket opened when invoking jsonmessenger.
 What are the IP address, the tcp port and the interface that this socket 
 refers to? Is there any way to set them?
 I undersotood the mechanism, but I don't know where to send my messages from 
 the external application.
 Thank you.
 Regards,
 
 Giorgio
 
 On 24/01/2012 13:49, Murphy McCauley wrote:
 
 The minimum to get up and going should be something like this:
 
 1) In your component's install function:
 from nox.coreapps.messenger.pymsgevent import JSONMsg_event
 JSONMsg_event.register_event_converter(self.ctxt)
 self.register_handler(JSONMsg_event.static_get_name(), myHandler)
 
 2) Implement a handler:
 def myHandler (e):
   import json
   print json.loads(e.jsonstring)
   e.reply(json.dumps({msg:Hello world}))
 
 3) Include jsonmessenger on the commandline or as a dependency
 
 
 That may not be exactly correct -- it's adapted from a quick writeup I did 
 in December about using the new Python support for the regular messenger 
 (as opposed to the JSON messenger), which has not yet been pushed to the 
 repository.  For reference, that post was:
 http://noxrepo.org/pipermail/nox-dev/2011-December/008382.html
 
 (If using the new version of messenger that I linked to in that post, you 
 remove the register_event_converter() call from step 1 and include 
 pyjsonmessenger instead of jsonmessenger in step 3.)
 
 Invoking the jsonmessenger component (on the commandline or by including it 
 as a dependency in your app's meta.json) will create the server socket for 
 you.
 
 You absolutely do not have to use the messenger.py class.  I'm removing it 
 from that directory, because all it ever does is confuse people -- it really 
 doesn't belong there.  messenger.py is a library for writing JSON messenger 
 *clients* (external programs) in Python.  That may be useful to you, but you 
 don't need it for the NOX side of things.
 
 Hope that helps.
 
 -- Murphy
 
 On Jan 24, 2012, at 4:12 AM, Giorgio Mazza wrote:
 
 Thank you.
 I try to sum up the operations I need to perform, to see if I understood 
 correctly.
 Basically in my external application I have to set up a socket that sends 
 json messages and this would be quite simple.
 In my nox component, instead, I have to import the JSONMsg_event and, 
 within the install() instruction, to handle it with my specific method, 
 that, in my case, would only save these json messages into a dictionary, 
 for using them later, according to some conditions.
 Is that correct?
 
 A couple of things that I didn't understand:
 - I assume I also have to set up a server socket in my nox component, in 
 order to receive json messages and handle  JSONMsg_events. So, I think this 
 socket has to be already up and running when I handle the event. So, when 
 do I have to create it and how? Do I have to use messenger.py channel class?
 - Second question, probably related to the first. I think to be pretty 
 confused about jsonmessenger: what are the jsonmessenger files I could look 
 into in order to understand fields and methods that I would need to use? 
 Are the jsonmessenger.cc and jsonmessenger.hh in 
 nox/src/nox/coreapps/messenger? And, if it is the case, how can I integrate 
 them into a python component?
 
 Thanks again,
 
 Giorgio
 
 On 24/01/2012 12:28, Kyriakos Zarifis wrote:
 
 Hi Giorgio,
 
 yes, I think using jsonmessenger would be the best approach for this.
 
 you need to implement a send/receive interface on the external application 
 and in your nox component. For the external application, it's pretty 
 straightforward - Connect to the jsonmessenger socket and send json 
 strings. In your nox application you need to register for JSON messages, 
 and handle them appropriately. 
 
 The wiki explains the communication in a few steps (specifically for the 
 GUI-NOX, but it will be similar and simpler for any external app) here:
 
 If you want to see a full example, the GUI and the monitoring component in 
 destiny could be a place to look. I'm afraid it's much more complex than 
 what you need, but the bits you need are in there if you dig in the code a 
 bit.
 
 
 On Tue, Jan 24, 2012 at 2:16 AM, Giorgio Mazza 
 giorgio.mazza...@gmail.com wrote:
 Hi all!
 I have written a simple component in python that works fine.
 Now I would to improve it, making it to install flow entries depending on 
 parameters received from an external application.
 In particular I want to pass those parameters via json messages to my 
 component, which, in my thougths, has to open a permanent socket 
 listening for them, save those parameters in a dictionary and, as a 
 consequence, decide the desired 

Re: [nox-dev] Receiving JSON messages

2012-01-24 Thread Giorgio Mazza

Thank you.
I try to sum up the operations I need to perform, to see if I understood 
correctly.
Basically in my external application I have to set up a socket that 
sends json messages and this would be quite simple.
In my nox component, instead, I have to import the JSONMsg_event and, 
within the install() instruction, to handle it with my specific 
method, that, in my case, would only save these json messages into a 
dictionary, for using them later, according to some conditions.

Is that correct?

A couple of things that I didn't understand:
- I assume I also have to set up a server socket in my nox component, in 
order to receive json messages and handle  JSONMsg_events. So, I think 
this socket has to be already up and running when I handle the event. 
So, when do I have to create it and how? Do I have to use messenger.py 
channel class?
- Second question, probably related to the first. I think to be pretty 
confused about jsonmessenger: what are the jsonmessenger files I could 
look into in order to understand fields and methods that I would need to 
use? Are the jsonmessenger.cc and jsonmessenger.hh in 
nox/src/nox/coreapps/messenger? And, if it is the case, how can I 
integrate them into a python component?


Thanks again,

Giorgio

On 24/01/2012 12:28, Kyriakos Zarifis wrote:

Hi Giorgio,

yes, I think using jsonmessenger would be the best approach for this.

you need to implement a send/receive interface on the external 
application and in your nox component. For the external application, 
it's pretty straightforward - Connect to the jsonmessenger socket and 
send json strings. In your nox application you need to register for 
JSON messages, and handle them appropriately.


The wiki explains the communication in a few steps (specifically for 
the GUI-NOX, but it will be similar and simpler for any external 
app) here 
http://noxrepo.org/noxwiki/index.php/NOX_GUI#Connecting_a_subview_to_a_NOX_component:


If you want to see a full example, the GUI 
http://tinyurl.com/6p2yl5o and the monitoring 
http://tinyurl.com/6nv83a3 component in destiny could be a place to 
look. I'm afraid it's much more complex than what you need, but the 
bits you need are in there if you dig in the code a bit.



On Tue, Jan 24, 2012 at 2:16 AM, Giorgio Mazza 
giorgio.mazza...@gmail.com mailto:giorgio.mazza...@gmail.com wrote:


Hi all!
I have written a simple component in python that works fine.
Now I would to improve it, making it to install flow entries
depending on parameters received from an external application.
In particular I want to pass those parameters via json messages to
my component, which, in my thougths, has to open a permanent
socket listening for them, save those parameters in a dictionary
and, as a consequence, decide the desired switch behaviour
(whether install or not a flow entry for the received parameters).
In previous threads I found that I have to use jsonmessenger (even
in python?) or to have a look to discovery.py, but I am not sure
to have understood what I have to do and where in order to realize
such a behaviour.
Could anyone, please, help me?
Thank you in advance,

Giorgio Mazza
___
nox-dev mailing list
nox-dev@noxrepo.org mailto:nox-dev@noxrepo.org
http://noxrepo.org/mailman/listinfo/nox-dev




___
nox-dev mailing list
nox-dev@noxrepo.org
http://noxrepo.org/mailman/listinfo/nox-dev


Re: [nox-dev] Receiving JSON messages

2012-01-24 Thread Giorgio Mazza

Ok, thanks.
Now things are more clear in my mind.
I'll try and let you know if I succeed. Otherwise I would bother you 
again :)

Regards,

Giorgio

On 24/01/2012 13:49, Murphy McCauley wrote:

The minimum to get up and going should be something like this:

1) In your component's install function:
from nox.coreapps.messenger.pymsgevent import JSONMsg_event
JSONMsg_event.register_event_converter(self.ctxt)
self.register_handler(JSONMsg_event.static_get_name(), myHandler)

2) Implement a handler:
def myHandler (e):
  import json
  print json.loads(e.jsonstring)
  e.reply(json.dumps({msg:Hello world}))

3) Include jsonmessenger on the commandline or as a dependency


That may not be exactly correct -- it's adapted from a quick writeup I 
did in December about using the new Python support for the regular 
messenger (as opposed to the JSON messenger), which has not yet been 
pushed to the repository.  For reference, that post was:

http://noxrepo.org/pipermail/nox-dev/2011-December/008382.html

(If using the new version of messenger that I linked to in that post, 
you remove the register_event_converter() call from step 1 and include 
pyjsonmessenger instead of jsonmessenger in step 3.)


Invoking the jsonmessenger component (on the commandline or by 
including it as a dependency in your app's meta.json) will create the 
server socket for you.


You absolutely do not have to use the messenger.py class.  I'm 
removing it from that directory, because all it ever does is confuse 
people -- it really doesn't belong there.  messenger.py is a library 
for writing JSON messenger *clients* (external programs) in Python. 
 That may be useful to you, but you don't need it for the NOX side of 
things.


Hope that helps.

-- Murphy

On Jan 24, 2012, at 4:12 AM, Giorgio Mazza wrote:


Thank you.
I try to sum up the operations I need to perform, to see if I 
understood correctly.
Basically in my external application I have to set up a socket that 
sends json messages and this would be quite simple.
In my nox component, instead, I have to import the JSONMsg_event 
and, within the install() instruction, to handle it with my 
specific method, that, in my case, would only save these json 
messages into a dictionary, for using them later, according to some 
conditions.

Is that correct?

A couple of things that I didn't understand:
- I assume I also have to set up a server socket in my nox component, 
in order to receive json messages and handle  JSONMsg_events. So, I 
think this socket has to be already up and running when I handle the 
event. So, when do I have to create it and how? Do I have to use 
messenger.py channel class?
- Second question, probably related to the first. I think to be 
pretty confused about jsonmessenger: what are the jsonmessenger files 
I could look into in order to understand fields and methods that I 
would need to use? Are the jsonmessenger.cc and jsonmessenger.hh in 
nox/src/nox/coreapps/messenger? And, if it is the case, how can I 
integrate them into a python component?


Thanks again,

Giorgio

On 24/01/2012 12:28, Kyriakos Zarifis wrote:

Hi Giorgio,

yes, I think using jsonmessenger would be the best approach for this.

you need to implement a send/receive interface on the external 
application and in your nox component. For the external application, 
it's pretty straightforward - Connect to the jsonmessenger socket 
and send json strings. In your nox application you need to register 
for JSON messages, and handle them appropriately.


The wiki explains the communication in a few steps (specifically for 
the GUI-NOX, but it will be similar and simpler for any external 
app) here 
http://noxrepo.org/noxwiki/index.php/NOX_GUI#Connecting_a_subview_to_a_NOX_component:


If you want to see a full example, the GUI 
http://tinyurl.com/6p2yl5o and the monitoring 
http://tinyurl.com/6nv83a3 component in destiny could be a place 
to look. I'm afraid it's much more complex than what you need, but 
the bits you need are in there if you dig in the code a bit.



On Tue, Jan 24, 2012 at 2:16 AM, Giorgio Mazza 
giorgio.mazza...@gmail.com mailto:giorgio.mazza...@gmail.com wrote:


Hi all!
I have written a simple component in python that works fine.
Now I would to improve it, making it to install flow entries
depending on parameters received from an external application.
In particular I want to pass those parameters via json messages
to my component, which, in my thougths, has to open a
permanent socket listening for them, save those parameters in
a dictionary and, as a consequence, decide the desired switch
behaviour (whether install or not a flow entry for the received
parameters).
In previous threads I found that I have to use jsonmessenger
(even in python?) or to have a look to discovery.py, but I am
not sure to have understood what I have to do and where in order
to realize such a behaviour.
Could anyone, please, help me?
Thank you in