Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

MVC pattern problem  RSS feed

Matteo Giuberti
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I really don't know where should i put this topic so i posted here.

I am developing a software that relies on communication between some device with radio antenna and uses a graphical interface.
I really don't want to annoy you explaining every aspect of my project so I'll try to simplifying it.

Let's assume device A and B can communicate reciprocally through radio antenna.
Device B is put somewhere in a room and continues sending packets to A with a simple send() through a thread which implements an infinite while loop.In every packet sent A writes a random number.
On the other side device A is attached to a PC and needs to receive ( receive() ) packets from B, to read the number contained in every packet and to generate some stats (i.e. mean and variance of the sample received).
Everytime A receives a packet it should update the stats that are shown through a GUI with some x-y graphs (x number of samples considered for the stat, y mean or variance).

Obviously this is an abstraction of what i need to do. my program is more complex and I want to try using an MVC pattern (Model-View-Controller) in order to generalize it for future modifications.
I'm not that expert in using this pattern but i read here and there something and tried to implement it.
In my MODEL I collect all my data structures.
In my VIEW I put some JPanel to plot the graphs and maybe some button to clear them.
In my CONTROLLER I implemented the Listeners for the View buttons and some methods to compute mean and variance from the data in the Model class.
Controller has reference to View and Model.
View only to the Model.
Model has no reference at all.
This is just like the pattern should be realized.

NOw the problematic part: I need to create an infinite loop that remains on receive() to receive every packets from device B and I thought about a thread doing this.
Where should i put the creation of the thread??in the model or in the controller?? in general, where do i put the flow of my program??

now when i receive a packet in this thread and read the value in it what should i do??which methods should i call to update real time my View??

I read somewhere that Threads should be put in the Model and the controllers must contains only the interactions between view and model, but how can i communicate to my controller, in this case, that my stats are updated and must be sent to the view??

Thought about wait and could be right??I haven't used them so far and i'm not so expert..i read also about java callbacks..are they useful in my context???

Hope that someone help me!!
thanks in advance!
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!