-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectUpdater.java
More file actions
144 lines (129 loc) · 3.77 KB
/
ObjectUpdater.java
File metadata and controls
144 lines (129 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.ArrayList;
/***
class used to update new ideas in reatime and perform searches
**/
class ObjectUpdater extends Thread
{
public static int NEW_THOUGHTS_ONLY = 1;
public static int POP_UP_MODE = 2;
public static int MAX_OBJECTS = 170;
private ObjectProvider objects;
private DAL server;
private long lastCall;
private int mode;
private boolean frozen;
ObjectUpdater(ObjectProvider objects)
{
this.mode = NEW_THOUGHTS_ONLY;
this.frozen = false;
this.objects = objects;
this.server = objects.getDALServer();
this.readTime();
}
public void run()
{
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
break;
}
if (this.frozen)
{
//System.out.println("Updater frozen, skipping");
continue;
}
//------------------- Check for new thoughts on the server ---------------------
ArrayList newObjects;
if(this.server.getBrainstorm() == 0)
{
//System.out.println("Getting all new objects since.");
newObjects = server.getNewObjectsSince(this.lastCall);
}
else
{
//System.out.println("Getting Brainstorm objects from " + this.server.getBrainstorm());
newObjects = server.getBrainstorm(this.server.getBrainstorm());
//newObjects = server.getNewObjectsSince(this.lastCall);
}
if(newObjects.size() > 0)
{
TextObject lastObject = (TextObject)newObjects.get(newObjects.size()-1);
this.setTime(lastObject.getTimestamp());
}
//System.out.println("Number of new thoughts: " + newObjects.size());
for(int i=0; i<newObjects.size(); i++)
{
synchronized(this)
{
Thing cur = (Thing)newObjects.get(i);
if(this.server.getBrainstorm() == 0)
{
//System.out.println("Adding thought as general brainstorm");
if(objects.addWorldOnlyObject(cur))
{
objects.connectLastObject();
}
}
else
{
if(!objects.existsInWorld(cur.getId()) && objects.addWorldOnlyObject(cur))
{
//System.out.println("Adding new thought from brainstorm.");
objects.connectLastObject();
}
}
}
}
//------------------------------------------------------------------------------
//------------------------------- Pop-up thoughts ------------------------------
if(objects.currentSearchChanged() && objects.size() < MAX_OBJECTS)
{
if(objects.getCurrentSearch() != "")
{
this.mode = POP_UP_MODE;
}
else
{
this.mode = NEW_THOUGHTS_ONLY;
}
if (mode == POP_UP_MODE)
{
ArrayList popups = server.searchThoughts(objects.getCurrentSearch());
//System.out.println("Number of new thoughts: " + popups.size());
Thing newObj;
for(int i=0; i<popups.size(); i++)
{
synchronized(this)
{
newObj = (Thing)popups.get(i);
if(!objects.existsInWorld(newObj.getId()))
{
newObj.setMode(Thing.TEMPORARY);
objects.addWorldOnlyObject(newObj);
//objects.reconnectLastObject();
}
}
}
}
}
//------------------------------------------------------------------------------
}
}
public void freeze()
{
this.frozen = true;
}
public void unfreeze()
{
this.frozen = false;
}
public void readTime()
{
this.lastCall = server.getCurrentTime();
}
public void setTime(long time)
{
lastCall = time;
}
}