Skip to content

Commit

Permalink
[#18] Fixed race condition in Node and made the requestposition in vi…
Browse files Browse the repository at this point in the history
…ew more visible
  • Loading branch information
Saetch committed Jun 4, 2024
1 parent 64ce17c commit 1e3f10b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
9 changes: 4 additions & 5 deletions Cluster/Node_cs/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Text.Json.Serialization;
Expand All @@ -9,7 +10,7 @@ namespace Node_cs
public class ApiConfig
{

public Dictionary<Tuple<int, int>, double> savedValues = new Dictionary<Tuple<int, int>, double>();
public ConcurrentDictionary<Tuple<int, int>, double> savedValues = new ConcurrentDictionary<Tuple<int, int>, double>();
public String BICUBIC_INTERPOLATION_SERVICE_URL = "http://bicubic_interpolation_service:8080/calculate";
public String hostname = Environment.GetEnvironmentVariable("HOSTNAME");
public String COORDINATOR_SERVICE_URL = "coordinator";
Expand Down Expand Up @@ -147,10 +148,8 @@ private void registerRequests(WebApplication app)
try {
Console.WriteLine("Received addValue-call with params: " + toAdd.x + " " + toAdd.y + " " + toAdd.value);
var key = new Tuple<int, int>(toAdd.x, toAdd.y);
if (savedValues.ContainsKey(new Tuple<int, int>(toAdd.x, toAdd.y))){
if (!savedValues.TryAdd(key, toAdd.value)){
savedValues[key] = toAdd.value;
}else{
savedValues.Add(key, toAdd.value);
}

return Results.Ok("Value added: " + toAdd.x + " " + toAdd.y + " " + toAdd.value);
Expand Down Expand Up @@ -222,7 +221,7 @@ private void DealWithResponse(HttpResponseMessage response){
int x = Int32.Parse(valueParts[0].Split(":")[1]);
int y = Int32.Parse(valueParts[1].Split(":")[1]);
double value = Double.Parse(valueParts[2].Replace("}","").Replace("]","").Split(":")[1]);
savedValues.Add(new Tuple<int, int>(x,y), value);
savedValues.TryAdd(new Tuple<int, int>(x, y), value);
}

Console.WriteLine("Finished computing the response from coordinator service ... ");
Expand Down
4 changes: 2 additions & 2 deletions Cluster/Node_cs/NodeBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,11 @@ internal static async Task<List<XYValues>> DeleteSavedValuesBelow(string hash, A
}
var tuple = new Tuple<int, int>(pointHash.x, pointHash.y);
var ret = api.savedValues.ContainsKey(tuple);
if(!ret) throw new Exception("Tried removing nonexistent value");
if (!ret) throw new Exception("Tried removing nonexistent value");

double outp;
api.savedValues.TryGetValue(new Tuple<int, int>(pointHash.x, pointHash.y), out outp);
var removed = api.savedValues.Remove(new Tuple<int, int>(pointHash.x, pointHash.y));
var removed = api.savedValues.TryRemove(new KeyValuePair<Tuple<int, int>, double>(new Tuple<int, int>(pointHash.x, pointHash.y), outp));
if (!removed) throw new Exception("Failed to remove value");
resultXYValues.Add(new XYValues { x = pointHash.x, y = pointHash.y, value = outp });

Expand Down
36 changes: 27 additions & 9 deletions Cluster/vueWebUI/src/components/SVGMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<circle v-for="corner in corners" :key="corner[0]" :cx="corner[1].x" :cy="corner[1].y" r="7" fill="white" />
<!-- Draw edges -->
<line v-for="edge in edges" :key="edge[0]" :x1="edge[1].x1" :y1="edge[1].y1" :x2="edge[1].x2" :y2="edge[1].y2" stroke="white" />
<circle key="currentRequest" :cx="currentRequestPositionInView.x" :cy="currentRequestPositionInView.y" r="6" fill="green"/>
<circle key="currentRequest" :cx="currentRequestPositionInView.x" :cy="currentRequestPositionInView.y" :r=radius fill="green"/>
</svg>
</div>

Expand Down Expand Up @@ -54,7 +54,8 @@ export default {
dist_between_corners: 100,
translateX: 0,
translateY: 0,
directions: {up: false, down: false, left: false, right: false}
directions: {up: false, down: false, left: false, right: false},
radius: 6,
};
},
async mounted() {
Expand All @@ -73,7 +74,7 @@ export default {
this.position.x = window.innerWidth / 2 - this.svgWidth / 2;
this.position.y = window.innerHeight / 2 - this.svgHeight / 2;
this.requestNewData();
setInterval(this.MoveRequest, 30);
setInterval(this.MoveRequest, 1000/27);
},
unmounted() {
},
Expand Down Expand Up @@ -152,19 +153,32 @@ export default {
if (xCeil != this.currentPosCeil.x || yCeil != this.currentPosCeil.y) {
this.currentPosCeil.x = xCeil;
this.currentPosCeil.y = yCeil;
console.log("Passed a border!");
if(newData){
await this.requestNewCeilData();
if(newData ){
if(!this.hasNeededCorners(xCeil, yCeil)){
this.requestNewCeilData();
}else {
this.requestNewData();
}
}
} else {
if(newData){
await this.requestNewData();
this.requestNewData();
}
}
this.currentRequestPositionInView.x = this.corners.get("0/0").x + this.dist_between_corners * this.currentRequestPosition.x;
this.currentRequestPositionInView.y = this.corners.get("0/0").y - this.dist_between_corners * this.currentRequestPosition.y;
},
hasNeededCorners(x, y){
for( let i = x - 2; i <= x + 2; i++){
for( let j = y - 2; j <= y + 2; j++){
if(!this.corners.has(i+'/'+j)){
return false;
}
}
}
return true;
},
async requestNewData(){
const response = await fetch(this.informationUrl + "/getValue/" + this.currentRequestPosition.x + "/" + this.currentRequestPosition.y);
const data = await response.json();
Expand All @@ -173,7 +187,11 @@ export default {
async requestNewCeilData(){
const response = await fetch(this.informationUrl + "/getValueAutoInc/" + this.currentRequestPosition.x + "/" + this.currentRequestPosition.y+"/3");
const data = await response.json();
console.log(data);
this.zValue = data.value.value;
data.addedCorners.forEach(corner => {
this.addCorner(corner.x, corner.y);
this.addEdges(corner.x, corner.y);
});
},
startDrag(event) {
this.isDragging = true;
Expand Down Expand Up @@ -219,7 +237,7 @@ export default {
this.scale = newScale;
this.position.x -= newXDistFromCenter - xDistFromCenter;
this.position.y -= newYDistFromCenter - yDistFromCenter;
this.radius = 6 / newScale;
},
onMouseOver() {
if (!this.isDragging) {
Expand Down

0 comments on commit 1e3f10b

Please sign in to comment.