Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

buffering algorithm

Posted on 2013-12-06
2
Medium Priority
?
413 Views
Last Modified: 2013-12-07
Hi Experts,
I have been struggling for days now how to solve this problem  and couldn't get it right.
Here is the problem:
I receive startPlay command | stopPlay command from the User Interface layer
every command contains a code so e.g.
at 10.33 I receive startPlay (#1)
after 100 ms I receive stopPlay()
I calculate the duration = diff between start time and stop time and send that to network using some protocol.
Then I receive second command and third command etc...
in order to have those commands working at the network side, the duration should be at least 85 ms and the spacing between command e.g command1 and command2 needs to be 45 ms.
There is some delay at the UI layer ( that I cannot control), so sometimes I receive the start command and stop command too short (less than 85 ms), sometimes the gap between command1 and command 2 is less than 45 sec.
I tried to write a buffering algorithm to solve that delay so I can raise the duration to 85 ms if it is less than that and same for the time between stopping first command and starting  the next one.
As of yet I could n't get it working right and came here to seek your advice how to approach this problem.
I thought about having two queues one for the start command and one for the stop command
and a Timer when it kicks check the first queue and run the command then after some time check the second queue and run the stop command but I'm still not clear how to solve it right.
I'm pretty sure this kind of problems was already solved.
0
Comment
Question by:bachra04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 39703899
Something like this might do it for you?

private ExecutorService m_Executor = Executors.newFixedThreadPool(1) ;

// For a start
m_Executor.execute(new Runnable() {			
	public void run() {
              long startTime = System.currentTimeMillis() ;
              startPlay() ;
              long elapsed = System.currentTimeMillis() - startTime ;
              long timeToSleepMs = Math.Max(85-elapsed, 0) ;
              sleep(timeToSleepMs) ;
	}
}) ;

// For a stop
m_Executor.execute(new Runnable() {			
	public void run() {
            stopPlay() ;
	}
}) ;

Open in new window


The idea is to handle the start and stops on a separate thread.  So you can add a sleep() into the start logic making sure the start call takes at least 85 ms.
As the stop is executed on the same thread, it will wait for the first sleep to finish before executing.

However, your thread can continue to receive these and add them to the executor as quickly as you like - they'll just build up and execute in order for you.  So you don't block the UI as it calls to you.

Any help?

Doug
0
 
LVL 2

Author Closing Comment

by:bachra04
ID: 39703947
Now I just need to code it in C/C++ with pthread.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question