import be.nikiroo.jvcard.Data;
import be.nikiroo.jvcard.parsers.Format;
import be.nikiroo.jvcard.parsers.Vcard21Parser;
-import be.nikiroo.jvcard.resources.StringUtils;
-import be.nikiroo.jvcard.resources.bundles.RemoteBundle;
-import be.nikiroo.jvcard.resources.enums.RemotingOption;
+import be.nikiroo.jvcard.remote.SimpleSocket.BlockAppendable;
+import be.nikiroo.jvcard.resources.RemoteBundle;
+import be.nikiroo.jvcard.resources.RemotingOption;
+import be.nikiroo.utils.StringUtils;
/**
* This class implements a small server that can listen for requests to
* </p>
*
* @author niki
- *
+ *
*/
public class Server implements Runnable {
private ServerSocket ss;
try {
ss.open(false);
- while (processCmd(ss))
- ;
+ while (processCmd(ss)) {
+ // nothing to do: process the command
+ }
} catch (IOException e) {
e.printStackTrace();
try {
s.sendLine(StringUtils.fromTime(file.lastModified()));
- while (processLockedCmd(s, name))
- ;
+ while (processLockedCmd(s, name)) {
+ // nothing to do: process the command
+ }
} catch (InvalidParameterException e) {
System.err
.println("Unsupported command received from a client connection, closing it: "
switch (command) {
case GET_CARD: {
- s.sendBlock(doGetCard(name));
+ sendCardBlock(s, name);
break;
}
case POST_CARD: {
} else {
Card card = new Card(vcf, Format.VCard21);
try {
- while (processContactCmd(s, card))
- ;
+ while (processContactCmd(s, card)) {
+ // nothing to do: process the command
+ }
card.save();
s.sendLine(StringUtils.fromTime(card.getLastModified()));
} catch (InvalidParameterException e) {
switch (command) {
case GET_CONTACT: {
Contact contact = card.getById(cmd.getParam());
- if (contact != null)
- s.sendBlock(Vcard21Parser.toStrings(contact, -1));
- else
+ if (contact != null) {
+ BlockAppendable app = s.createBlockAppendable();
+ Vcard21Parser.write(app, contact, -1);
+ app.close();
+ } else {
s.sendBlock();
+ }
break;
}
case POST_CONTACT: {
throw new InvalidParameterException(
"Cannot find contact to modify for UID: " + uid);
}
- while (processDataCmd(s, contact))
- ;
+ while (processDataCmd(s, contact)) {
+ // nothing to do: process the command
+ }
break;
}
case DELETE_CONTACT: {
case GET_DATA: {
for (Data data : contact) {
if (data.getName().equals(cmd.getParam())) {
- for (String line : Vcard21Parser.toStrings(data)) {
- s.send(line);
- }
+ BlockAppendable app = s.createBlockAppendable();
+ Vcard21Parser.write(app, data);
+ // note: we do NOT close 'app', since it would send an EOB
}
}
s.sendBlock();
* @throws IOException
* in case of error
*/
- private List<String> doGetCard(String name) throws IOException {
- List<String> lines = new LinkedList<String>();
-
+ private void sendCardBlock(SimpleSocket s, String name) throws IOException {
File vcf = getFile(name);
+ BlockAppendable app = s.createBlockAppendable();
if (vcf != null && vcf.exists()) {
Card card = new Card(vcf, Format.VCard21);
// timestamp + data
- lines.add(StringUtils.fromTime(card.getLastModified()));
- lines.addAll(Vcard21Parser.toStrings(card));
+ app.append(StringUtils.fromTime(card.getLastModified()) + "\r\n");
+ Vcard21Parser.write(app, card);
}
- return lines;
+ app.close();
}
/**