java - Update the list of clients online -
i'm writing server in java listens @ particular port. different clients send messages server @ regular intervals.
the messages contain respective public keys of clients.
the server has list of public keys.
if newly received public key not nowadays in list, server adds there.
i have done part of comparing public keys , writing public key not nowadays in list.
code multithreaded server (here server listens @ 2 different ports) :
while(!isstopped()){ system.out.println("server working"); seek { serversocket = serversocketchannel.open(); serversocket.configureblocking(false); serversocket.bind(new inetsocketaddress(serverportrequest)); serversocket.register(selector, selectionkey.op_accept); serversocket = serversocketchannel.open(); serversocket.configureblocking(false); serversocket.bind(new inetsocketaddress(serverportaccept)); serversocket.register(selector, selectionkey.op_accept); while(selector.isopen()){ selector.select(); set<selectionkey> readykeys = selector.selectedkeys(); iterator<selectionkey> iterator = readykeys.iterator(); while(iterator.hasnext()){ selectionkey key = (selectionkey) iterator.next(); if(key.isacceptable()){ socketchannel client = serversocket.accept(); (new thread(new workerrunnable(client,"multithreaded server"))).start(); } } } } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } }
the workerrunnable thread check if client's public key in server's list or not :
if(!ispresent(userpk)) { fileoutputstream pk; seek { pk = new fileoutputstream("contactspk/contact"+server.pkcounter++); pk.write(userpk); pk.close(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } }
here userpk
byte
array stores public key obtained message sent client.
how check @ regular intervals (say 30 seconds) client, list of clients, has not sent server message? delete client i.e. public key list of stored public keys of server.
that's great question!
you're there you're trying achieve, want careful couple of gotchas!
you start off code with:
while(!isstopped()){ ... }
which way of ensuring you're consistently listening in on port, you're doing creating mutex lock process. can read on here:
http://en.wikipedia.org/wiki/mutual_exclusion
what ensure no other processes have access processor while you're running code. that, unfortunately you'll see start scale code , add together on functionality, loop prevent decoupling code, ensuring robustness, , adding functionality (i'll later!). first you'll want utilize web framework application, can done using plethora of different options. here 2 resources can help decide:
http://zeroturnaround.com/rebellabs/the-curious-coders-java-web-frameworks-comparison-spring-mvc-grails-vaadin-gwt-wicket-play-struts-and-jsf/
http://en.wikipedia.org/wiki/comparison_of_web_application_frameworks#java
i reccommend starting off spring mvc framework, it's generic , used these days , you'll have ample resources online help started.
once you've picked framework, you're going want create mvc architecture web application, here couple of resources:
http://en.wikipedia.org/wiki/model%e2%80%93view%e2%80%93controller http://blog.codinghorror.com/understanding-model-view-controller/
your view isn't going consist of much, , in fact can deed pass public key along back-end (your controller , model).
with above mentioned, can rid of code looping , waiting hear ping ports, can specify ports hear in spring mvc configuration files.
now fun part! asked:
"how check @ regular intervals (say 30 seconds) client, list of clients, has not sent server message? delete client i.e. public key list of stored public keys of server."
this can achieved via 2 stores in end: redis - session based key store, , simple relation database of flavor.
your algorithm can go follows:
1. every time view forwards info of request backend, add together redis database, in form <id, key> 2. create thread run every 30 seconds do: 2a. list of id's redis 2b. perform left outer bring together on table , b, sql table of ids, , b list of ids redis. left outer bring together returns values inner bring together plus values in left table not match right table. here resource sql joins: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ 2c. result of bring together final answer, sure store in db. 2d. purge keys redis (this one-liner)
you can create thread run every 30 seconds on backend using scheduledexecutorservice follows:
scheduledexecutorservice exec = executors.newsinglethreadscheduledexecutor(); exec.scheduleatfixedrate(new runnable() { @override public void run() { // 2a above: (get list of id's redis) // 2b above (perform left outer bring together on table , b, sql table of ids, , b list of ids redis. left outer bring together returns values inner bring together plus values in left table not match right table.) // 2c above (the result of bring together final answer, sure store in db) // 2d above (purge keys redis (this one-liner) } }, 0, 30, timeunit.seconds);
since concrete , repeatable task, have couple of other options have tradeoffs associated them. create crontab job can execute script above, or utilize triggers built many relational databases's capabilities. main downwards side both loosing code visibility take so.
i mentioned above de-coupiling. coupling (http://en.wikipedia.org/wiki/coupling_(computer_programming) of import concept begin write code scales. want maintain code loosely coupled possible (http://en.wikipedia.org/wiki/loose_coupling) beacuse it's going create debugging , create writing in oop fashion much easier you.
java multithreading client-server rsa public-key
No comments:
Post a Comment